Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Custom input dialog that returns some value



  • Hi,

    I have a question for the purpose to understand "how to do this" for possible future tasks.

    Qt has input dialog that returns value when this dialog is invoked like:

    double val = QInputDialog::getDouble(nullptr, "Set value",
                   "Value:", 0,
                   -100, 100, 2, &ok) ;
    

    I could only write my own dialog that returns value like that:

    MyInputDialog myInputDialog(defaultValue, nullptr);
    myInputDialog.exec();
    double val = myInputDialog.getValue();
    

    As you can see to get a value from MyInputDialog I need to call specific method. But Qt allows to get value "inline". How this is done?



  • @Please_Help_me_D said in Custom input dialog that returns some value:

    But Qt allows to get value "inline".

    You mean this line:

    double val = QInputDialog::getDouble(....)
    

    QInputDialog::getDouble is a static method which returns the double value.

    If you can not use this feature from QInputDialog itself, it's easy to implement for your own dialog.
    However, if you subclass QInputDialog your MyInputDialog class should have MyInputDialog::getDouble too.



  • @Please_Help_me_D said in Custom input dialog that returns some value:

    But Qt allows to get value "inline".

    You mean this line:

    double val = QInputDialog::getDouble(....)
    

    QInputDialog::getDouble is a static method which returns the double value.

    If you can not use this feature from QInputDialog itself, it's easy to implement for your own dialog.
    However, if you subclass QInputDialog your MyInputDialog class should have MyInputDialog::getDouble too.


  • Lifetime Qt Champion

    Hi,

    The static methods of QInputDialog do exactly what you are writing. They setup a QInputDialog instance with the configuration information you gave it, call exec and then return the value.

    You can do the same with your subclass.



  • @Pl45m4 @SGaist thank you for explanaition.
    No there is no problem with using QInputDialog. But for my task I needed some input dialog where input is double value but this value is represented in QSpinBox as scientific notation like: 2,31e+12. This can't be done with QInputDialog.

    What did I do.
    I found specific spinbox: QScientificSpinbox
    I created new class with QDesigner form where I added QDoubleSpinBox and promoted it to QScientificSpinBox and added two buttons: Ok and Close
    Here is the code:
    scienceinputdialog.h

    #ifndef SCIENCEINPUTDIALOG_H
    #define SCIENCEINPUTDIALOG_H
    
    #include <QDialog>
    
    namespace Ui {
    class ScienceInputDialog;
    }
    
    class ScienceInputDialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit ScienceInputDialog(double value, QWidget *parent);
        ~ScienceInputDialog();
        double getValue();
    
    private slots:
        void on_pushButton_ok_clicked();
        void on_pushButton_close_clicked();
    
    private:
        Ui::ScienceInputDialog *ui;
    
    private:
        double value;
    };
    
    #endif // SCIENCEINPUTDIALOG_H
    
    

    scienceinputdialog.cpp

    #include "scienceinputdialog.h"
    #include "ui_scienceinputdialog.h"
    
    ScienceInputDialog::ScienceInputDialog(double value, QWidget *parent) :
        QDialog(parent),
        ui(new Ui::ScienceInputDialog)
    {
        ui->setupUi(this);
        this->value = value;
        ui->doubleSpinBox->setValue(value);
    }
    
    ScienceInputDialog::~ScienceInputDialog()
    {
        delete ui;
    }
    double ScienceInputDialog::getValue(){
        return value;
    }
    
    void ScienceInputDialog::on_pushButton_ok_clicked()
    {
        this->value = ui->doubleSpinBox->value();
        this->close();
    }
    
    void ScienceInputDialog::on_pushButton_close_clicked()
    {
        this->close();
    }
    
    

    This class works normally. But to run this code I have to create an object of this class, write .exec() and only then output the value.
    But how would you correct the code to implement static method wich do the same job with only one line written like:

    double val = QScienceInputDialog::getValue(double defaultValue);
    

    this is how it looks like:
    1.png


  • Lifetime Qt Champion

    Simply move the threee lines you wrote in your first post into a static function inside ScienceInputDialog and call this function.



  • @Christian-Ehrlicher thank you, that helped


Log in to reply