Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    [SOLVERD] QVector of QPushButton and slots, events

    General and Desktop
    2
    3
    1370
    Loading More Posts
    • 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
      Pepek last edited by

      Hello,
      I have dynamic array of buttons, labels and so on. Number of those depends on user input. Unfortunately each of buttons is doing something else. Number of buttons is 1-16. I may just make 16 slots and just do not connect all, but is there any way to do it with events? I tried but hitbutton() is protected and isDown is not working...
      @void Test::mousePressEvent(QMouseEvent *event){
      for(int i =0;i<buttons.size();i++){
      if(event->button()==Qt::LeftButton){
      // if(buttons.at(i)->hitButton(QCursor::pos())){
      if(buttons.at(i)->isDown()){
      labels.at(i)->setText("Click!");
      }
      } else {
      QWidget::mousePressEvent(event);
      }
      }
      }@

      1 Reply Last reply Reply Quote 0
      • D
        DerManu last edited by

        Not 100% clear what you want to do, but maybe this helps:
        sender() returns the QObject that caused a slot to be called (via a signal). So you could connect all buttons to the same slot and distinguish the buttons in there via qobejct_cast<QPushButton*>(sender()).

        1 Reply Last reply Reply Quote 0
        • P
          Pepek last edited by

          Thanks! It works!

          @void Test::buttonAction(){
          for(int i=0;i<buttons.size();i++){
          if(qobject_cast<QPushButton*>(sender())==buttons.at(i)){
          labels.at(i)->setText("Boom");
          }
          }
          }@

          1 Reply Last reply Reply Quote 0
          • First post
            Last post