Qt Forum

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

    Qt Academy Launch in California!

    Solved How to get the correct sender from a custom widget

    General and Desktop
    qtablewidget signals emit signal & slot
    2
    3
    1999
    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.
    • Rattata1234
      Rattata1234 last edited by

      Hello guys! This is my first post, hopefully i won't make too many mistakes.
      Soo i have a problem using the QTableWidget and a custom widget. I created a custom widget containing a QPushButton and QComboBox. To goal is get the currentIndex from the combobox of the row, where the button was pressed. I forgot to mention that the combobox and the button are in the same row. Here is the code:

      //for loop to fill all rows:
                  PushButton *sendButton = new QPushButton();
                  sendButton->setText("Send");
      
                  QComboBox *myComboBox= new QComboBox();
                  myComboBox->addItem("Test");
      
                  QHBoxLayout *customLayout = new QHBoxLayout();
                  customLayout->addWidget(myComboBox);
                  customLayout->addWidget(sendButton);
      
                  QWidget *customWidget = new QWidget();
                  customWidget->setLayout(customLayout);
      
                  ui->tableWidget->setCellWidget(row, 5, customWidget);
                  commandComboBox->setProperty("row", row);
                  sendButton->setProperty("row", row);
      
                  connect(sendButton, SIGNAL(clicked()), this, SLOT(sendButtonClicked()));
      
      void Test::sendButtonClicked()
      {
          int row;
      
          QPushButton *myButton = qobject_cast<QPushButton *>(sender());
          row = myButton->property("row").toInt();//THIS seems to work
      
          QComboBox *myCombo = qobject_cast<QComboBox *>(sender());
          int index= myCombo->currentIndex();//<--------HERE is the problem
      
      }
      
      

      Can anyone tell me how i can get the current index from the "sender" comboBox?

      Thank you!

      1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        Hi and welcome :)

        It seems you only hook up the button and its clicked() ?

        so,
        QComboBox *myCombo = qobject_cast<QComboBox *>(sender());
        int index= myCombo->currentIndex();//<--------HERE is the problem

        should crash as myCombo is NULL since its not a QComboBox but a QPushButton..
        (so the cast fails)

        If you hook up the combobox signal
        currentIndexChanged(int index)
        to a slot u can get it directly when user click in it.

        Rattata1234 1 Reply Last reply Reply Quote 2
        • Rattata1234
          Rattata1234 @mrjj last edited by

          @mrjj Thank you!
          Sooo i kind of did what you said and used the currentIndexChanged signal. I made a Qbytearray where i store the currentindex and replace the index when an index changed. Then i can use this array to get the currentIndex from the row where the button was pressed. I know that there is probably a better solution but it works^^
          I will mark this post as answered i guess.

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