Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Custom input dialog that returns some value
Forum Updated to NodeBB v4.3 + New Features

Custom input dialog that returns some value

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Please_Help_me_DP Offline
    Please_Help_me_DP Offline
    Please_Help_me_D
    wrote on last edited by
    #1

    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?

    Pl45m4P 1 Reply Last reply
    0
    • Please_Help_me_DP Please_Help_me_D

      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?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @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.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      Please_Help_me_DP 1 Reply Last reply
      2
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • Pl45m4P Pl45m4

          @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_DP Offline
          Please_Help_me_DP Offline
          Please_Help_me_D
          wrote on last edited by Please_Help_me_D
          #4

          @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

          Christian EhrlicherC 1 Reply Last reply
          0
          • Please_Help_me_DP Please_Help_me_D

            @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

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            Please_Help_me_DP 1 Reply Last reply
            3
            • Christian EhrlicherC Christian Ehrlicher

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

              Please_Help_me_DP Offline
              Please_Help_me_DP Offline
              Please_Help_me_D
              wrote on last edited by
              #6

              @Christian-Ehrlicher thank you, that helped

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved