简体   繁体   中英

QInputDialog with multiple fields

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 )?

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();
}

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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM