Need some help in Signal and Slots Connections.
-
Hello everybody, I need some help in a program i am creating. First of all , in my Program i use the QLabel, QSpinbox and QPushButton Widgets. What i need from that program is When i Click the Button to Copy the Value of the Spinbox and Paste it on the Label. I have done whatever possible but it brings me a warning at QObject slot. Let me show you the codes:
mypage.h
@#ifndef MYPAGE_H
#define MYPAGE_H#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>class QLabel;
class QSpinBox;
class QPushButton;namespace Ui {
class Mypage;
}class Mypage : public QWidget
{
Q_OBJECT
public:
explicit Mypage(QWidget *parent = 0);
~Mypage();
private slots:
void pushButtonClicked();private:
Ui::Mypage *ui;
QLabel *label;
QSpinBox *numberBox;
QPushButton *pushButton;
};#endif // MYPAGE_H
@mypage.cpp
@#include "mypage.h"
#include "ui_mypage.h"
#include <QtGui>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>Mypage::Mypage(QWidget *parent) :
QWidget(parent),
ui(new Ui::Mypage)
{numberBox = new QSpinBox; numberBox->setRange(0, 1000); QLabel *label= new QLabel; QPushButton *pushButton = new QPushButton; ui->setupUi(this);
// Here is the Spot that doesn't work. It says that there is no such Slot
QObject::connect(pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
}
void Mypage::pushButtonClicked()
{
label -> setNum(numberBox->value());
}Mypage::~Mypage()
{
delete ui;
}
@Thank you
-
You are hiding the pointer here (you also do the same mistake for the QLabel):
@Mypage::Mypage(QWidget *parent) :
QWidget(parent),
ui(new Ui::Mypage)
{numberBox = new QSpinBox; numberBox->setRange(0, 1000); QLabel *label= new QLabel;
//here you declare another pushButton pointer
// QPushButton *pushButton = new QPushButton;
// you should use the one that is member
pushButton = new QPushButton;//or if you want the button that you added in designer, don't call: new QPushButton; just use: ui->pushButton in the connect statement
ui->setupUi(this);// Here is the Spot that doesn't work. It says that there is no such Slot
QObject::connect(pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
}@
*** If you want to use the button, label and spinbox you added in designer delete these lines: @QLabel *label;
QSpinBox *numberBox;
QPushButton *pushButton;@ from class declaration and the calls to new and access those from ui pointer:the connect will look like this:
@QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));@
And also you use the ui->label to set the text to the lavel you added in designer and same for spinbox -
Made the corrections, but still no results. Check the new .cpp
mypage.cpp
@#include "mypage.h"
#include "ui_mypage.h"
#include <QtGui>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>Mypage::Mypage(QWidget *parent) :
QWidget(parent),
ui(new Ui::Mypage)
{numberBox = new QSpinBox; numberBox->setRange(0, 1000); label= new QLabel; ui->setupUi(this); QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
}
void Mypage::pushButtonClicked()
{
label -> setNum( numberBox->value());
}
@ -
I have updated my previous post please see it again, basically for the label that you added in designer it's the same as for the button:
@
Mypage::Mypage(QWidget *parent) :
QWidget(parent),
ui(new Ui::Mypage)
{
//don't call new (setupUi is doing that)
// numberBox = new QSpinBox;
numberBox->setRange(0, 1000);
//don't call new (setupUi is doing that)
//label= new QLabel;ui->setupUi(this); QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
}
void Mypage::pushButtonClicked()
{
//assuming that you named that way in designer
//you use ui->label and ui->numberBox
ui->label -> setNum( ui->numberBox->value());
}
@
LE: Also you should delete the declaration of pointers from your class:
@
class Mypage : public QWidget
{
Q_OBJECT
public:
explicit Mypage(QWidget *parent = 0);
~Mypage();
private slots:
void pushButtonClicked();private:
Ui::Mypage *ui;
//these are not needed if you want to use those from designer (by using ui->WIDGETNAME)
/*QLabel *label;
QSpinBox *numberBox;
QPushButton pushButton;/
};
@ -
Also all the calls that access the widgets should be after the setupUi call, so there is a mistake that crashes your program:
@
Mypage::Mypage(QWidget *parent) :
QWidget(parent),
ui(new Ui::Mypage)
{
//this line should use ui-> and it should be placed after setupUi call
//numberBox->setRange(0, 1000);ui->setupUi(this); ui->numberBox->setRange(0, 1000); QObject::connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
}
@ -
Okey, I corrected the .h and the .cpp files as you told. Now the problem is at the void Mypage::pushButtonClicked() i think. label and numberBox was the pointers i used in the class from .h The names of QWidgets i have in Designer are : label, spinBox and pushButton
I deleted the QLabel, QSpinbox and QPushButtons from the .h so now it is:
.h
@#ifndef MYPAGE_H
#define MYPAGE_H#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>class QLabel;
class QSpinBox;
class QPushButton;namespace Ui {
class Mypage;
}class Mypage : public QWidget
{
Q_OBJECT
public:
explicit Mypage(QWidget *parent = 0);private slots:
void pushButtonClicked();private:
Ui::Mypage *ui;};
#endif // MYPAGE_H
@and the cpp is :
@#include "mypage.h"
#include "ui_mypage.h"
#include <QtGui>
#include <QPushButton>
#include <QLabel>
#include <QSpinBox>Mypage::Mypage(QWidget *parent) :
QWidget(parent),
ui(new Ui::Mypage)
{
ui->setupUi(this);
ui->spinBox = new QSpinBox;
ui->spinBox->setRange(0, 1000);
ui->label= new QLabel;QObject::connect(ui -> pushButton, SIGNAL(clicked()), this, SLOT(pushButtonClicked()));
}
void Mypage::pushButtonClicked()
{
ui->label -> setNum( ui->label ->value() );
}@
What i have to correct in the pushButtonClicked() procedure? -
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