QT QWebEngineView加载网页 resize 与 resizeEvent的区别

QWebEngineView加载网页
1.pro文件中写入

QT       += core gui webenginewidgets

2.头文件加入

#include 
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

protected:
    void resizeEvent(QResizeEvent *);

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
    QWebEngineView *view;
};

3.主文件:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    view = new QWebEngineView(this);
    //view->resize(this->size());
    view->load(QUrl("http://www.baidu.com/"));
    view->hide();
}

void MainWindow::resizeEvent(QResizeEvent *event){
    view->resize(this->width(),this->height()-50);
}

void MainWindow::on_pushButton_clicked()
{
    //    view = new QWebEngineView(this);
    //    view->resize(ui->frame->size());
    //    view->load(QUrl("http://www.baidu.com/"));
    view->show();
}

直接启用浏览器加载网页方法:

QDesktopServices::openUrl(QUrl("http://www.baidu.com"));

不写入resizeEvent时,调用

view->resize(this->width(),this->height()-50);

运行结果不可动态拉伸
QT QWebEngineView加载网页 resize 与 resizeEvent的区别_第1张图片

写入resizeEvent 运行结果可以动态拉伸。

你可能感兴趣的:(Qt)