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. ui detect mouse event on objects
Forum Updated to NodeBB v4.3 + New Features

ui detect mouse event on objects

Scheduled Pinned Locked Moved Unsolved General and Desktop
26 Posts 6 Posters 3.0k 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.
  • P Offline
    P Offline
    Pfanne
    wrote on last edited by
    #1

    [WIN10] [MVS2019] [QtV6.3.1]

    Hi,

    in my code I got a lot of checkboxes.

        QCheckBox *IO_1;
        QCheckBox *IO_2;
        QCheckBox *IO_3;
        ... 
        ...
        QCheckBox *IO_46;
        QCheckBox *IO_47;
        QCheckBox *IO_48;
    

    To check them by code I use a simple loop and select the checkboxes by searching for the object pointer by object name.

            for (int i = 0; i < 48; i++) {
                QString cbName = "IO_";
                cbName.append(QString::number(i + 1));
                QCheckBox* cb = ui->SITIPEMaster->findChild<QCheckBox*>(cbName);
    
                cb->setChecked(sitipe_master.ptm.id[id].io[i].value);
            }
    

    instead of setting each cb separately...

            ui->IO_1->setChecked(sitipe_master.ptm.id[id].io[0].value);
            ui->IO_2->setChecked(sitipe_master.ptm.id[id].io[1].value);
            ui->IO_3->setChecked(sitipe_master.ptm.id[id].io[2].value);
            ui->IO_4->setChecked(sitipe_master.ptm.id[id].io[3].value);
            ui->IO_5->setChecked(sitipe_master.ptm.id[id].io[4].value);
            ...
            ...
    

    that works fine....

    now I want to detect a a cklick event on one of these checkboxes in a easy way.
    I don´t want to check them alle in a own function/event...

        void on_IO_1_clicked();
        void on_IO_2_clicked();
        void on_IO_3_clicked();
        void on_IO_4_clicked();
        void on_IO_5_clicked();
        ...
        ...
    

    I tryed it with :

    protected:
        void mousePressEvent(QMouseEvent* event);
    
    --------------
    
    void MainWindow::mousePressEvent(QMouseEvent* event) {
        QWidget* const widget = childAt(event->pos());
        qDebug() << "child widget" << widget;
    }
    

    I got the following error:

    "childAt(event->pos());"
    ebent, type of pointer not alowed...

    What is the correct way to detect mouse event on objects?

    M M 2 Replies Last reply
    0
    • P Pfanne

      [WIN10] [MVS2019] [QtV6.3.1]

      Hi,

      in my code I got a lot of checkboxes.

          QCheckBox *IO_1;
          QCheckBox *IO_2;
          QCheckBox *IO_3;
          ... 
          ...
          QCheckBox *IO_46;
          QCheckBox *IO_47;
          QCheckBox *IO_48;
      

      To check them by code I use a simple loop and select the checkboxes by searching for the object pointer by object name.

              for (int i = 0; i < 48; i++) {
                  QString cbName = "IO_";
                  cbName.append(QString::number(i + 1));
                  QCheckBox* cb = ui->SITIPEMaster->findChild<QCheckBox*>(cbName);
      
                  cb->setChecked(sitipe_master.ptm.id[id].io[i].value);
              }
      

      instead of setting each cb separately...

              ui->IO_1->setChecked(sitipe_master.ptm.id[id].io[0].value);
              ui->IO_2->setChecked(sitipe_master.ptm.id[id].io[1].value);
              ui->IO_3->setChecked(sitipe_master.ptm.id[id].io[2].value);
              ui->IO_4->setChecked(sitipe_master.ptm.id[id].io[3].value);
              ui->IO_5->setChecked(sitipe_master.ptm.id[id].io[4].value);
              ...
              ...
      

      that works fine....

      now I want to detect a a cklick event on one of these checkboxes in a easy way.
      I don´t want to check them alle in a own function/event...

          void on_IO_1_clicked();
          void on_IO_2_clicked();
          void on_IO_3_clicked();
          void on_IO_4_clicked();
          void on_IO_5_clicked();
          ...
          ...
      

      I tryed it with :

      protected:
          void mousePressEvent(QMouseEvent* event);
      
      --------------
      
      void MainWindow::mousePressEvent(QMouseEvent* event) {
          QWidget* const widget = childAt(event->pos());
          qDebug() << "child widget" << widget;
      }
      

      I got the following error:

      "childAt(event->pos());"
      ebent, type of pointer not alowed...

      What is the correct way to detect mouse event on objects?

      M Offline
      M Offline
      medyakovvit
      wrote on last edited by
      #2

      @Pfanne

      for (int i = 0; i < 48; i++) 
      {
          QString cbName = "IO_";
          cbName.append(QString::number(i + 1));
           QCheckBox* cb = ui->SITIPEMaster->findChild<QCheckBox*>(cbName);
      
          cb->setChecked(sitipe_master.ptm.id[id].io[i].value);
          // you can use toggled() or clicked()
          connect(cb, &QCheckBox::toggled, cb, [id, i](bool checked)) {
              // here you have 'i' 
          });
      }
      
      P 1 Reply Last reply
      2
      • P Pfanne

        [WIN10] [MVS2019] [QtV6.3.1]

        Hi,

        in my code I got a lot of checkboxes.

            QCheckBox *IO_1;
            QCheckBox *IO_2;
            QCheckBox *IO_3;
            ... 
            ...
            QCheckBox *IO_46;
            QCheckBox *IO_47;
            QCheckBox *IO_48;
        

        To check them by code I use a simple loop and select the checkboxes by searching for the object pointer by object name.

                for (int i = 0; i < 48; i++) {
                    QString cbName = "IO_";
                    cbName.append(QString::number(i + 1));
                    QCheckBox* cb = ui->SITIPEMaster->findChild<QCheckBox*>(cbName);
        
                    cb->setChecked(sitipe_master.ptm.id[id].io[i].value);
                }
        

        instead of setting each cb separately...

                ui->IO_1->setChecked(sitipe_master.ptm.id[id].io[0].value);
                ui->IO_2->setChecked(sitipe_master.ptm.id[id].io[1].value);
                ui->IO_3->setChecked(sitipe_master.ptm.id[id].io[2].value);
                ui->IO_4->setChecked(sitipe_master.ptm.id[id].io[3].value);
                ui->IO_5->setChecked(sitipe_master.ptm.id[id].io[4].value);
                ...
                ...
        

        that works fine....

        now I want to detect a a cklick event on one of these checkboxes in a easy way.
        I don´t want to check them alle in a own function/event...

            void on_IO_1_clicked();
            void on_IO_2_clicked();
            void on_IO_3_clicked();
            void on_IO_4_clicked();
            void on_IO_5_clicked();
            ...
            ...
        

        I tryed it with :

        protected:
            void mousePressEvent(QMouseEvent* event);
        
        --------------
        
        void MainWindow::mousePressEvent(QMouseEvent* event) {
            QWidget* const widget = childAt(event->pos());
            qDebug() << "child widget" << widget;
        }
        

        I got the following error:

        "childAt(event->pos());"
        ebent, type of pointer not alowed...

        What is the correct way to detect mouse event on objects?

        M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        @Pfanne
        You may have a look at QButtonGroup or QGroupBox

        P 1 Reply Last reply
        4
        • M medyakovvit

          @Pfanne

          for (int i = 0; i < 48; i++) 
          {
              QString cbName = "IO_";
              cbName.append(QString::number(i + 1));
               QCheckBox* cb = ui->SITIPEMaster->findChild<QCheckBox*>(cbName);
          
              cb->setChecked(sitipe_master.ptm.id[id].io[i].value);
              // you can use toggled() or clicked()
              connect(cb, &QCheckBox::toggled, cb, [id, i](bool checked)) {
                  // here you have 'i' 
              });
          }
          
          P Offline
          P Offline
          Pfanne
          wrote on last edited by
          #4

          @medyakovvit

           // you can use toggled() or clicked()
              connect(cb, &QCheckBox::toggled, cb, [id, i](bool checked)) {
                  // here you have 'i' 
          

          what exactly happend here?
          you connect the checkbox toggled/clicked event to ???
          Where can I catch this event for further use?

          M JonBJ 2 Replies Last reply
          0
          • P Pfanne

            @medyakovvit

             // you can use toggled() or clicked()
                connect(cb, &QCheckBox::toggled, cb, [id, i](bool checked)) {
                    // here you have 'i' 
            

            what exactly happend here?
            you connect the checkbox toggled/clicked event to ???
            Where can I catch this event for further use?

            M Offline
            M Offline
            mpergand
            wrote on last edited by
            #5

            @Pfanne said in ui detect mouse event on objects:

            what exactly happend here?

            It is a lambda expression, see HERE

            1 Reply Last reply
            0
            • P Pfanne

              @medyakovvit

               // you can use toggled() or clicked()
                  connect(cb, &QCheckBox::toggled, cb, [id, i](bool checked)) {
                      // here you have 'i' 
              

              what exactly happend here?
              you connect the checkbox toggled/clicked event to ???
              Where can I catch this event for further use?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Pfanne
              It's a C++ lambda, like an "anonymous function". Search for lambda on https://wiki.qt.io/New_Signal_Slot_Syntax. @mpergand uses it here to pass the i value from the for loop for you to use in the lambda code, which identifies which checkbox has been toggled.

              1 Reply Last reply
              0
              • M mpergand

                @Pfanne
                You may have a look at QButtonGroup or QGroupBox

                P Offline
                P Offline
                Pfanne
                wrote on last edited by
                #7

                @mpergand

                I take a look at QButtonGroup, this look like the right object for my usecase.
                I tryed this:

                *.h

                private slots:
                    void on_ioGroup_clicked(int id);
                
                MainWindow::MainWindow(QWidget* parent) :
                    QMainWindow(parent),
                    ui(new Ui::MainWindow)
                
                {
                    ui->setupUi(this);
                    QButtonGroup* ioGroup = new QButtonGroup(this);
                    ioGroup->addButton(ui->IO_1, 1;
                    ioGroup->addButton(ui->IO_2, 2);
                    
                    connect(ioGroup, SIGNAL(buttonClicked(int)), this, SLOT(on_ioGroup_clicked(int)));
                }
                
                ----
                
                void MainWindow::on_ioGroup_clicked(int id) {
                    qDebug() << "id clicked" << id;
                }
                    ```
                
                but "on_ioGroup-clicked" will not be executed by clicking on  IO_1 or IO_2.
                JonBJ 1 Reply Last reply
                0
                • P Pfanne

                  @mpergand

                  I take a look at QButtonGroup, this look like the right object for my usecase.
                  I tryed this:

                  *.h

                  private slots:
                      void on_ioGroup_clicked(int id);
                  
                  MainWindow::MainWindow(QWidget* parent) :
                      QMainWindow(parent),
                      ui(new Ui::MainWindow)
                  
                  {
                      ui->setupUi(this);
                      QButtonGroup* ioGroup = new QButtonGroup(this);
                      ioGroup->addButton(ui->IO_1, 1;
                      ioGroup->addButton(ui->IO_2, 2);
                      
                      connect(ioGroup, SIGNAL(buttonClicked(int)), this, SLOT(on_ioGroup_clicked(int)));
                  }
                  
                  ----
                  
                  void MainWindow::on_ioGroup_clicked(int id) {
                      qDebug() << "id clicked" << id;
                  }
                      ```
                  
                  but "on_ioGroup-clicked" will not be executed by clicking on  IO_1 or IO_2.
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Pfanne
                  Use the "modern" syntax for yourconnect()s and you would see there is no buttonClicked(int) with int parameter signal. OTOH there is a QButtonGroup::idClicked(int id) signal.

                  P 1 Reply Last reply
                  1
                  • JonBJ JonB

                    @Pfanne
                    Use the "modern" syntax for yourconnect()s and you would see there is no buttonClicked(int) with int parameter signal. OTOH there is a QButtonGroup::idClicked(int id) signal.

                    P Offline
                    P Offline
                    Pfanne
                    wrote on last edited by
                    #9

                    @JonB

                    yes, I also saw that "buttonClicked(int)" is not up to date!

                    this worked:

                    connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
                    

                    this not:

                        connect(ioGroup, &QButtonGroup::idClicked(int id), this, MainWindow::on_ioGroup_clicked(int id));
                    
                    P JonBJ M 3 Replies Last reply
                    0
                    • P Pfanne

                      @JonB

                      yes, I also saw that "buttonClicked(int)" is not up to date!

                      this worked:

                      connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
                      

                      this not:

                          connect(ioGroup, &QButtonGroup::idClicked(int id), this, MainWindow::on_ioGroup_clicked(int id));
                      
                      P Offline
                      P Offline
                      Pfanne
                      wrote on last edited by
                      #10

                      @Pfanne

                      finaly this worked:

                          connect(ioGroup, SIGNAL(idClicked(int)), this, SLOT(on_ioGroup_clicked(int)));
                      
                      
                      void MainWindow::on_ioGroup_clicked(int id) {
                          qDebug() << "id clicked" << id;
                          QAbstractButton* cb = ioGroup->button(id);
                          qDebug() << "Object" << cb->objectName();
                      }
                      
                      1 Reply Last reply
                      0
                      • P Pfanne

                        @JonB

                        yes, I also saw that "buttonClicked(int)" is not up to date!

                        this worked:

                        connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
                        

                        this not:

                            connect(ioGroup, &QButtonGroup::idClicked(int id), this, MainWindow::on_ioGroup_clicked(int id));
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @Pfanne said in ui detect mouse event on objects:

                        this worked:
                        connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);

                        Correct.

                        finaly this worked:
                        connect(ioGroup, SIGNAL(idClicked(int)), this, SLOT(on_ioGroup_clicked(int)));

                        Since you had it correct first why change it?

                        P 1 Reply Last reply
                        0
                        • P Pfanne

                          @JonB

                          yes, I also saw that "buttonClicked(int)" is not up to date!

                          this worked:

                          connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
                          

                          this not:

                              connect(ioGroup, &QButtonGroup::idClicked(int id), this, MainWindow::on_ioGroup_clicked(int id));
                          
                          M Offline
                          M Offline
                          mpergand
                          wrote on last edited by
                          #12

                          @Pfanne said in ui detect mouse event on objects:

                          connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);

                          You must specify an adress (&) here:
                          &MainWindow::on_ioGroup_clicked);

                          JonBJ 1 Reply Last reply
                          3
                          • M mpergand

                            @Pfanne said in ui detect mouse event on objects:

                            connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);

                            You must specify an adress (&) here:
                            &MainWindow::on_ioGroup_clicked);

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @mpergand said in ui detect mouse event on objects:

                            &MainWindow::on_ioGroup_clicked);

                            OIC I missed that when OP said it was correct.

                            M JonBJ 2 Replies Last reply
                            0
                            • JonBJ JonB

                              @mpergand said in ui detect mouse event on objects:

                              &MainWindow::on_ioGroup_clicked);

                              OIC I missed that when OP said it was correct.

                              M Offline
                              M Offline
                              mpergand
                              wrote on last edited by mpergand
                              #14

                              Hi @JonB

                              Off topic question :
                              How to set text color as you do in red in your posts ?

                              Pl45m4P JonBJ 2 Replies Last reply
                              0
                              • M mpergand

                                Hi @JonB

                                Off topic question :
                                How to set text color as you do in red in your posts ?

                                Pl45m4P Offline
                                Pl45m4P Offline
                                Pl45m4
                                wrote on last edited by
                                #15

                                @mpergand said in ui detect mouse event on objects:

                                How to set text color as you do in red in your posts ?

                                ` Text ` = Text


                                If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                ~E. W. Dijkstra

                                M 1 Reply Last reply
                                0
                                • M mpergand

                                  Hi @JonB

                                  Off topic question :
                                  How to set text color as you do in red in your posts ?

                                  JonBJ Offline
                                  JonBJ Offline
                                  JonB
                                  wrote on last edited by
                                  #16

                                  @mpergand said in ui detect mouse event on objects:

                                  How to set text color as you do in red in your posts ?

                                  Interesting. I don't know and have never set a text color in any of my posts! :) @Pl45m4 post above with red text for Text is the first time I have seen any color.

                                  Oh, hang on! Like buttonClicked()? LMAO, until today's forum update that was in green, no wonder I didn't know about red! The "color" is just up to the forum style. The important thing is that code is in monospace font. Use the Code icon (</>) when you are typing in. Or

                                  `in-line codecharacters`
                                  

                                  ```

                                  multiline code
                                  multiline code
                                  

                                  ```
                                  First one is single-backtick inline, second one is triple-backtick above and below block.

                                  Pl45m4P 1 Reply Last reply
                                  0
                                  • JonBJ JonB referenced this topic on
                                  • JonBJ JonB

                                    @mpergand said in ui detect mouse event on objects:

                                    How to set text color as you do in red in your posts ?

                                    Interesting. I don't know and have never set a text color in any of my posts! :) @Pl45m4 post above with red text for Text is the first time I have seen any color.

                                    Oh, hang on! Like buttonClicked()? LMAO, until today's forum update that was in green, no wonder I didn't know about red! The "color" is just up to the forum style. The important thing is that code is in monospace font. Use the Code icon (</>) when you are typing in. Or

                                    `in-line codecharacters`
                                    

                                    ```

                                    multiline code
                                    multiline code
                                    

                                    ```
                                    First one is single-backtick inline, second one is triple-backtick above and below block.

                                    Pl45m4P Offline
                                    Pl45m4P Offline
                                    Pl45m4
                                    wrote on last edited by Pl45m4
                                    #17

                                    @JonB said in ui detect mouse event on objects:

                                    Interesting. I don't know and have never set a text color in any of my posts! :) @Pl45m4 post above with red text for Text is the first time I have seen any color.

                                    I use this text format every time I mention some Qt term or class. QPushButton, QObject, Q_OBJECT.
                                    So did/do you ;-)
                                    I don't know what exactly you did when writing these posts, but I used single backticks (don't know how they are called)
                                    ` QPushButton ` (now escaped to make them visible)

                                    LMAO, until today's forum update that was in green, no wonder I didn't know about red

                                    I could swear it was red before, with some kind of red-ish background.
                                    At least when used outside of a code section in regular paragraphs.


                                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                    ~E. W. Dijkstra

                                    JonBJ 1 Reply Last reply
                                    0
                                    • Pl45m4P Pl45m4

                                      @JonB said in ui detect mouse event on objects:

                                      Interesting. I don't know and have never set a text color in any of my posts! :) @Pl45m4 post above with red text for Text is the first time I have seen any color.

                                      I use this text format every time I mention some Qt term or class. QPushButton, QObject, Q_OBJECT.
                                      So did/do you ;-)
                                      I don't know what exactly you did when writing these posts, but I used single backticks (don't know how they are called)
                                      ` QPushButton ` (now escaped to make them visible)

                                      LMAO, until today's forum update that was in green, no wonder I didn't know about red

                                      I could swear it was red before, with some kind of red-ish background.
                                      At least when used outside of a code section in regular paragraphs.

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by
                                      #18

                                      @Pl45m4 Maybe. Whatever I thought the original question was how to write in red, rather than in monospace which happens to show as red!

                                      1 Reply Last reply
                                      1
                                      • Pl45m4P Pl45m4

                                        @mpergand said in ui detect mouse event on objects:

                                        How to set text color as you do in red in your posts ?

                                        ` Text ` = Text

                                        M Offline
                                        M Offline
                                        mpergand
                                        wrote on last edited by mpergand
                                        #19

                                        @Pl45m4
                                        Thanks for the tip :)

                                        I finally found the Help button I was looking for yesterday that describes the markdown syntax.

                                        1 Reply Last reply
                                        0
                                        • JonBJ JonB

                                          @Pfanne said in ui detect mouse event on objects:

                                          this worked:
                                          connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);

                                          Correct.

                                          finaly this worked:
                                          connect(ioGroup, SIGNAL(idClicked(int)), this, SLOT(on_ioGroup_clicked(int)));

                                          Since you had it correct first why change it?

                                          P Offline
                                          P Offline
                                          Pfanne
                                          wrote on last edited by Pfanne
                                          #20

                                          @JonB
                                          the first version:

                                          connect(ioGroup, &QButtonGroup::idClicked, this, MainWindow::on_ioGroup_clicked);
                                          

                                          is without the object ID, implementing the ID in this version a error occurse.

                                          M 1 Reply Last reply
                                          0

                                          • Login

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