Passing QString from QDialog to QDialog



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



  • 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");
    
    }
    
    


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


  • Qt Champions 2016

    @gabor53 said:

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



  • Hi @mrjj

    Yes I included.


  • Moderators

    Is the name of the class newwindow or NewWindow?



  • Hi @jsulm

    Yes there is.


  • Qt Champions 2016

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



  • 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)
    1. 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. :) :)


  • Thank you


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.