Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Moving the Qt App
Qt 6.11 is out! See what's new in the release blog

Moving the Qt App

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #1

    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?

    raven-worxR 1 Reply Last reply
    0
    • K Kris Revi

      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?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Kris-Revi
      install an event filter on ui->frameTopBar

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • K Offline
        K Offline
        Kris Revi
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          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()))

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          kshegunovK 1 Reply Last reply
          0
          • VRoninV VRonin

            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()))

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by kshegunov
            #5

            @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, QEvent and especially its type property and eventFilter() together with installEventFilter, the latter you'd typically do after the call to setupUi.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            0
            • K Offline
              K Offline
              Kris Revi
              wrote on last edited by
              #6

              @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, QEvent and especially its type property and eventFilter() together with installEventFilter, the latter you'd typically do after the call to setupUi.

              thanks for that! im a bit slow! but your explanation helped me actually! :) got it working fine now!

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved