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. Send Variable values from QDialog to QMainwindow function

Send Variable values from QDialog to QMainwindow function

Scheduled Pinned Locked Moved Unsolved General and Desktop
qdialogc++qt 5.4
10 Posts 4 Posters 2.0k Views
  • 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.
  • L Offline
    L Offline
    learnist
    wrote on 25 Jun 2020, 07:44 last edited by learnist
    #1

    My UI MainWindow has a QListWidget and , upon clicking of its items ,a corresponding QDialog box must pop up , which prompts the user to enter some values in QDialog box, The values entered into line_edits of QDialog box must stored into a QString variable and this variable should be accessed/used in a function of MainWindow,

    for Example: I have a QListWidget, with 3 items "New York","Glasgow","Mumbai", and when i double click item named "New York", a pop-up shows up asking me this
    b09f5c91-0bde-4c13-96b3-4cd47ef9345b-image.png

    and after i enter 3 and Hilton , the item in QListWidget which was initially "New York" must be changed to "New York -3 , The safehouse is in Hilton" my code for MainWindow.cpp is

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtCore>
    #include <QtGui>
    #include <sstream>
    #include <QtWidgets/qmessagebox.h>
    #include <QtWidgets/qlistwidget.h>
    
    using namespace std;
    
    MainWindow::MainWindow(QWidget* parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->My_listwidget->addItem("New York");
        ui->My_listwidget->addItem("Glasgow");
        ui->My_listwidget->addItem("Mumbai");
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_My_listwidget_itemDoubleClicked(QListWidgetItem* item)
    {
        QString test = item->text();
        std::string test_s = test.toStdString();
    
        if (test_s.find("New York") != std::string::npos) // check if item contains text "New York"
        {
            WinApp winApp;
            winApp.setModal(true);   //Displaying the window here
            winApp.exec();
        }
        
        if (test_s.find("Glasgow") != std::string::npos) 
        {
        // show another dialog box asking some questions
        }
        if (test_s.find("Mumbai") != std::string::npos) 
        {
        // show another dialog box asking some questions
        }
    }
    

    My code for mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QtWidgets/QMainWindow>
    #include "ExecutionContext.h"
    #include <QtWidgets/qlistwidget.h>
    //#include "secdialog.h"
    #include <qregexp.h>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
    
        Q_OBJECT                      //Used to handle events
    
    public:
        MainWindow(QWidget* parent = 0);
        ~MainWindow();                    //Destructor used to free resources
    
    private slots:
    
        void on_xml_scripts_textbox_itemDoubleClicked(QListWidgetItem* item);
    
        Ui::MainWindow* ui; // pointing to UI class
    
    private:
    
    };
    

    my code for WinApp.h , winapp is the name of my dialog

    #include <QtWidgets/qdialog.h>
    #include "ui_WinApp.h"
    
    class WinApp : public QDialog, public Ui::WinApp
    {
        Q_OBJECT
    
    public:
        WinApp(QWidget *parent = Q_NULLPTR);
        ~WinApp();
    
    private slots:
    
    private:
        Ui::WinApp ui;
    };
    

    WinApp.cpp

    #include "WinApp.h"
    
    WinApp::WinApp(QWidget *parent)
        : QDialog(parent)
    {
        ui.setupUi(this);
    }
    
    WinApp::~WinApp()
    {
    }
    
    J 1 Reply Last reply 25 Jun 2020, 07:48
    0
    • L learnist
      25 Jun 2020, 07:44

      My UI MainWindow has a QListWidget and , upon clicking of its items ,a corresponding QDialog box must pop up , which prompts the user to enter some values in QDialog box, The values entered into line_edits of QDialog box must stored into a QString variable and this variable should be accessed/used in a function of MainWindow,

      for Example: I have a QListWidget, with 3 items "New York","Glasgow","Mumbai", and when i double click item named "New York", a pop-up shows up asking me this
      b09f5c91-0bde-4c13-96b3-4cd47ef9345b-image.png

      and after i enter 3 and Hilton , the item in QListWidget which was initially "New York" must be changed to "New York -3 , The safehouse is in Hilton" my code for MainWindow.cpp is

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QtCore>
      #include <QtGui>
      #include <sstream>
      #include <QtWidgets/qmessagebox.h>
      #include <QtWidgets/qlistwidget.h>
      
      using namespace std;
      
      MainWindow::MainWindow(QWidget* parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->My_listwidget->addItem("New York");
          ui->My_listwidget->addItem("Glasgow");
          ui->My_listwidget->addItem("Mumbai");
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::on_My_listwidget_itemDoubleClicked(QListWidgetItem* item)
      {
          QString test = item->text();
          std::string test_s = test.toStdString();
      
          if (test_s.find("New York") != std::string::npos) // check if item contains text "New York"
          {
              WinApp winApp;
              winApp.setModal(true);   //Displaying the window here
              winApp.exec();
          }
          
          if (test_s.find("Glasgow") != std::string::npos) 
          {
          // show another dialog box asking some questions
          }
          if (test_s.find("Mumbai") != std::string::npos) 
          {
          // show another dialog box asking some questions
          }
      }
      

      My code for mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QtWidgets/QMainWindow>
      #include "ExecutionContext.h"
      #include <QtWidgets/qlistwidget.h>
      //#include "secdialog.h"
      #include <qregexp.h>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class MainWindow; }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
      
          Q_OBJECT                      //Used to handle events
      
      public:
          MainWindow(QWidget* parent = 0);
          ~MainWindow();                    //Destructor used to free resources
      
      private slots:
      
          void on_xml_scripts_textbox_itemDoubleClicked(QListWidgetItem* item);
      
          Ui::MainWindow* ui; // pointing to UI class
      
      private:
      
      };
      

      my code for WinApp.h , winapp is the name of my dialog

      #include <QtWidgets/qdialog.h>
      #include "ui_WinApp.h"
      
      class WinApp : public QDialog, public Ui::WinApp
      {
          Q_OBJECT
      
      public:
          WinApp(QWidget *parent = Q_NULLPTR);
          ~WinApp();
      
      private slots:
      
      private:
          Ui::WinApp ui;
      };
      

      WinApp.cpp

      #include "WinApp.h"
      
      WinApp::WinApp(QWidget *parent)
          : QDialog(parent)
      {
          ui.setupUi(this);
      }
      
      WinApp::~WinApp()
      {
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 25 Jun 2020, 07:48 last edited by jsulm
      #2

      @learnist Simply add getter methods to your dialog which you then call in main window to get the values user entered in the dialog.

      Also, there is no need for setModal(true) if you're using exec().

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      L 1 Reply Last reply 25 Jun 2020, 07:56
      2
      • J jsulm
        25 Jun 2020, 07:48

        @learnist Simply add getter methods to your dialog which you then call in main window to get the values user entered in the dialog.

        Also, there is no need for setModal(true) if you're using exec().

        L Offline
        L Offline
        learnist
        wrote on 25 Jun 2020, 07:56 last edited by
        #3

        @jsulm
        Oh okay , and using modal is okay, or should i go for modal less ??

        J 1 Reply Last reply 25 Jun 2020, 08:01
        0
        • L learnist
          25 Jun 2020, 07:56

          @jsulm
          Oh okay , and using modal is okay, or should i go for modal less ??

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 25 Jun 2020, 08:01 last edited by
          #4

          @learnist said in Send Variable values from QDialog to QMainwindow function:

          and using modal is okay, or should i go for modal less ??

          This is something you should know. It is your app.
          But there is one important thing when using non-modal dialog - show() is NOT blocking. That means you can't access the values from dialog just after show(). You will need to do this in a slot connected to https://doc.qt.io/qt-5/qdialog.html#accepted

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          L 1 Reply Last reply 25 Jun 2020, 08:52
          0
          • J jsulm
            25 Jun 2020, 08:01

            @learnist said in Send Variable values from QDialog to QMainwindow function:

            and using modal is okay, or should i go for modal less ??

            This is something you should know. It is your app.
            But there is one important thing when using non-modal dialog - show() is NOT blocking. That means you can't access the values from dialog just after show(). You will need to do this in a slot connected to https://doc.qt.io/qt-5/qdialog.html#accepted

            L Offline
            L Offline
            learnist
            wrote on 25 Jun 2020, 08:52 last edited by
            #5

            @jsulm and thanks for the answer, this really narrows down my search but,

            i placed a getter method in
            winApp.cpp

            QString WinApp::getDialogueValue()
            {
            	return ui.lineEdit->text();
            }
            

            i also added its declaration in winApp.h

            private slots:
            	QString getDialogueValue();
            

            , now how should i call and get its value in the mainwindow here after this

                 WinApp winApp;
                    winApp.setModal(true);
                    winApp.exec();
            
            
            ``
            J 1 Reply Last reply 25 Jun 2020, 08:54
            0
            • L learnist
              25 Jun 2020, 08:52

              @jsulm and thanks for the answer, this really narrows down my search but,

              i placed a getter method in
              winApp.cpp

              QString WinApp::getDialogueValue()
              {
              	return ui.lineEdit->text();
              }
              

              i also added its declaration in winApp.h

              private slots:
              	QString getDialogueValue();
              

              , now how should i call and get its value in the mainwindow here after this

                   WinApp winApp;
                      winApp.setModal(true);
                      winApp.exec();
              
              
              ``
              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 25 Jun 2020, 08:54 last edited by
              #6

              @learnist said in Send Variable values from QDialog to QMainwindow function:

              now how should i call and get its value in the mainwindow here after this

              Like any other method:

              WinApp winApp;
              winApp.setModal(true);
              winApp.exec();
              winApp.getDialogueValue();
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • L Offline
                L Offline
                learnist
                wrote on 25 Jun 2020, 09:08 last edited by
                #7

                Awesome, it works great, but function can return only 1 QString, it might be from 1 line edit, but i have multiple line edits in my QDialog box, so how can i deal with that situation?

                J J.HilkJ JonBJ 3 Replies Last reply 25 Jun 2020, 09:13
                0
                • L learnist
                  25 Jun 2020, 09:08

                  Awesome, it works great, but function can return only 1 QString, it might be from 1 line edit, but i have multiple line edits in my QDialog box, so how can i deal with that situation?

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 25 Jun 2020, 09:13 last edited by
                  #8

                  @learnist said in Send Variable values from QDialog to QMainwindow function:

                  so how can i deal with that situation?

                  You can add getter for each line edit...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3
                  • L learnist
                    25 Jun 2020, 09:08

                    Awesome, it works great, but function can return only 1 QString, it might be from 1 line edit, but i have multiple line edits in my QDialog box, so how can i deal with that situation?

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on 25 Jun 2020, 09:46 last edited by
                    #9

                    @learnist the possibilities are endless
                    for example multiple getters like @jsulm suggested
                    return a struct,
                    return a QStringList
                    return an array
                    etc. etc


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    3
                    • L learnist
                      25 Jun 2020, 09:08

                      Awesome, it works great, but function can return only 1 QString, it might be from 1 line edit, but i have multiple line edits in my QDialog box, so how can i deal with that situation?

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on 25 Jun 2020, 09:58 last edited by JonB
                      #10

                      @learnist said in Send Variable values from QDialog to QMainwindow function:

                      Awesome, it works great, but function can return only 1 QString, it might be from 1 line edit, but i have multiple line edits in my QDialog box, so how can i deal with that situation?

                      I will just throw this thought in. You may not fancy changing your architecture, but it is good to know about if you start having a lot of widgets to deal with.

                      https://doc.qt.io/qt-5/qdatawidgetmapper.html is a very nice interface between multiple widgets and back-end data, without you having to write lots of dedicated getters/setters. But it requires you changing so that your data is all kept in a model. Bear it in mind.

                      1 Reply Last reply
                      1

                      1/10

                      25 Jun 2020, 07:44

                      • Login

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