Need some help in Signal and Slots Connections.
-
Alright, here i am again with my Final Problem. I had to make some changes in my Final Program and now it's the Final Step to complete it. My problem now is Taking an int Value from a Second Window and add the Value to a Label at the First Window. Let's take a look at the U.I. of the program:
1st Screen : !http://s10.postimg.org/3mwa4ipc9/Main_Window1.jpg()!
2nd Screen: http://postimg.org/image/kp27co4d9/Now. How I want it To Work. First Of all User Presses the Record Button at MainWindow1. MainWindow2 Pops up, User Writes an Int Value on the slot, and when the Done Button is Clicked i want this Value to be copied and added to the previous Value that MainWindow1's label had.
Here is my Code:
mainwindow1.h
@#ifndef MAINWINDOW1_H
#define MAINWINDOW1_H
#include <QWidget>
#include <QPushButton>
#include<mainwindow2.h>
namespace Ui {
class MainWindow1;
}class MainWindow1 : public QWidget
{
Q_OBJECTpublic:
explicit MainWindow1(QWidget *parent = 0);
~MainWindow1();public slots:
void openNewWindow();private:
Ui::MainWindow1 *ui;
class MainWindow2 *mMyNewWindow;private slots:
void on_Record_clicked();
};#endif // MAINWINDOW1_H@
mainwindow2.h
@#ifndef MAINWINDOW2_H
#define MAINWINDOW2_H
#include <QWidget>
#include<mainwindow1.h>
#include <QPushButton>
namespace Ui {
class MainWindow2;
}class MainWindow2 : public QWidget
{
Q_OBJECTpublic:
explicit MainWindow2(QWidget *parent = 0);
~MainWindow2();public slots:
void openOldWindow();private:
Ui::MainWindow2 *ui;
class MainWindow1 *mMyOldWindow;private slots:
void on_Done_clicked();};
#endif // MAINWINDOW2_H@
mainwindow1.cpp
@#include "mainwindow1.h"
#include "ui_mainwindow1.h"
#include <QWidget>
#include <QPushButton>
MainWindow1::MainWindow1(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindow1)
{
ui->setupUi(this);
QObject::connect(ui->Record, SIGNAL( clicked() ), this, SLOT(on_Record_clicked()));
}MainWindow1::~MainWindow1()
{
delete ui;
}void MainWindow1::openNewWindow()
{
mMyNewWindow = new MainWindow2();mMyNewWindow->show();
}
void MainWindow1::on_Record_clicked()
{
openNewWindow();
}
@mainwindow2.cpp
@#include "mainwindow2.h"
#include "ui_mainwindow2.h"
#include <QWidget>
#include <QPushButton>
MainWindow2::MainWindow2(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindow2)
{
ui->setupUi(this);QObject::connect(ui->Done, SIGNAL( clicked() ), this, SLOT(on_Done_clicked()));
}
MainWindow2::~MainWindow2()
{
delete ui;
}void MainWindow2::openOldWindow()
{
mMyOldWindow = new MainWindow1();mMyOldWindow->show();
}
void MainWindow2::on_Done_clicked()
{
//Here I am taking the Value that user Entered in the "Score1" as Int
//Now the problem is that i Want this Value to be added to the previous Window
//At the Label Slot
//QString mystring1(ui->Score1->text());
//int c = mystring1.toInt();openOldWindow();
}
@Thanks for your help :)
-
I've made some progress, although I am still waiting for your help :-/ ... Let's take a look again at the Ui of MainWindow1 and MainWindow2:
MainWindow1 : !http://s18.postimg.org/53h2p1w0p/Main_Window1.jpg()!
MainWindow2 : !http://s22.postimg.org/oy6xeu7mp/Main_Window2.jpg()!So, My New Goal is to make the proccess that i will explain now work.
- User Open the Program
- He/she presses the Recount Button and MainWindow2 is coming Up
- He/she Enteres a Value at the "QlineEdit" Widget
- When he/she presses the Done Button I need the program to Save the Value that user entered, and send it to the MainWindow1
- After Pushing the Done Button MainWindow1 is coming up again
- When User presses the Count Button i want the Value that he/she entered to appear at one of the Qlabels (you can check them on photos).
I managed to have a progress, but still it doesn't work, It seems like values are reset after Windows Changes. Have a look at the Code now:
MainWindow1.h
@#ifndef MAINWINDOW1_H
#define MAINWINDOW1_H
#include <QWidget>
#include <QPushButton>
#include<mainwindow2.h>
namespace Ui {
class MainWindow1;
}class MainWindow1 : public QWidget
{
Q_OBJECTpublic:
explicit MainWindow1(QWidget *parent = 0);
~MainWindow1();public slots:
void openNewWindow();
void on_Count_clicked();
void on_Record_clicked();
void Proccess(int a);private:
Ui::MainWindow1 *ui;
class MainWindow2 *mMyNewWindow;private slots:
};
#endif // MAINWINDOW1_H@
MainWindow2.h
@#ifndef MAINWINDOW2_H
#define MAINWINDOW2_H
#include <QWidget>
#include<mainwindow1.h>
#include <QPushButton>
namespace Ui {
class MainWindow2;
}class MainWindow2 : public QWidget
{
Q_OBJECTpublic:
explicit MainWindow2(QWidget *parent = 0);
~MainWindow2();public slots:
void openOldWindow();
void on_Done_clicked();
int GetNumber();private:
Ui::MainWindow2 *ui;
class MainWindow1 *mMyOldWindow;private slots:
};
#endif // MAINWINDOW2_H@
MainWindow1.cpp
@#include "mainwindow1.h"
#include "ui_mainwindow1.h"
#include "mainwindow2.h"
#include <QWidget>
#include <QPushButton>MainWindow1::MainWindow1(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindow1)
{
ui->setupUi(this);
QObject::connect(ui->Record, SIGNAL( clicked() ), this, SLOT(on_Record_clicked()));
QObject::connect(ui->Count, SIGNAL( clicked() ), this, SLOT(on_Count_clicked()));
}MainWindow1::~MainWindow1()
{
delete ui;
}void MainWindow1::openNewWindow()
{
mMyNewWindow = new MainWindow2();
mMyNewWindow->show();}
void MainWindow1::on_Record_clicked()
{
openNewWindow();
}void MainWindow1::Proccess(int a)
{
ui->Test->setNum(a);
}void MainWindow1::on_Count_clicked()
{
int number;
MainWindow2 key2;
key2.on_Done_clicked();
}@MainWindow2.cpp
@#include "mainwindow2.h"
#include "ui_mainwindow2.h"
#include <QWidget>
#include <QPushButton>
#include <QString>
MainWindow2::MainWindow2(QWidget *parent) :QWidget(parent), ui(new Ui::MainWindow2)
{
int p;
ui->setupUi(this);
p = GetNumber();
MainWindow1 key;
key.Proccess(p);QObject::connect(ui->Done, SIGNAL( clicked() ), this, SLOT(on_Done_clicked()));
}
MainWindow2::~MainWindow2()
{
delete ui;
}int MainWindow2::GetNumber()
{
QString mystring1(ui->Score1->text());
int c = mystring1.toInt();ui->label->setNum(c); return c;
}
void MainWindow2::on_Done_clicked()
{
int k;
k = GetNumber();
MainWindow1 key1;
key1.Proccess(k);
openOldWindow();
}void MainWindow2::openOldWindow()
{
mMyOldWindow = new MainWindow1();
mMyOldWindow->show();
}@ -
Hi,
It seems that you are creating new widgets every time you click on a button. May I suggest to simplify things a bit ? It seems that what you want to do with MainWindow2 already exists as "QInputDialog":http://qt-project.org/doc/qt-4.8/qinputdialog.html
I would also suggest to have a look at the examples from the Qt documentation to better understand how widget interaction may be achieved.
Hope it helps
-
Read the "example":http://qt-project.org/doc/qt-4.8/dialogs-standarddialogs.html
-
Hmmm. It's interesting. But, is it possible to do it with Widgets? As you can understand this is just the "problem" i have in my main Program. In there when i Press the Recount Button a New Window(main Class QWidget) pops up in which the user enter the values from a Calculator-type keyboard I created...
-
Sure it is, apply the same techniques used by QInputDialog.
-
After reading tons of posts, questions, answers, i realized that what i need to do is to make my own "QInputDialog::getInteger()" . I read the example you gave me , I Isolated the part that i am interested in, which is to take Int from the pop up dialog that shows up, but i also realized that this is a Standar procedure in Qt Libraries and i cannot change the Widgets used in it. So is there a way to make MainWindow2 on my code to be my own "QInputDialog::getInteger()" - style Dialog?
-
Read the source from QInputDialog, you'll see the pattern used and you can then replicate it for your purpose.
-
For them who want to know how the problem solved:
The easier way to take something of your dialog back to your mainwindow is to
define an external variable at the "top" of your mainwindow.cpp (for example @extern int c@)
and then just to pass it to your dialog (just @int c@ at the top of your dialog cpp).
For more details search for external variables.I know that we should avoid external declarations, but this way the life of a
non-profesional programmer... is realy much easier....This method, interrupts and the problem of arguments in SLOTS...
you don't realy need them.