How to get the correct sender from a custom widget
-
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!
-
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 problemshould 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. -
@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.