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. How to get the correct sender from a custom widget
QtWS25 Last Chance

How to get the correct sender from a custom widget

Scheduled Pinned Locked Moved Solved General and Desktop
qtablewidgetsignals emitsignal & slot
3 Posts 2 Posters 2.4k 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.
  • R Offline
    R Offline
    Rattata1234
    wrote on 10 Jul 2016, 14:37 last edited by
    #1

    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
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 10 Jul 2016, 20:06 last edited by
      #2

      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.

      R 1 Reply Last reply 11 Jul 2016, 18:23
      2
      • M mrjj
        10 Jul 2016, 20:06

        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.

        R Offline
        R Offline
        Rattata1234
        wrote on 11 Jul 2016, 18:23 last edited by
        #3

        @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
        1

        1/3

        10 Jul 2016, 14:37

        • Login

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