[C++] Call a pushbutton and a LineEdit
-
Hello all,
I'm tring to manage a quantity in a LineEdit by using 2 Pusbutton "+" and "-".
I created a count system, but i don't know to call my push buttons for using them
My code : ```void addproduct::setup(){ QString quantity = ui->quantite->text(); quantite.setNum(compteur_.getQuantite()); connect(pushButtonPlus, SIGNAL(clicked()), this, SLOT(incrementerQuantite)); connect(pushButtonLess, SIGNAL(clicked()), this, SLOT(decrementerQuantite)); connect(&compteur_, SIGNAL(envoyerQuantite(int)), quantite, SLOT(setNum(int))); }
The error :
D:\Users\fabdu\Documents\blossom\ajouterproduit.cpp:25: erreur : 'pushButtonPlus' was not declared in this scope D:/Users/fabdu/Documents/blossom/ajouterproduit.cpp:25:13: error: 'pushButtonPlus' was not declared in this scope 25 | connect(pushButtonPlus, SIGNAL(clicked()), this, SLOT(incrementerQuantite)); | ^~~~~~~~~~~~~~ D:\Users\fabdu\Documents\blossom\ajouterproduit.cpp:26: erreur : 'pushButtonLess' was not declared in this scope D:/Users/fabdu/Documents/blossom/ajouterproduit.cpp:26:13: error: 'pushButtonLess' was not declared in this scope 26 | connect(pushButtonLess, SIGNAL(clicked()), this, SLOT(decrementerQuantite)); | ^~~~~~~~~~~~~~
and :
Thank you for advance
-
Hi
it says
'pushButtonPlus' was not declared in this scopemeaning it does not know the pushButtonPlus.
So how did you add it ?
if via Designer, it would live in the ui->pushButtonPlus otherwise
you need to connect where you create them. -
@mrjj thanks for your answer.
Yes i know but i don't know how to call it.
I created my buttons and my LineEdit in the UI :
I tried just "ui->pushButtonPlus;" but it seems not to be enough.
Thanks for advance -
Hi,
Did you properly initialize it before trying to access it ?
-
@SGaist Thanks for your answer.
This is the question of my post : how to initialize them when they exist in my UI ?
Thanks -
@SGaist said in [C++] Call a pushbutton and a LineEdit:
Otherwise
Thanks but i don't find in this documentation what i'm looking for.
I already put a screenshot of what i did : i created my buttons and my LineEdit in my file.ui.Now i want to use them in my file.cpp, in my connect, but my buttons "are not declared in this scope".
Someone can help me to initialize them ?
Thanks -
The code you posted is incomplete so we can only guess what is happening.
-
@SGaist See all of my code :
ajouterproduit.cpp :
#include "ajouterproduit.h" #include "ui_ajouterproduit.h" #include "compteur.h" #include "compteur.cpp" ajouterproduit::ajouterproduit(QWidget *parent) : QDialog(parent), ui(new Ui::ajouterproduit) { ui->setupUi(this); setup(); } ajouterproduit::~ajouterproduit() { delete ui; } void ajouterproduit::setup(){ QString quantite = ui->quantite->text(); ui->pushButtonPlus; quantite.setNum(compteur_.getQuantite()); connect(pushButtonPlus, SIGNAL(clicked()), this, SLOT(incrementerQuantite)); connect(pushButtonLess, SIGNAL(clicked()), this, SLOT(decrementerQuantite)); connect(&compteur_, SIGNAL(envoyerQuantite(int)), quantite, SLOT(setNum(int))); } void ajouterproduit::incrementerQuantite(){ compteur_.augmenterQuantite(); } void ajouterproduit::decrementerQuantite(){ compteur_.reduireQuantite(); } void ajouterproduit::on_pushButton_3_clicked()
ajouterproduit.h :
#ifndef AJOUTERPRODUIT_H #define AJOUTERPRODUIT_H #include "compteur.h" #include <QDialog> namespace Ui { class ajouterproduit; } class ajouterproduit : public QDialog { Q_OBJECT public: explicit ajouterproduit(QWidget *parent = nullptr); ~ajouterproduit(); void setup(); public slots: void incrementerQuantite(); void decrementerQuantite(); private slots: void on_pushButton_3_clicked(); void on_pushButton_clicked(); private: Ui::ajouterproduit *ui; compteur compteur_; }; #endif // AJOUTERPRODUIT_H
Thanks for advance
-
@Cervo2paille said in [C++] Call a pushbutton and a LineEdit:
void ajouterproduit::setup(){
QString quantite = ui->quantite->text(); ui->pushButtonPlus;
The line above does nothing.
quantite.setNum(compteur_.getQuantite()); connect(pushButtonPlus, SIGNAL(clicked()), this, SLOT(incrementerQuantite)); connect(pushButtonLess, SIGNAL(clicked()), this, SLOT(decrementerQuantite));
ui->
is missing. -
@Cervo2paille said in [C++] Call a pushbutton and a LineEdit:
connect(&compteur_, SIGNAL(envoyerQuantite(int)), quantite, SLOT(setNum(int)));
Je prévois un autre problème :)
-
@Cervo2paille said in [C++] Call a pushbutton and a LineEdit:
connect(pushButtonPlus, SIGNAL(clicked()), this, SLOT(incrementerQuantite)); connect(pushButtonLess, SIGNAL(clicked()), this, SLOT(decrementerQuantite));
I don't think your
SLOT(...)
s are correct, when you get that far. -
@SGaist said in [C++] Call a pushbutton and a LineEdit:
ui->pushButtonPlus;
I try to get my button ! If I do a mistake can you give me the correct code to call my button ? Thanks
@mpergand En effet j'ai aussi ce problème ci : "quantite" est une QString et pas Qobject donc ça pose problème. Comment procéder ? Merci
@JonB Oh thanks in fact :
connect(pushButtonPlus, SIGNAL(clicked()), this, SLOT(incrementerQuantite())); connect(pushButtonLess, SIGNAL(clicked()), this, SLOT(decrementerQuantite()));
If you need to test my project, see here :
http://e.pc.cd/bcMotalK
Thanks. -
@Cervo2paille said in [C++] Call a pushbutton and a LineEdit:
If I do a mistake can you give me the correct code to call my button ?
All you have to do is to add ui-> to pushButtonPlus as @SGaist already told you:
connect(ui->pushButtonPlus, SIGNAL(clicked()), this, SLOT(incrementerQuantite()));
And you really should switch to newer Qt5 connect syntax: https://doc.qt.io/qt-6/signalsandslots.html
-
@jsulm thanks between the answers and you message, this is what i understand after few minutes :D
I understand but the documentation never show how to call and UI button by using ui->. :SMy last problem concerns this line :
connect(&compteur_, SIGNAL(envoyerQuantite(int)), quantite, SLOT(setNum(int)));
Thanks -
@Cervo2paille If you read the error message you should know what the problem is: the third parameter is a QString, so quantite is a QString, right? That can't work.
And again: please use new connect syntax... -
Ok i understand :
connect(&compteur_, SIGNAL(envoyerQuantite(int)), ui->quantite, SLOT(setNum(int)));
I got again an error on this line :
But this is a classic class :
Thanks for advance.
PS : don't anwer i'm looking for
-
@Cervo2paille
Looks likecompteur_
(thecompteur
class) is not aQObject
, and you can only useQObject
s for signals or slots. -
In fact it was a simple class
I put it in QObjet :#ifndef COMPTEUR_H #define COMPTEUR_H #include <QObject> class compteur: public QObject { Q_OBJECT public: compteur(); int getQuantite(); void augmenterQuantite(); void reduireQuantite(); signals: int envoyerQuantite(int); private: int quantite_; }; #endif // COMPTEUR_H
But i got many error in my compteur.cpp :
"Multiple definition of compteur". I clean, recompilate my project...
Any idea? Thanks -
@Cervo2paille said in [C++] Call a pushbutton and a LineEdit:
I clean
Did you also try to delete the build folder?
Please post the error messages as text, not images...