[C++] Call a pushbutton and a LineEdit
-
@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
-
@Cervo2paille
Sorry, I was mistaken. The more usual way to inherit fromQObejct
is:// .h file explicit compteur(QObject *parent = nullptr); // .cpp file compteur::compteur(QObject *parent = nullptr) : QObject(parent)
But if you really do not want to accept a parent for
compteur
your code is OK without any explicitQObject()
call, so that is not the cause of your error messages.Does your
ajouterproduit.h
file have#include "compteur.h"
at its head? The error message seems to be complaining inajouterproduit.cpp.obj
. And btw, how did you manage to generate a file with a.cpp.obj
extension?! -
I done an error, an include of .cpp
It works !Thanks all for your time.
-
@Cervo2paille said in [C++] Call a pushbutton and a LineEdit:
I done an error, an include of .cpp
Yeah, not a good idea :)