Reputation: 3830
Is it possible to easily make a QInputDialog
with multiple elements of QComboBox
? Alternatively, what is the most feasible way of prompting a popup-window/dialog with the possibility of different fields (etc. 2 x QComboBox
+ 1 x QLineEdit
)?
Upvotes: 2
Views: 5632
Reputation: 19102
TADA: How to do it the slightly harder way all in code. And the main reason it is slightly harder, is because you have to do all the layout tinkering by hand, with compiling between each iteration.
PS: Forms are super helpful. And once you start doing mobile or embedded development you may want to start using QML and QML forms (Qt Quick Controls).
For describing examples online, forms are difficult and hard to show off, but most large projects I've worked on lately, have some forms or QML in the mix.
Hope that helps.
QDialog * d = new QDialog();
QVBoxLayout * vbox = new QVBoxLayout();
QComboBox * comboBoxA = new QComboBox();
comboBoxA->addItems(QStringList() << "A" << "B" << "C");
QComboBox * comboBoxB = new QComboBox();
comboBoxB->addItems(QStringList() << "A" << "B" << "C");
QLineEdit * lineEditA = new QLineEdit();
QDialogButtonBox * buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel);
QObject::connect(buttonBox, SIGNAL(accepted()), d, SLOT(accept()));
QObject::connect(buttonBox, SIGNAL(rejected()), d, SLOT(reject()));
vbox->addWidget(comboBoxA);
vbox->addWidget(comboBoxB);
vbox->addWidget(lineEditA);
vbox->addWidget(buttonBox);
d->setLayout(vbox);
int result = d->exec();
if(result == QDialog::Accepted)
{
// handle values from d
qDebug() << "The user clicked:"
<< "ComboBoxA" << comboBoxA->currentText()
<< "ComboBoxB" << comboBoxB->currentText()
<< "LineEditA" << lineEditA->text();
}
Upvotes: 6
Reputation: 19102
Right click on your project in Qt Creator.
Click Add New...
.
Click Qt > Qt Designer Form Class
.
In the Qt Designer Form Class
wizard click Dialog with Buttons
, click Ok, then give it a name, and then click finish.
Now drag and drop as many combo boxes and line edits into view as you want.
Change the object names (in the property editor on the right) to something meaningful.
For the combo boxes, double click on them and add items you want shown in the dropdown.
Now when you want to use one of the values from those elements, you can do something like this:
Dialog d;
int result = d.exec();// Show it as a modal dialog
if(result == QDialog::Accepted)
{
// handle values from d
qDebug() << "The user clicked:"
<< "ComboBoxA" << d.getComboBoxAText()
<< "ComboBoxB" << d.getComboBoxBText()
<< "LineEditA" << d.getLineEditAText();
}
But make sure you add some public functions into your Dialog class:
QString Dialog::getComboBoxAText()
{
return ui->comboBoxA()->currentText();
}
Hope that helps.
Upvotes: 1