This example shows how to use two other static methods in QInputDialog for the user to enter an item from a fixed list or some text.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
private:
void setItem();
void setText();
QLineEdit *item_text;
QLineEdit *str_text;
};
This declaration for MainWindow declares two slot methods and two line edit widgets which will be updated by the corresponding slot methods.
MainWindow::MainWindow()
{
setMinimumSize(700, 350);
QPushButton *item_pb = new QPushButton("Item");
item_text = new QLineEdit();
QPushButton *str_pb = new QPushButton("String");
str_text = new QLineEdit();
QPushButton *close_pb = new QPushButton();
close_pb->setText("Close");
QGridLayout *grid1 = new QGridLayout();
grid1->setContentsMargins(75, 45, 75, 25);
grid1->setHorizontalSpacing(20);
grid1->setVerticalSpacing(45);
grid1->addWidget(item_pb, 0, 0);
grid1->addWidget(item_text, 0, 1);
grid1->addWidget(str_pb, 1, 0);
grid1->addWidget(str_text, 1, 1);
QHBoxLayout *layout1 = new QHBoxLayout();
layout1->addStretch();
layout1->addWidget(close_pb);
layout1->addStretch();
QVBoxLayout *layoutMain = new QVBoxLayout(this);
layoutMain->addLayout(grid1);
layoutMain->addSpacing(75);
layoutMain->addLayout(layout1);
connect(item_pb, &QPushButton::clicked,
this, &MainWindow::setItem);
connect(str_pb, &QPushButton::clicked,
this, &MainWindow::setText);
connect(close_pb, &QPushButton::clicked,
this, &QWidget::close);
}
The setup for this code is very similar to the last three examples. The user interface will display two push buttons with two associated line edits.
Signal / Slot Connections
Lines 36 and 39 set up what action should be taken when either the “Item” or “String” push buttons are clicked.
void MainWindow::setItem()
{
QStringList itemList;
itemList << "Spring" << "Summer" << "Fall" << "Winter";
bool ok;
QString data = QInputDialog::getItem(this, "Item",
"Select a Season:", itemList, 0, false, &ok);
if (ok && ! data.isEmpty()) {
item_text->setText(data);
}
}
void MainWindow::setText()
{
bool ok;
QString data = QInputDialog::getText(this, "String",
"User name:", QLineEdit::Normal,
QDir::home().dirName(), &ok);
if (ok && ! data.isEmpty()) {
str_text->setText(data);
}
}
This block of code shows the implementation of the two slot methods specified in connect(). Line 8 calls the static method QInputDialog::getItem() which is implemented in the CsGui library. When the “Item” button is clicked a new dialog box will be displayed with a drop down list of the four seasons. When the user selects an entry it will be displayed in the line edit. The passed parameters set up the Window title, a prompt for the user, and the values for the drop down box.
Line 20 calls the static method QInputDialog::getText() which is also implemented in the CsGui library. When the “Text” button is clicked a new dialog box will be displayed with the passed parameters.
Line 22 uses a the static method QDir::home() to retrieve an object representing the Home directory for the current user’s computer. The call to dirName() returns the last component of the Home directory as a string.
For both of these, if the user presses the OK button to accept their input, then the value of the “ok” variable will be true. This is checked before updating the line edit widget.
Main Function
Since the source code for main() has not changed there is no need to show it again. Refer to example 3 or download the full source for this example.
Running the Example
To build and run this example use the same CMake build file and commands as we showed for the first example. The only suggested modification is on line 2 of the CMakeLists.txt file, change “example_1” to “example_12”.