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. check for clicks on buttons inside a vector?
QtWS25 Last Chance

check for clicks on buttons inside a vector?

Scheduled Pinned Locked Moved Solved General and Desktop
buttonvectordynamically
7 Posts 3 Posters 1.3k Views
  • 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.
  • L Offline
    L Offline
    legitnameyo
    wrote on 5 Apr 2019, 12:00 last edited by legitnameyo 4 May 2019, 12:01
    #1

    I've got the following function to create a button when another button is pressed

    void NoteTest::on_btn_add_note_clicked() {
        QPushButton *crt = new QPushButton(ui->frame_note_list);
    
    
        crt->setText(QString("text"));
        crt->move(0, 30*total_notes);
        crt->resize(186, 30);
        crt->setStyleSheet(QString("QPushButton {color: #444444; background: #e6e6e6; border: 2px solid #cccccc; border-width: 0px 0px 2px 0px;} QPushButton:hover {border: 2px black solid; color: #333333;background: #F7F7F7;} QPushButton:pressed {background: #e6e6e6;}"));
        crt->show();
    
        v_notes.push_back(crt);
        total_notes += 1;
    }
    

    Now, how do check when one of those buttons are pressed? And can I know which one of the buttons that was pressed?

    1 Reply Last reply
    0
    • L Offline
      L Offline
      legitnameyo
      wrote on 5 Apr 2019, 12:16 last edited by legitnameyo 4 May 2019, 12:16
      #2

      I changed the code to

      void NoteTest::on_btn_add_note_clicked() {
          QPushButton *crt = new QPushButton(ui->frame_note_list);
          connect(crt, SIGNAL(clicked()), SLOT(on_btn_note_clicked()));
      
          crt->setText(QString("text"));
          crt->move(0, 30*total_notes);
          crt->resize(186, 30);
          crt->setStyleSheet(QString("QPushButton {color: #444444; background: #e6e6e6; border: 2px solid #cccccc; border-width: 0px 0px 2px 0px;} QPushButton:hover {border: 2px black solid; color: #333333;background: #F7F7F7;} QPushButton:pressed {background: #e6e6e6;}"));
          crt->show();
      
          v_notes.push_back(crt);
          total_notes += 1;
      }
      
      void NoteTest::on_btn_note_clicked() {
          qDebug() << "clicked!";
      }
      

      and now it says "clicked!" when I press a button, but I do not know WHICH one that was pressed...

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 5 Apr 2019, 12:42 last edited by
        #3

        Hi
        Why do you need to know which buttons?
        Anyway, you can use sender()

        void NoteTest::on_btn_note_clicked() {
          QPushButton * theButton = qobject_cast<QPushButton *> ( sender() );
          if(theButton)  {
           //do what you need
           }
         qDebug() << "clicked!";
        }
        
        1 Reply Last reply
        1
        • L Offline
          L Offline
          legitnameyo
          wrote on 5 Apr 2019, 16:45 last edited by
          #4

          I need to know what button has been pressed (not the full history) in order to do the right action. Your way, using sender, worked perfectly! Thanks!

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 5 Apr 2019, 19:56 last edited by
            #5

            What are the "right actions" ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • L Offline
              L Offline
              legitnameyo
              wrote on 7 Apr 2019, 08:44 last edited by
              #6

              The "right actions" is open the right document when I press a certain button in this vector.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 7 Apr 2019, 20:08 last edited by
                #7

                Rather than using a bunch of ifs, what about using a QMap or a QHash to store the information and retrieve that in the slot ? This will make your implementation easier to maintain.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1

                3/7

                5 Apr 2019, 12:42

                • Login

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