Need some help in Signal and Slots Connections.
-
Depends on what do you mean by "add": integer addition or string append.
For both you take text from QLabel (take the "old" value), "convert it to integer":http://qt-project.org/doc/qt-4.8/qstring.html#toInt if you need that, do the add and than set the result back to the QLabel (using setText or setNum depending on what addition you used) -
I only want Integer. For example. When you run the program the label has the value 0. Doing the procedure that you (thank god) solved, it just replaces the "text"(number) at the label.I want to keep the value, and every time OK is pushed, i want it to add the new value to the old one. So, i thought to make an int a; at the pushButtonClicked() procedure but i cannot give it value at the start because every time i click the button it won't keep the last value, but it will keep changing to 0.
-
To the int a you give the value you get from ui->label->text() converted to int (see the documentation link from my previous post) than you add the int you get from spinbox and finally set the result to the label.
Try to do it and post again if you get into some troubles. -
Just for informational reasons i managed to do that. So if anyone wants to learn how to use the QString::ToInt class that's how it is done :)
@void MainWindow::pushButton1Clicked()
{
int l;l = ui->spinBox1 ->value(); QString mystring1(ui->Score1->text()); int c = mystring1.toInt(); c = l + c; ui->Score1 -> setNum( c ); ui->spinBox1 ->setValue(0); }@
Zlatomir, Really thanks for your help. Dunno if i could do it without your help. I really appreciate it :)
-
Alright, now i need some help in something else, it is again connected to "Signal and Slots" theme. First of all have a look at the Ui. !http://s17.postimage.org/lmu8852xb/image.jpg()! So, my "new" problem is that i want somehow to connect the Max and Min Buttons with the Done button, and after that i need to check with a bool:
1st case. Max Clicked ---> Done clicked ---> +200 points in the Label when the Pushbutton is clicked
2nd case. Min Clicked ---> Done Clicked ---> +100 points in the Label when the Pushbutton is clicked
3rd case. Max Clicked ---> Done NOT Clicked ---> -200 points in the Label when Pushbutton clicked
4th case. Min Clicked ---> Done NOT clicked ---> -100 points in the Label when Pushbutton clicked
5th case. All buttons Unclicked, No difference to Label when Pushbutton clickedLet's have a look at the code
mypage.h
@#ifndef MYPAGE_H
#define MYPAGE_H#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>
#include <QString>
#include <QAbstractButton>namespace Ui {
class Mypage;
}class Mypage : public QWidget
{
Q_OBJECT
public:
explicit Mypage(QWidget *parent = 0);// I will explain the "int" at the mypage.cpp code
private slots:
int on_Done_clicked();
int on_MinPoints_clicked();
int on_MaxPoints_clicked();
void pushButtonClicked();private:
Ui::Mypage *ui;};
#endif // MYPAGE_H@
mypage.cpp
@#include "mypage.h"
#include "ui_mypage.h"
#include <QtGui>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>
#include <QString>
#include <QAbstractButton>Mypage::Mypage(QWidget *parent) :
QWidget(parent),
ui(new Ui::Mypage)
{
ui->setupUi(this);
QObject::connect(ui -> MaxPoints, SIGNAL(clicked()), this, SLOT(on_MaxPoints_clicked()));
QObject::connect(ui -> MinPoints, SIGNAL(clicked()), this, SLOT(on_MaxPoints_clicked()));
QObject::connect(ui -> Done, SIGNAL(clicked()), this, SLOT(on_Done_clicked()));
QObject::connect(ui -> pushButton, SIGNAL(clicked()), this, SLOT(on_Done_clicked()));
QObject::connect(ui -> pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
}void MainWindow::pushButton1Clicked()
{
int l,s;l = ui->spinBox1 ->value(); QString mystring1(ui->label->text()); int c = mystring1.toInt(); c = l + c;
//Here is a first explanation of my code.
//I need this int s; to take a value from the on_Done_Clicked()
//that's why i have turned these functions from void to int
s = on_Done_clicked();ui->label -> setNum( c + s ); ui->spinBox1 ->setValue(0); }
//Check that they are now int, not void as they should be
int Mypage::on_MaxPoints_clicked()
{
//So in these functions i need to take a Signal (true or false) from Buttons MaxPoints
//and MinPoints. If they are clicked i need to take true, if not i need to take false
//and after that I use it in the "if (Status1 == true) e.t.c."
int max;
bool Status1;
Status1 = ui->MaxPoints->clicked();
if (Status1 == true)
max = 200;
else max =0;
return max;
}int Mypage::on_MinPoints_clicked()
{
int min;
bool Status2;
Status2 = ui->MinPoints->clicked();
if (Status2 == true)
min = 200;
else min =0;
return min;
}int Mypage::on_Done_clicked()
{
//So here in this Function i want to take the Values from on_MaxPoints_clicked()
//and on_MinPoints_clicked() check what is pressed and what not and return the right
// "done" in the pushButton1Clicked()
int done,Maxvalue,Minvalue;
Maxvalue = on_MaxPoints_clicked();
Minvalue = on_MinPoints_clicked();
if (Maxvalue!=0 && Minvalue==0)
done= Maxvalue;
else if (Maxvalue==0 && Minvalue!=0)
done= Minvalue;
else
done=0;
return done;
}
//In Compiling i have really many errors. I want you to explain me
//first of all if this that i'm trying can be done
//and Secondly if i am following the right way to do it and what i am doing wrong
@ -
First tell us more about the errors, we can't help you if we don't know.
Anyway at a first look i spotted a few things:
-
You added slots so you most likely need to Run qmake and than Build/Run your project
-
You call the on_Done_clicked slot manually in pushButton1Clicked are you sure you want that?
3.1) In many places you call the signal clicked from the slot that is actually connected with the same signal.
3.2 You try to get a bool return from the signal "call"
Possible fix: maybe you actually need "check-able QPushButton":http://qt-project.org/doc/qt-4.8/qabstractbutton.html#checked-prop
You do this in many places, example:
@int Mypage::on_MaxPoints_clicked()
{
//... this code is excuted when MaxPoints button has been clicked so there is no point in calling clicked and also clicked is not returning anything
Status1 = ui->MaxPoints->clicked();
//...
}@
4) You use the naming convention for auto-connect and you also connect manually, this will result in two calls of the slot per each click on the button, auto-connect name convention is naming the slot like this: on_WIDGETNAME_SIGNALNAME you can read about it "here":http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html#a-dialog-without-auto-connect (i prefer to avoid it and make the connections myself) -
-
Alright, i solved the problem. My thoughts were correct :) The void -->int change is working. The only thing that i changed is the " Status1 = ui->MaxPoints->clicked(); " . I focused on the Check/uncheck buttons and with the ui-> MaxPointsCheck -> isChecked(); the job that i wanted is now done :) Again thanks for the answers :) You helped me a lot :)
-
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.