Moving the Qt App
-
in the mainwindow class in header i have
protected: void mousePressEvent(QMouseEvent *) override;and then in the mainwindow cpp i have
void MainWindow::mousePressEvent(QMouseEvent * event) { if(isMaximized() == false && event->type() == QEvent::MouseButtonPress && event->buttons() == Qt::LeftButton) { if (QWindow *window = windowHandle()) window->startSystemMove(); } _MousePos_x = event->x(); _MousePos_y = event->y(); }this works great and all but no matter where i click i can move the window! what i realy want is to check and only enable movement of window if (ui->frameTopBar) is clicked on but how?
-
in the mainwindow class in header i have
protected: void mousePressEvent(QMouseEvent *) override;and then in the mainwindow cpp i have
void MainWindow::mousePressEvent(QMouseEvent * event) { if(isMaximized() == false && event->type() == QEvent::MouseButtonPress && event->buttons() == Qt::LeftButton) { if (QWindow *window = windowHandle()) window->startSystemMove(); } _MousePos_x = event->x(); _MousePos_y = event->y(); }this works great and all but no matter where i click i can move the window! what i realy want is to check and only enable movement of window if (ui->frameTopBar) is clicked on but how?
@Kris-Revi
install an event filter onui->frameTopBar -
im not 100% sure about this eventFilter stuff but i got this working for another thing
Header
bool eventFilter(QObject *object, QEvent *event) override;Cpp
bool MainWindow::eventFilter(QObject *object, QEvent *event) { if (object == (QObject*) ui->frameLedStripDeviceContainer) { if (event->type() == QEvent::Enter) { if ( devicemenucollapsed ) { ui->frameDeviceMenu->setMinimumWidth(264); ui->frameDeviceMenu->setMaximumWidth(264); ui->labelLedStripDevices->setVisible(true); ui->labelLedStripDevicesSeparator->setVisible(false); ui->labelLedMatrixDevices->setVisible(true); ui->labelLedMatrixDevicesSeparator->setVisible(false); ui->labelSoftwareVersion->setVisible(true); for (QPushButton * dButtons : _DeviceButtonList) { dButtons->setText( dButtons->property("orgtext").toString() ); } } } else if (event->type() == QEvent::Leave) { if ( devicemenucollapsed ) { ui->frameDeviceMenu->setMinimumWidth(52); ui->frameDeviceMenu->setMaximumWidth(52); ui->labelLedStripDevices->setVisible(false); ui->labelLedStripDevicesSeparator->setVisible(true); ui->labelLedMatrixDevices->setVisible(false); ui->labelLedMatrixDevicesSeparator->setVisible(true); ui->labelSoftwareVersion->setVisible(false); for (QPushButton * dButtons : _DeviceButtonList) { dButtons->setText(""); } } } return QWidget::eventFilter(object, event); } } ui->frameLedStripDeviceContainer->installEventFilter(this);but how would i implement the QMouseEvent into the eventFilter thing?
-
The event filter is 100% the correct solution but the quick and dirty one could be:
if(QRect(mapToGlobal(ui->frameTopBar->pos()),ui->frameTopBar->size()).contains(event->globalPos()))@VRonin said in Moving the Qt App:
The event filter is 100% the correct solution but the quick and dirty one could be:
Maybe. Promoting to a custom widget is a possibility.
@Kris-Revi said in Moving the Qt App:
but how would i implement the QMouseEvent into the eventFilter thing?
Exactly like in the code you showed: you check the source, then check the event type and react to it however you like. An event filter just intercepts the events before they reach the original destination. Look up
QMouseEvent,QEventand especially itstypeproperty andeventFilter()together withinstallEventFilter, the latter you'd typically do after the call tosetupUi. -
@kshegunov said in Moving the Qt App:
@VRonin said in Moving the Qt App:
The event filter is 100% the correct solution but the quick and dirty one could be:
Maybe. Promoting to a custom widget is a possibility.
@Kris-Revi said in Moving the Qt App:
but how would i implement the QMouseEvent into the eventFilter thing?
Exactly like in the code you showed: you check the source, then check the event type and react to it however you like. An event filter just intercepts the events before they reach the original destination. Look up
QMouseEvent,QEventand especially itstypeproperty andeventFilter()together withinstallEventFilter, the latter you'd typically do after the call tosetupUi.thanks for that! im a bit slow! but your explanation helped me actually! :) got it working fine now!