This example shows how to use two static methods in QInputDialog for the user to enter a number.
#include <QtCore>
#include <QtGui>
class MainWindow : public QWidget
{
public:
MainWindow();
private:
void setInteger();
void setDouble();
QLineEdit *integer_text;
QLineEdit *double_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 *integer_pb = new QPushButton("Integer");
integer_text = new QLineEdit();
QPushButton *double_pb = new QPushButton("Double");
double_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(integer_pb, 0, 0);
grid1->addWidget(integer_text, 0, 1);
grid1->addWidget(double_pb, 1, 0);
grid1->addWidget(double_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(integer_pb, &QPushButton::clicked,
this, &MainWindow::setInteger);
connect(double_pb, &QPushButton::clicked,
this, &MainWindow::setDouble);
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 “Integer” or “Double” push buttons are clicked.
void MainWindow::setInteger()
{
bool ok;
int value = QInputDialog::getInt(this, "Integer",
"Percentage:", 25, 0, 100, 1, &ok);
if (ok) {
integer_text->setText(QString("%1%")
.formatArg(value));
}
}
void MainWindow::setDouble()
{
bool ok;
double value = QInputDialog::getDouble(this, "Double",
"Unit Price:", 37.56, -10000, 10000, 2, &ok);
if (ok) {
double_text->setText(QString("$%1")
.formatArg(value));
}
}
This block of code shows the implementation of the two slot methods specified in connect(). Line 5 calls the static method QInputDialog::getInt() which is implemented in the CsGui library. When the “Integer” button is clicked a new dialog box will be displayed. The passed parameters set up the Window title, a prompt for the user, and a range of acceptable values.
Line 16 calls the static method QInputDialog::getDouble() which is implemented in the CsGui library. When the “Double” button is clicked a new dialog box will be displayed with similar passed parameters.
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_11”.