Home > Software design >  QWebEngineView causes window to move slow
QWebEngineView causes window to move slow

Time:08-09

m trying to move my QMainWindow by using another widget inside QMainWindow.

Im moving my window by overriding :

void mousePressEvent(QMouseEvent *event)
void mouseMoveEvent(QMouseEvent *event)

in a QTabWidget. The window moves well and everything works fine if there is no QWebEngineView widget, but if add the QWebEngineView widget to a layout the movement of the window is laggy, any idea?

here is a minimal reproducible example:

mainwindow.h:

#include <QMainWindow>
#include <QMouseEvent>
#include <QtWidgets/QtWidgets>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

class MyTabWidget : public QTabWidget
{
    Q_OBJECT
    public:
    MyTabWidget(QWidget *parent = 0) : QTabWidget(parent){}
    protected:
    void mousePressEvent(QMouseEvent *event) override
    {
        QWidget::mousePressEvent(event);
        pressPoint = event->pos();
    }
    void mouseMoveEvent(QMouseEvent *event) override
    {
        QWidget::mouseMoveEvent(event);
        this->window()->move(this->window()->pos()   (event->pos() - pressPoint) );
    }

    private:
        QPoint pressPoint;
};
#endif // MAINWINDOW_H

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebEngineView>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    MyTabWidget *tabWidget = new MyTabWidget(this);
    ui->verticalLayout->addWidget(tabWidget);

    QWidget *widget = new QWidget(this);
    QHBoxLayout *hlayout = new QHBoxLayout(this);
    QWebEngineView *view = new QWebEngineView(this);
    widget->setLayout(hlayout);
    hlayout->addWidget(view);


    tabWidget->addTab(widget, "asd");
    view->load(QUrl("https://www.google.com/"));


}

MainWindow::~MainWindow()
{
    delete ui;
}

In this code if you delete everything related to QWebEngineView the window will move without lag.

Edit:

this example does the job but , is there a better way to do this?

setUpdatesEnabled(false); //This in mousePressEvent
bigVisualChanges();
setUpdatesEnabled(true); //This in mouseReleaseEvent

i dont like this way because for example if im watching a youtube video the image will freeze.

CodePudding user response:

Try to reimplement your mousePressEvent and mouseMoveEvent this way, with globalPosition() instead of pos(). Worked for me. Window started to move smoothly.

void mousePressEvent(QMouseEvent *event) override
{
    pressPoint = event->globalPosition().toPoint();
    QWidget::mousePressEvent(event);
}
void mouseMoveEvent(QMouseEvent *event) override
{
    QPoint delta = event->globalPosition().toPoint() - pressPoint;
    this->window()->move(this->window()->pos()   delta );
    pressPoint = event->globalPosition().toPoint();
    QWidget::mouseMoveEvent(event);
}

Update: For better web engine performance (page loading, video playing and webpage animations), build in release configuration.

Update2: test show https://youtu.be/QM3UXjipr8Q.

  •  Tags:  
  • c qt
  • Related