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. Passing QString from QDialog to QDialog
QtWS25 Last Chance

Passing QString from QDialog to QDialog

Scheduled Pinned Locked Moved Solved General and Desktop
qdialog
10 Posts 5 Posters 3.1k 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.
  • G Offline
    G Offline
    gabor53
    wrote on 30 Apr 2016, 03:46 last edited by
    #1

    Hi,
    I have 2 QDialogs: Additem and review. Additem collects data which I'd like to display in review. I have the following:

    additem.h

    namespace Ui {
    class Additem;
    }
    
    class Additem : public QDialog
    {
        Q_OBJECT
    public:
          QString sID;
    
        explicit Additem(QWidget *parent = 0);
        ~Additem();
    

    review.cpp

    #include "review.h"
    #include "ui_review.h"
    #include "additem.h"
    
    review::review(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::review)
    {
        ui->setupUi(this);
    
        qDebug() << "ID: " << Additem::sID;
    
    }
    
    review::~review()
    {
        delete ui;
    }
    

    When I run it I get the following error message:
    What can I do to fix this? Or, which is a better way of passing QStrings from QDialog to QDialog? Thank you.

    P 1 Reply Last reply 30 Apr 2016, 05:22
    0
    • G gabor53
      30 Apr 2016, 03:46

      Hi,
      I have 2 QDialogs: Additem and review. Additem collects data which I'd like to display in review. I have the following:

      additem.h

      namespace Ui {
      class Additem;
      }
      
      class Additem : public QDialog
      {
          Q_OBJECT
      public:
            QString sID;
      
          explicit Additem(QWidget *parent = 0);
          ~Additem();
      

      review.cpp

      #include "review.h"
      #include "ui_review.h"
      #include "additem.h"
      
      review::review(QWidget *parent) :
          QDialog(parent),
          ui(new Ui::review)
      {
          ui->setupUi(this);
      
          qDebug() << "ID: " << Additem::sID;
      
      }
      
      review::~review()
      {
          delete ui;
      }
      

      When I run it I get the following error message:
      What can I do to fix this? Or, which is a better way of passing QStrings from QDialog to QDialog? Thank you.

      P Offline
      P Offline
      panoramix
      wrote on 30 Apr 2016, 05:22 last edited by panoramix
      #2

      Hi @gabor53

      Below I have put a small example but remember that there are definitely more elegant and efficient methods of doing the same thing using the signals.
      Then you should look at the documentation of signals and slots .

      You can try to do something like the above.
      In your project creates two form one will be the MainWindow and the other the NewWindow.

      I paste the code below.

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include "newwindow.h"
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();
          void FuncParent();
      
      
          private slots:
              void on_pushButton_clicked();
      
      private:
          Ui::MainWindow *ui;
          NewWindow *mMyNewWindow;
      };
      
      #endif // MAINWINDOW_H
      
      

      newwindow.h

      #ifndef NEWWINDOW_H
      #define NEWWINDOW_H
      
      #include <QWidget>
      
      namespace Ui {
      class NewWindow;
      }
      
      class NewWindow : public QWidget
      {
          Q_OBJECT
      
      public:
          explicit NewWindow(QWidget *parent = 0);
          ~NewWindow();
          void FuncChild();
      
      private slots:
          void on_pushButton_clicked();
      
      private:
          Ui::NewWindow *ui;
      };
      
      #endif // NEWWINDOW_H
      
      

      main.cpp

      #include "mainwindow.h"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MainWindow w;
          w.show();
      
          return a.exec();
      }
      
      

      mainwindows.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
          mMyNewWindow = new NewWindow(); // Be sure to destroy you window somewhere
      }
      
      MainWindow::~MainWindow()
      {
          mMyNewWindow->close();
          delete mMyNewWindow;
          delete ui;
      }
      
      void MainWindow::on_pushButton_clicked()
      {
      
          mMyNewWindow->show();
          mMyNewWindow->FuncChild();
      
      }
      
      void MainWindow::FuncParent()
      {
          ui->pushButton->setText("Testo impostato da Child Window");
      }
      
      

      newwindow.cpp

      #include "newwindow.h"
      #include "ui_newwindow.h"
      #include "mainwindow.h"
      
      NewWindow::NewWindow(QWidget *parent) :
          QWidget(parent),
          ui(new Ui::NewWindow)
      {
          ui->setupUi(this);
      }
      
      NewWindow::~NewWindow()
      {
          delete ui;
      }
      
      void NewWindow::on_pushButton_clicked()
      {
          //MainWindow.ui.pushButton.setText("cliccato");
          ui->pushButton->setText("cliccato");
      
      }
      
      void NewWindow::FuncChild()
      {
           ui->pushButton->setText("Displayed from MainWindows");
      
      }
      
      
      G 1 Reply Last reply 2 May 2016, 03:46
      2
      • P panoramix
        30 Apr 2016, 05:22

        Hi @gabor53

        Below I have put a small example but remember that there are definitely more elegant and efficient methods of doing the same thing using the signals.
        Then you should look at the documentation of signals and slots .

        You can try to do something like the above.
        In your project creates two form one will be the MainWindow and the other the NewWindow.

        I paste the code below.

        mainwindow.h

        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include "newwindow.h"
        
        namespace Ui {
        class MainWindow;
        }
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();
            void FuncParent();
        
        
            private slots:
                void on_pushButton_clicked();
        
        private:
            Ui::MainWindow *ui;
            NewWindow *mMyNewWindow;
        };
        
        #endif // MAINWINDOW_H
        
        

        newwindow.h

        #ifndef NEWWINDOW_H
        #define NEWWINDOW_H
        
        #include <QWidget>
        
        namespace Ui {
        class NewWindow;
        }
        
        class NewWindow : public QWidget
        {
            Q_OBJECT
        
        public:
            explicit NewWindow(QWidget *parent = 0);
            ~NewWindow();
            void FuncChild();
        
        private slots:
            void on_pushButton_clicked();
        
        private:
            Ui::NewWindow *ui;
        };
        
        #endif // NEWWINDOW_H
        
        

        main.cpp

        #include "mainwindow.h"
        #include <QApplication>
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
        
            return a.exec();
        }
        
        

        mainwindows.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        
        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
            mMyNewWindow = new NewWindow(); // Be sure to destroy you window somewhere
        }
        
        MainWindow::~MainWindow()
        {
            mMyNewWindow->close();
            delete mMyNewWindow;
            delete ui;
        }
        
        void MainWindow::on_pushButton_clicked()
        {
        
            mMyNewWindow->show();
            mMyNewWindow->FuncChild();
        
        }
        
        void MainWindow::FuncParent()
        {
            ui->pushButton->setText("Testo impostato da Child Window");
        }
        
        

        newwindow.cpp

        #include "newwindow.h"
        #include "ui_newwindow.h"
        #include "mainwindow.h"
        
        NewWindow::NewWindow(QWidget *parent) :
            QWidget(parent),
            ui(new Ui::NewWindow)
        {
            ui->setupUi(this);
        }
        
        NewWindow::~NewWindow()
        {
            delete ui;
        }
        
        void NewWindow::on_pushButton_clicked()
        {
            //MainWindow.ui.pushButton.setText("cliccato");
            ui->pushButton->setText("cliccato");
        
        }
        
        void NewWindow::FuncChild()
        {
             ui->pushButton->setText("Displayed from MainWindows");
        
        }
        
        
        G Offline
        G Offline
        gabor53
        wrote on 2 May 2016, 03:46 last edited by
        #3

        Hi @panoramix

        I started to implement the code above. I ran into a problem with this line:

        private:
          
            newwindow *mMyNewWindow;    
        
        

        I keep getting an error message saying 'newwindow' doesn't name a type.
        My QDialogs are:
        mainwindow and newwindow. What am I missing here?
        Thank you.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 2 May 2016, 07:22 last edited by
          #4

          @gabor53 said:

          Hi,
          did you include "newwindow.h"
          in the file where
          newwindow *mMyNewWindow;
          is ?

          G 1 Reply Last reply 3 May 2016, 02:58
          0
          • M mrjj
            2 May 2016, 07:22

            @gabor53 said:

            Hi,
            did you include "newwindow.h"
            in the file where
            newwindow *mMyNewWindow;
            is ?

            G Offline
            G Offline
            gabor53
            wrote on 3 May 2016, 02:58 last edited by
            #5

            Hi @mrjj

            Yes I included.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 3 May 2016, 05:21 last edited by
              #6

              Is the name of the class newwindow or NewWindow?

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

              G 1 Reply Last reply 3 May 2016, 13:40
              0
              • J jsulm
                3 May 2016, 05:21

                Is the name of the class newwindow or NewWindow?

                G Offline
                G Offline
                gabor53
                wrote on 3 May 2016, 13:40 last edited by
                #7

                Hi @jsulm

                Yes there is.

                M 1 Reply Last reply 3 May 2016, 13:42
                0
                • G gabor53
                  3 May 2016, 13:40

                  Hi @jsulm

                  Yes there is.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 3 May 2016, 13:42 last edited by
                  #8

                  @gabor53 said:

                  newwindow' doesn't name a type.

                  If you still get this error, it means it have no idea
                  what newwindow is.
                  often a missing include or as @jsulm asks, a case of misspelled class name.

                  1 Reply Last reply
                  1
                  • N Offline
                    N Offline
                    Ni.Sumi
                    wrote on 3 May 2016, 14:34 last edited by Ni.Sumi 5 Mar 2016, 14:36
                    #9

                    Hi @gabor53 ,

                    Possible :

                    1. If you copied the code simply. I request you to check once again the the macros :) which are at the beginning of the header. Or simply repalce the macro with #pragma once

                    2. Try adding "class NewWindow" instead of include"newwindow". or as @jsulm said check class name(try rename and run, you might know)

                    3. If they both failed to work and need of working version of that above code!!? Here is the working version of that code. download it .
                      Still, interested to find error, use Winmerge to see the difference between my file and yours. :) :)

                    1 Reply Last reply
                    3
                    • G Offline
                      G Offline
                      gabor53
                      wrote on 3 May 2016, 14:38 last edited by
                      #10

                      Thank you

                      1 Reply Last reply
                      0

                      10/10

                      3 May 2016, 14:38

                      • Login

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