Passing QString from QDialog to QDialog
-
wrote on 30 Apr 2016, 03:46 last edited by
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,
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.wrote on 30 Apr 2016, 05:22 last edited by panoramixHi @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 @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"); }
wrote on 2 May 2016, 03:46 last edited byHi @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. -
@gabor53 said:
Hi,
did you include "newwindow.h"
in the file where
newwindow *mMyNewWindow;
is ? -
@gabor53 said:
Hi,
did you include "newwindow.h"
in the file where
newwindow *mMyNewWindow;
is ? -
Is the name of the class newwindow or NewWindow?
-
-
wrote on 3 May 2016, 14:34 last edited by Ni.Sumi 5 Mar 2016, 14:36
Hi @gabor53 ,
Possible :
-
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
-
Try adding "class NewWindow" instead of include"newwindow". or as @jsulm said check class name(try rename and run, you might know)
-
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. :) :)
-
-
wrote on 3 May 2016, 14:38 last edited by
Thank you
1/10