[C++] Call a pushbutton and a LineEdit
-
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...
-
@jsulm yes i did, but nothing change.
My compteur.cpp :
#include "compteur.h" compteur::compteur():quantite_(0) { } int compteur::getQuantite(){ return quantite_; } void compteur::augmenterQuantite(){ quantite_++; emit envoyerQuantite(quantite_); } void compteur::reduireQuantite(){ quantite_--; emit envoyerQuantite(quantite_); }
Thanks
-
@Cervo2paille
I do not know whether it would cause the error messages you see, but where does yourcompteur()
constructor call the baseQObject
constructor? -
@JonB Sorry i don't understand your question.
I got the filling that put my class like a ": public QObject" was enough.If you need more information see my compteur.h :
#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
Thanks