Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidget connect to QPushButton
Forum Updated to NodeBB v4.3 + New Features

QListWidget connect to QPushButton

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 4 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM mrjj

    @Eleonore

    Hi
    Since I'm not sure where you get blocked.

    let's try to imagine we have this

    alt text

    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
    alt text

    I hope it gives you an idea.

    else please show some code so we can find out what is not working for you.

    E Offline
    E Offline
    Eleonore
    wrote on last edited by Eleonore
    #8

    @mrjj
    p1.JPG p2.JPG 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.

    JonBJ 1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #9

      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 up

      so 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
      TASTYO

      we 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 :)

      E 1 Reply Last reply
      1
      • E Eleonore

        @mrjj
        p1.JPG p2.JPG 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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #10

        @Eleonore
        I have not had a McD in > 1 year as a result of Covid. Your menu makes my mouth water ;-) What I find surprising/amusing are the French names like "Le Big Tasty Poulet". Nice to see you still protect your language and keep it intact :)

        1 Reply Last reply
        2
        • mrjjM mrjj

          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 up

          so 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
          TASTYO

          we 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 :)

          E Offline
          E Offline
          Eleonore
          wrote on last edited by
          #11

          @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

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #12

            @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.
            like

            panier  *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 :)

            1 Reply Last reply
            0
            • E Offline
              E Offline
              Eleonore
              wrote on last edited by Eleonore
              #13

              @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.

              jsulmJ 1 Reply Last reply
              0
              • E Eleonore

                @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.

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #14

                @Eleonore said in QListWidget connect to QPushButton:

                Qt doesn' recongnize momentPtr and panierPtr

                Please post the relevant code and the while error message.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                E 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Eleonore said in QListWidget connect to QPushButton:

                  Qt doesn' recongnize momentPtr and panierPtr

                  Please post the relevant code and the while error message.

                  E Offline
                  E Offline
                  Eleonore
                  wrote on last edited by
                  #15

                  @jsulm
                  my panier.cpp
                  12054e9d-3324-4aeb-a8b4-3bcdb4d44623-image.png

                  my panier.h
                  19249d30-f4c5-42e2-8672-1e85c8ddebc8-image.png

                  my commande.h
                  25d9ff7c-3d90-493e-90ae-d1dc6f7b64c5-image.png

                  my commande.cpp
                  ed387ef1-0360-42b0-ab69-daa52fdd8528-image.png

                  and my mainwindow.cpp
                  36779cc4-4d2c-4757-9909-664853ce27be-image.png

                  jsulmJ 1 Reply Last reply
                  0
                  • E Eleonore

                    @jsulm
                    my panier.cpp
                    12054e9d-3324-4aeb-a8b4-3bcdb4d44623-image.png

                    my panier.h
                    19249d30-f4c5-42e2-8672-1e85c8ddebc8-image.png

                    my commande.h
                    25d9ff7c-3d90-493e-90ae-d1dc6f7b64c5-image.png

                    my commande.cpp
                    ed387ef1-0360-42b0-ab69-daa52fdd8528-image.png

                    and my mainwindow.cpp
                    36779cc4-4d2c-4757-9909-664853ce27be-image.png

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #16

                    @Eleonore Next time please post text instead of screenshots.
                    I can't see where momentPtr and panierPtr are declared?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    E 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @Eleonore Next time please post text instead of screenshots.
                      I can't see where momentPtr and panierPtr are declared?

                      E Offline
                      E Offline
                      Eleonore
                      wrote on last edited by
                      #17

                      @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.

                      mrjjM 1 Reply Last reply
                      0
                      • E Eleonore

                        @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.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #18

                        @Eleonore

                        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);
                        
                        }
                        
                        E 1 Reply Last reply
                        0
                        • mrjjM mrjj

                          @Eleonore

                          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);
                          
                          }
                          
                          E Offline
                          E Offline
                          Eleonore
                          wrote on last edited by
                          #19

                          @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().

                          mrjjM 1 Reply Last reply
                          0
                          • E Eleonore

                            @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().

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #20

                            @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.html

                            Its 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.cpp

                            But 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

                            1 Reply Last reply
                            2
                            • E Offline
                              E Offline
                              Eleonore
                              wrote on last edited by
                              #21

                              @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!

                              1 Reply Last reply
                              0

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved