In my ui I have a button, which adds QComboBox (with some items) and QLabel(with some text) when clicked. Variable "index" is the number of added QComboBoxes and QLabels. "ol" is qvector with some data.
void MainWindow::on_iAddOtherButton_clicked()
{
QComboBox *p1 = new QComboBox(this);
p1->setObjectName("comboBox"+QString::number(index));
QLabel *p2 = new QLabel(this);
p2->setObjectName("othLabel"+QString::number(index));
for(int i = 0; i < static_cast<int>(ol.size()); ++i){
p1->addItem(ol.at(i).getName());
ui->otherLayout->addWidget(p1,index+1,0);
}
p2->setText(...some text...));
ui->otherLayout->addWidget(p2,index+1,1);
index++;
}
And this works well, in the layout they are in pairs like this:
QComboBox1 QLabel1
QComboBox2 Qlabel2
Now I want to change value of the QComboBox1, after which, text of the QLabel1, will automatically change to something else. I tried to do this with connect, but QComboBox on currentTextChanged()
emits only QString with a new value. Is there some way to emit name of object + new value? Or there is some completely other solution to do this?
From within your slot, you can call sender()
to get a pointer to the sender of the signal. From there you can decide what to do next based on sender()->objectName()
.
You might also consider using setProperty(...)
on the combobox objects to store an index to a vector of pointers to QLabel instances with the vector being a class member. Then you could retrieve the index inside your slot by calling sender()->property(...)
and use it to access the correct QLabel widget from the vector.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.