QListWidget connect to QPushButton
-
Hi
Since I'm not sure where you get blocked.let's try to imagine we have this
we then connect all buttons to the same slot
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // find all buttons and make list QList<QPushButton *> buttons = findChildren<QPushButton *>(); //loop list and connect all buttons for (auto button : buttons) { connect( button, &QPushButton::clicked, this, &MainWindow::ProductSelected); } }
in the slot all buttons calls, we want to know which button for which we
can use the sender() function for.void MainWindow::ProductSelected() { // sender returns Qwidget so we try to cast it to our buttons QPushButton *button = qobject_cast<QPushButton *> ( sender() ); if (button) { // if its a button // add the buttons text to the listbox. you will ofc. want something else ui->listWidget->addItem( button->text() ) ; } }
then we get
I hope it gives you an idea.
else please show some code so we can find out what is not working for you.
-
Hi
Yes I understand much better now. +10 for the picture.There is no magic with UI files. its just code.
So my ui->listWidget is just a pointer anyway.But you are right. being in different windows does change it a bit.
You need a good place where both moment and panier is known.
Often that is a place where you create both. maybe at start upso what i would do it to define a new signal in moment class
public signals: void ProductSelected(QString name, QString price); and then also add a new slot. public slots: void ButtonProductClicked();
then for all buttons, like
TASTYOwe connect it to our internal slot
connect( TASTYO , &QPushButton::clicked, this, &moment::ButtonProductClicked);then in moment cpp
void moment::ButtonProductClicked() { // sender returns Qwidget so we try to cast it to our buttons QPushButton *button = qobject_cast<QPushButton *> ( sender() ); if (button) { auto price = button->text(); auto productname = ??? /// we kinda also need access to label to get product name, right ?? emit ProductSelected( productname , price); // send our new signal } }
Then in panier you also add a matching slot
public slots: void ProductSelected(QString name, QString price);
and body in cpp where you just add to the listWidget
Then somewhere you need to connect the moments new signal to the panier slot
often it would be where you do
new panier(this)
if that place also knows the moment instance.then connect
connect( momentPtr,&moment::ProductSelected, panierPtr, panier::ProductSelected );and then it should work.
Pretty cool menu btw. Reminds me of MacDonalds :)
-
@mrjj
image url)
Here are the results of my interface with the code just above.
and the goal is to create interactions, or when you press the different buttons with the prices, they are added to my cart on the left. -
Hi
Yes I understand much better now. +10 for the picture.There is no magic with UI files. its just code.
So my ui->listWidget is just a pointer anyway.But you are right. being in different windows does change it a bit.
You need a good place where both moment and panier is known.
Often that is a place where you create both. maybe at start upso what i would do it to define a new signal in moment class
public signals: void ProductSelected(QString name, QString price); and then also add a new slot. public slots: void ButtonProductClicked();
then for all buttons, like
TASTYOwe connect it to our internal slot
connect( TASTYO , &QPushButton::clicked, this, &moment::ButtonProductClicked);then in moment cpp
void moment::ButtonProductClicked() { // sender returns Qwidget so we try to cast it to our buttons QPushButton *button = qobject_cast<QPushButton *> ( sender() ); if (button) { auto price = button->text(); auto productname = ??? /// we kinda also need access to label to get product name, right ?? emit ProductSelected( productname , price); // send our new signal } }
Then in panier you also add a matching slot
public slots: void ProductSelected(QString name, QString price);
and body in cpp where you just add to the listWidget
Then somewhere you need to connect the moments new signal to the panier slot
often it would be where you do
new panier(this)
if that place also knows the moment instance.then connect
connect( momentPtr,&moment::ProductSelected, panierPtr, panier::ProductSelected );and then it should work.
Pretty cool menu btw. Reminds me of MacDonalds :)
@mrjj
For the productname, I used the same technique as for the buttons but with my QLabel. At the end, in panier.cpp, you use in connect, momentPtr and panierPtr, what is it and what does "ptr" mean?
I don't understand what you mean bu new panier(this).
Thank you and great if it looks like it since my goal is to remake their app -
@Eleonore said in QListWidget connect to QPushButton:
Hi" connect, momentPtr and panierPtr, what is it and what does "ptr" mean?"
Hi
Ptr just means pointer. we need pointers to the classes to connect them.- new panier(this).
Some place this window is created.
likepanier *ThePanier = new panier(this). ThePanier ->show();
or something like that.
We can call it the creation place.
and we need "ThePanier"the same goes for the moment class. we also need a pointer to an instance
to connect.Well it looks good. Made me feel like eating one :)
-
@mrjj
Hi, I tried doing it myself before asking you for help, my problem is connect( momentPtr,&moment::ProductSelected, panierPtr, panier::ProductSelected ); Qt doesn' recongnize momentPtr and panierPtr. I tried to pu something else instead but nothing works. I think that there is also a problem with my ProductSelected, wwhat I understand is that Qt says it's not defined.
About the ThePanier ->show(); it's a little complicated because it's located in a function called "void opened order, void MainWindow::ouvrircommande()
{
com = new commander();
com->show();
com ->setFixedSize(1065,768);
com->move(0,0);
panier *pan = new panier;
pan ->show();
pan->setFixedSize(300,768);
pan->move(1066,0);}
This function is located in my mainwindow.cpp to open my two windows (the one with written our product and the card) at the same time.
Thanks for your help and my hope my teacher will think the same thing as you about my interface. -
@mrjj
Hi, I tried doing it myself before asking you for help, my problem is connect( momentPtr,&moment::ProductSelected, panierPtr, panier::ProductSelected ); Qt doesn' recongnize momentPtr and panierPtr. I tried to pu something else instead but nothing works. I think that there is also a problem with my ProductSelected, wwhat I understand is that Qt says it's not defined.
About the ThePanier ->show(); it's a little complicated because it's located in a function called "void opened order, void MainWindow::ouvrircommande()
{
com = new commander();
com->show();
com ->setFixedSize(1065,768);
com->move(0,0);
panier *pan = new panier;
pan ->show();
pan->setFixedSize(300,768);
pan->move(1066,0);}
This function is located in my mainwindow.cpp to open my two windows (the one with written our product and the card) at the same time.
Thanks for your help and my hope my teacher will think the same thing as you about my interface.@Eleonore said in QListWidget connect to QPushButton:
Qt doesn' recongnize momentPtr and panierPtr
Please post the relevant code and the while error message.
-
@Eleonore said in QListWidget connect to QPushButton:
Qt doesn' recongnize momentPtr and panierPtr
Please post the relevant code and the while error message.
-
@Eleonore Next time please post text instead of screenshots.
I can't see where momentPtr and panierPtr are declared?@jsulm
I didn't declare them because I'm not sure I understand what I should put at momentPtr, I don't know if I've already declared them under another name or if I have to declare something new. And if I have to declare, I don't know how and where to do it. -
@jsulm
I didn't declare them because I'm not sure I understand what I should put at momentPtr, I don't know if I've already declared them under another name or if I have to declare something new. And if I have to declare, I don't know how and where to do it.Hi. Good you tried first :)
Well those are just dummy names.
It just means a pointer to the class
so maybe here you have both ?
{ com = new commander(); <<< is this MOMENT class ? com->show(); com ->setFixedSize(1065,768); com->move(0,0); panier *pan = new panier; <<<< this is the panierPtr pointer pan ->show(); pan->setFixedSize(300,768); pan->move(1066,0); }
-
Hi. Good you tried first :)
Well those are just dummy names.
It just means a pointer to the class
so maybe here you have both ?
{ com = new commander(); <<< is this MOMENT class ? com->show(); com ->setFixedSize(1065,768); com->move(0,0); panier *pan = new panier; <<<< this is the panierPtr pointer pan ->show(); pan->setFixedSize(300,768); pan->move(1066,0); }
@mrjj
I kind of knew that it was the pointer, I arleady tried to replace it at that moment but it shows be an error message "use of undeaclared identifier "pan". I have this error message because this part of the code {
com = new commander(); <<< is this MOMENT class ?
com->show();
com ->setFixedSize(1065,768);
com->move(0,0);
panier *pan = new panier; <<<< this is the panierPtr pointer
pan ->show();
pan->setFixedSize(300,768);
pan->move(1066,0);}
is in my main.cpp but this part of code connect( momentPtr,&moment::ProductSelected, panierPtr, panier::ProductSelected ); is in my panier.cpp.
And I don't think I created a pointer for my QPushbutton, I just named it button that I declared in my void moment::ButtonProductClicked(). -
@mrjj
I kind of knew that it was the pointer, I arleady tried to replace it at that moment but it shows be an error message "use of undeaclared identifier "pan". I have this error message because this part of the code {
com = new commander(); <<< is this MOMENT class ?
com->show();
com ->setFixedSize(1065,768);
com->move(0,0);
panier *pan = new panier; <<<< this is the panierPtr pointer
pan ->show();
pan->setFixedSize(300,768);
pan->move(1066,0);}
is in my main.cpp but this part of code connect( momentPtr,&moment::ProductSelected, panierPtr, panier::ProductSelected ); is in my panier.cpp.
And I don't think I created a pointer for my QPushbutton, I just named it button that I declared in my void moment::ButtonProductClicked().@Eleonore
Hi
well we need both a panier pointer and a moment pointer to connect them.its impossible to guess at when i dont have whole project.
so if you have the MOMNET pointer in panier it could work.
also do understand that if inside the panier , then you can use "this" in place of the pointer
Did you read this ?
https://doc.qt.io/qt-5/signalsandslots.htmlIts not complicated but yo udo need a place where both classes are known to connect them.
That is often in MainWindow or some commen place.
Sometimes in main.cppBut i cant say where to do it as i dont know where you NEW a moment instance.
also i guess that
com = new commander();
is another class. so you dont seem to make a MOMENT in main.cpp so it has to be some other place -
@mrjj
Thanks a lot for your help. Unfortunately, after all of this help I still can't create this interraction. I have to hand in my project tonight so I'll just explain that I couldn' t do this part. I hope this conversation can help others in the future.
Thanks a lot!