Passing a variable between forms (NOT WORKING)
-
Hello, guys!
I tried to attempt to pass variable "QString userName" from maingame.cpp to maingame2.cpp and have my label change it's text to userName's value.
I tried to use the QObject::connect function to send signals and slots cross form; however, it seems as if maingame2 never receives it's SLOT, so the label in maingame2.ui never changes.
I did not create the ui from the main.cpp, if that is pertinent info.
##MAIN.CPP
#include <QApplication> #include "maingame2.h" #include "maingame.h" #include <QObject> //int main makes use of arguments c and v //from looking online, argc and argv can be accessed with QCoreApplication::arguments() //returns list of command line arguments int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow b; //refers to the mainwindow.ui b.show(); //shows the mainwindow.ui and makes it accessible for use ///initilize both forms MainGame mainGame; MainGame2 mainGame2; ///connect signals QObject::connect(&mainGame, SIGNAL(returnPressed(QString&)), &mainGame2, SLOT(onReturnPressed(QString&))); return a.exec(); }
#MAINGAME.CPP
#include "ui_maingame.h" #include <QLineEdit> #include <QString> #include <QObject> MainGame::MainGame(QWidget *parent) : QWidget(parent), ui(new Ui::MainGame) { ui->setupUi(this); ///variables //QString userName (currently located in MainGame.h::Public:) ///makes the button appear as a label. ui->btnConfirm->setFlat(true); QObject::connect(ui->lnName, SIGNAL(returnPressed()), ui->btnConfirm, SIGNAL(clicked())); } MainGame::~MainGame() { delete ui; } void MainGame::getText(QString &userName) { userName = ui->lnName->text(); } void MainGame::on_btnConfirm_clicked() { ///call getText in order to gather the user's input getText(userName); emit this->returnPressed(userName); maingame2.show(); ui->lblPrompt->setText(userName); //for debug purposes. }
#MAINGAME.H
#define MAINGAME_H #include <QWidget> #include <maingame2.h> namespace Ui { class MainGame; } class MainGame : public QWidget { Q_OBJECT public: explicit MainGame(QWidget *parent = 0); ~MainGame(); QString userName; QKeyEvent *key; private slots: void getText(QString &userName); void on_btnConfirm_clicked(); private: Ui::MainGame *ui; MainGame2 maingame2; signals: void returnPressed(QString &userName); }; #endif // MAINGAME_H
#MAINGAME2.H
#define MAINGAME2_H #include <QWidget> namespace Ui { class MainGame2; } class MainGame2 : public QWidget { Q_OBJECT public: explicit MainGame2(QWidget *parent = 0); ~MainGame2(); public slots: void onReturnPressed(QString &userName); private: Ui::MainGame2 *ui; }; #endif // MAINGAME2.H
#MAINGAME2.CPP
#include "ui_maingame2.h" MainGame2::MainGame2(QWidget *parent) : QWidget(parent), ui(new Ui::MainGame2) { ui->setupUi(this); } MainGame2::~MainGame2() { delete ui; } void MainGame2::onReturnPressed(QString &userName) { ui->label->setText(userName); }
Any help would be amazing! I need to get this working to get started on my comp sci game project.
-
Hi,
Did you check whether you had any error message on your console ?
On side note, you should rather pass const QString & as parameter of your signal and slots and update the connect statement to
QObject::connect(&mainGame, SIGNAL(returnPressed(QString)), &mainGame2, SLOT(onReturnPressed(QString)));
-
Hi,
Did you check whether you had any error message on your console ?
On side note, you should rather pass const QString & as parameter of your signal and slots and update the connect statement to
QObject::connect(&mainGame, SIGNAL(returnPressed(QString)), &mainGame2, SLOT(onReturnPressed(QString)));
-
I see, I've missed a line: in short, you are not showing the widget you are updating.
In main, you are connecting your two widgets but you don't show them. In MainGame, you have a an instance of MainGame2 that you never connect to but that you are showing.
First thing to do is to clean that part.
-
I see, I've missed a line: in short, you are not showing the widget you are updating.
In main, you are connecting your two widgets but you don't show them. In MainGame, you have a an instance of MainGame2 that you never connect to but that you are showing.
First thing to do is to clean that part.
-
No you don't have to show them before. You have to connect to the correct widget and you can show it whenever you want/need. What you were doing is having two different instances of MainGame2, connecting to the first one, and showing the second one.
-
No you don't have to show them before. You have to connect to the correct widget and you can show it whenever you want/need. What you were doing is having two different instances of MainGame2, connecting to the first one, and showing the second one.
I completely understand what you are saying, but I have no idea how to implement it. I
picked up QT just yesterday, and this is my first year of C++, taken from a highschool course, so I apologize for my ineptitude.I've been messing around with QT about an hour now, to link my connect with the correct MainGame2, but I can only find success when I attempt to do this connect within the main.
I'm asking for a huge favor, but if you can provide me with the actual code, I'd be eternally grateful. The portions that I want to happen have been commented (currently found in maingame.cpp), but the portions that connect properly are still in the code (main.cpp).
main.cpp
#include "mainwindow.h" #include <QApplication> #include "maingame2.h" #include "maingame.h" #include <QObject> //int main makes use of arguments c and v //from looking online, argc and argv can be accessed with QCoreApplication::arguments() //returns list of command line arguments int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow b; //refers to the mainwindow.ui b.show(); //shows the mainwindow.ui and makes it accessible for use ///initilize both forms MainGame mainGame; MainGame2 mainGame2; mainGame.show(); mainGame2.show(); ///connect signals QObject::connect(&mainGame, SIGNAL(returnPressed(const QString&)), &mainGame2, SLOT(onReturnPressed(const QString&))); return a.exec(); }
snippet from maingame.cpp
void MainGame::on_btnConfirm_clicked() { ///call getText in order to gather the user's input getText(userName); emit this->returnPressed(userName); //QObject::connect(this, SIGNAL(returnPressed(const QString&)), &mainGame2, SLOT(onReturnPressed(const QString&))); // mainGame2.show(); ui->lblPrompt->setText(userName); //for debug purposes. }
If you request any extra forms, I can provide; however, I don't believe that they are crucial to this certain scenario, as they remain unchanged from last time. (maingame.h, maingame2.h, mainwindow.h, mainwindow.cpp, maingame2.cpp).
-
Remove MainGame2 relate stuff from your main.cpp as well as the connect statement.
Add
connect(this, SIGNAL(returnPressed(QString)), &maingame2, SLOT(onReturnPressed(QString)));
to MainGame's constructor and it should work.
However, since you are starting, please take the time to go through Qt's documentation, examples and demos. Most of the basic questions can be answered by looking there.
On a side note, it's Qt, QT stands for Apple QuickTime which you could also be learning
-
Remove MainGame2 relate stuff from your main.cpp as well as the connect statement.
Add
connect(this, SIGNAL(returnPressed(QString)), &maingame2, SLOT(onReturnPressed(QString)));
to MainGame's constructor and it should work.
However, since you are starting, please take the time to go through Qt's documentation, examples and demos. Most of the basic questions can be answered by looking there.
On a side note, it's Qt, QT stands for Apple QuickTime which you could also be learning
-
One thing is, you are connecting lnName signal returnPressed on btnConfirm signal clicked, why ?