I do not fully understand how to create a dialog box with several LineEdit , and then, by pressing the OK button, get values ​​from them. If you can any example? I implement the dialog class as follows:

 #ifndef DIAL_H #define DIAL_H #include <QDialog> class dial : public QDialog { Q_OBJECT public: explicit dial(QWidget *parent = 0); int s; void on_pushButton_clicked(); signals: public slots: }; #endif // DIAL_H 

cpp

  #include "dial.h" dial::dial(QWidget *parent) : QDialog(parent) { } void dial::on_pushButton_clicked() { //s = ui->lineEdit->text().toInt(); // сохраняем результат accept(); // говорим что диалог выполнился успешно (а не закрыт с отменой) } 

A window appears but there are no elements on it. How to add them? and then click on the button to read the data?

    3 answers 3

    Widgets in Qt are designed in such a way that, in the overwhelming majority of cases, there is no need to inherit the base classes. This is especially true for QDialog .

     QDialog dlg(this); dlg.setWindowTitle(tr("My dialog")); QLineEdit *ledit1 = new QLineEdit(&dlg); QLineEdit *ledit2 = new QLineEdit(&dlg); QDialogButtonBox *btn_box = new QDialogButtonBox(&dlg); btn_box->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); connect(btn_box, &QDialogButtonBox::accepted, &dlg, &QDialog::accept); connect(btn_box, &QDialogButtonBox::rejected, &dlg, &QDialog::reject); QFormLayout *layout = new QFormLayout(); layout->addRow(tr("Line edit 1:"), ledit1); layout->addRow(tr("Line edit 2:"), ledit2); layout->addWidget(btn_box); dlg.setLayout(layout); // В случае, если пользователь нажал "Ok". if(dlg.exec() == QDialog::Accepted) { const QString &str1 = ledit1->text(); const QString &str2 = ledit2->text(); } 

    A key feature of the widget system in Qt is the use of the so-called. placement managers are successors of the QLayout class. In the example, this role is taken by QFormLayout . It is he who, exactly and with signatures spaced on the left side, will place two text fields within the dialogue.

    • IMHO, it is better to use QtDesigner where possible. As long as your widget is limited to two input fields and two buttons, it looks more or less tolerable, and when there are a dozen elements into four nested layouts, the code will grow into horror. - Alexander
    • @ Alexander, I do not argue. This is a purely individual preference. I do not like the designer, because the inner pedant does not accept the dirt in the code that most certainly occurs when it is generated by a car. Everything should be clear, to the last character and indent. Well, as a counterargument, all these dialogues are basically standardized and are easy to copy and paste with small changes, once written. But, I repeat, to each his own. - alexis031182

    An example with one edit and OK / Cancel buttons is a dialog for entering an octal value: https://github.com/nzeemin/ukncbtl-qt/blob/master/qdialogs.cpp

      Here is an elementary example of such a dialog: Create a dialog, set line values ​​to it in lineEdit. When you click the Apply button, the lines from the dialog are saved to the variables of the main form.

      mydialog.h

       #pragma once #include <QDialog> namespace Ui { class MyDialog; } class MyDialog : public QDialog { Q_OBJECT public: explicit MyDialog(QWidget *parent = 0); ~MyDialog(); void setEdit1Text(const QString &_text); void setEdit2Text(const QString &_text); QString lineEdit1Text() const; QString lineEdit2Text() const; private slots: void on_applyButton_clicked(); private: Ui::MyDialog *ui; }; 

      mydialog.cpp

       #include "mydialog.h" #include "ui_mydialog.h" MyDialog::MyDialog(QWidget *parent) : QDialog(parent), ui(new Ui::MyDialog) { ui->setupUi(this); } MyDialog::~MyDialog() { delete ui; } void MyDialog::setEdit1Text(const QString &_text) { ui->lineEdit1->setText(_text); } void MyDialog::setEdit2Text(const QString &_text) { ui->lineEdit2->setText(_text); } QString MyDialog::lineEdit1Text() const { return ui->lineEdit1->text(); } QString MyDialog::lineEdit2Text() const { return ui->lineEdit2->text(); } void MyDialog::on_applyButton_clicked() { accept(); } 

      mainwindow.h

       #pragma once #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; QString m_line1, m_line2; }; 

      mainwindow.cpp

       #include "mainwindow.h" #include "ui_mainwindow.h" #include "mydialog.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { MyDialog dialog; dialog.setEdit1Text(m_line1); dialog.setEdit2Text(m_line2); if(dialog.exec() == QDialog::Accepted) { m_line1 = dialog.lineEdit1Text(); m_line2 = dialog.lineEdit2Text(); } }