Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Send Variable values from QDialog to QMainwindow function

    General and Desktop
    qdialog c++ qt 5.4
    4
    10
    889
    Loading More Posts
    • 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
      learnist last edited by learnist

      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()
      {
      }
      
      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @learnist last edited by jsulm

        @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 Reply Quote 2
        • L
          learnist @jsulm last edited by

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

          jsulm 1 Reply Last reply Reply Quote 0
          • jsulm
            jsulm Lifetime Qt Champion @learnist last edited by

            @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 Reply Quote 0
            • L
              learnist @jsulm last edited by

              @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();
              
              
              ``
              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @learnist last edited by

                @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 Reply Quote 1
                • L
                  learnist last edited by

                  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?

                  jsulm J.Hilk JonB 3 Replies Last reply Reply Quote 0
                  • jsulm
                    jsulm Lifetime Qt Champion @learnist last edited by

                    @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 Reply Quote 3
                    • J.Hilk
                      J.Hilk Moderators @learnist last edited by

                      @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

                      Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


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

                      1 Reply Last reply Reply Quote 3
                      • JonB
                        JonB @learnist last edited by JonB

                        @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 Reply Quote 1
                        • First post
                          Last post