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. Next/back buttons
Forum Updated to NodeBB v4.3 + New Features

Next/back buttons

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 1.5k Views 3 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.
  • V Offline
    V Offline
    viniltc
    wrote on last edited by viniltc
    #1

    Hi all,

    I'm trying to implement the following:
    next.JPG

    Clicking Next/ Back button should navigate through 1, 2, 3, 4 buttons above and change its color. Let's say if I have slots for each of number buttons to change its property.

    How do I implement using signals and slots ?

    Thanks in advance

    jsulmJ Pl45m4P 2 Replies Last reply
    0
    • V viniltc

      Hi all,

      I'm trying to implement the following:
      next.JPG

      Clicking Next/ Back button should navigate through 1, 2, 3, 4 buttons above and change its color. Let's say if I have slots for each of number buttons to change its property.

      How do I implement using signals and slots ?

      Thanks in advance

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

      @viniltc said in Next/back buttons:

      How do I implement using signals and slots ?

      Like any other? What exactly is the problem? You do not need a slot for each of these 1/2/3/4 buttons, just one which is connected to Next button.
      In the slot connected to Next button check which of the 1/2/3/4 buttons was selected before and then take the next one.

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

      1 Reply Last reply
      6
      • V viniltc

        Hi all,

        I'm trying to implement the following:
        next.JPG

        Clicking Next/ Back button should navigate through 1, 2, 3, 4 buttons above and change its color. Let's say if I have slots for each of number buttons to change its property.

        How do I implement using signals and slots ?

        Thanks in advance

        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #3

        @viniltc

        
        // #######################################################
        // ############### MainWindow Constructor ################
        
        int m_currentBtn = 0;
        QPushButton * btn1 = new QPushButton(this, "1");
        QPushButton * btn2 = new QPushButton(this, "2");
        QPushButton * btn3 = new QPushButton(this, "3");
        QPushButton * btn4 = new QPushButton(this, "4");
        QPushButton * btnNxt = new QPushButton(this, "Next");
        QPushButton * btnPrev = new QPushButton(this, "Back");
        QButtonGroup * btnGrp = new QButtonGroup(this);
        btnGrp->addButton(btn1, 1);
        btnGrp->addButton(btn2, 2);
        btnGrp->addButton(btn3, 3);
        btnGrp->addButton(btn4, 4);
        
        btn1->setStylesheet("Style"); // initial style (start with btn1)
        m_currentBtn = 1;
        
        connect(btnNxt, &QPushButton::clicked, this, &MainWindow::nextBtn);
        connect(btnPrev, &QPushButton::clicked, this, &MainWindow::prevBtn);
        connect(this, &MainWindow::buttonChanged, this, &MainWindow::paintBtn);
        
        // #################################################
        
        
        void MainWindow::nextBtn()
        {
            m_currentBtn++;
            if(m_currentBtn > btnGrp->buttons().size())
            {
                m_currentBtn = 1
            }
            emit buttonChanged(m_currentBtn);
        }
        
        void MainWindow::prevBtn()
        {
            m_currentBtn--;
             if(m_currentBtn < 1)
             {
                 m_currentBtn = btnGrp->buttons().size();
             }
            
          emit buttonChanged(m_currentBtn);
        }
        
        void MainWindow::paintBtn(int id)
        {
           for(int i = 1; i < btnGrp->buttons().size() + 1; i++)
           {
                if(i == id)
                {
                    btnGrp->button(id)->setStylesheet("Style");
                }
                else
                {
                   btnGrp->button(id)->setStylesheet(""); // reset all btns to default
                }
           }
           
        }
        
        

        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        mr ZXM 1 Reply Last reply
        2
        • Pl45m4P Pl45m4

          @viniltc

          
          // #######################################################
          // ############### MainWindow Constructor ################
          
          int m_currentBtn = 0;
          QPushButton * btn1 = new QPushButton(this, "1");
          QPushButton * btn2 = new QPushButton(this, "2");
          QPushButton * btn3 = new QPushButton(this, "3");
          QPushButton * btn4 = new QPushButton(this, "4");
          QPushButton * btnNxt = new QPushButton(this, "Next");
          QPushButton * btnPrev = new QPushButton(this, "Back");
          QButtonGroup * btnGrp = new QButtonGroup(this);
          btnGrp->addButton(btn1, 1);
          btnGrp->addButton(btn2, 2);
          btnGrp->addButton(btn3, 3);
          btnGrp->addButton(btn4, 4);
          
          btn1->setStylesheet("Style"); // initial style (start with btn1)
          m_currentBtn = 1;
          
          connect(btnNxt, &QPushButton::clicked, this, &MainWindow::nextBtn);
          connect(btnPrev, &QPushButton::clicked, this, &MainWindow::prevBtn);
          connect(this, &MainWindow::buttonChanged, this, &MainWindow::paintBtn);
          
          // #################################################
          
          
          void MainWindow::nextBtn()
          {
              m_currentBtn++;
              if(m_currentBtn > btnGrp->buttons().size())
              {
                  m_currentBtn = 1
              }
              emit buttonChanged(m_currentBtn);
          }
          
          void MainWindow::prevBtn()
          {
              m_currentBtn--;
               if(m_currentBtn < 1)
               {
                   m_currentBtn = btnGrp->buttons().size();
               }
              
            emit buttonChanged(m_currentBtn);
          }
          
          void MainWindow::paintBtn(int id)
          {
             for(int i = 1; i < btnGrp->buttons().size() + 1; i++)
             {
                  if(i == id)
                  {
                      btnGrp->button(id)->setStylesheet("Style");
                  }
                  else
                  {
                     btnGrp->button(id)->setStylesheet(""); // reset all btns to default
                  }
             }
             
          }
          
          
          mr ZXM Offline
          mr ZXM Offline
          mr ZX
          wrote on last edited by
          #4

          @Pl45m4 said in Next/back buttons:

          QPushButton * btn1 = new QPushButton(this, "1");
          QPushButton * btn2 = new QPushButton(this, "2");
          QPushButton * btn3 = new QPushButton(this, "3");
          QPushButton * btn4 = new QPushButton(this, "4");
          QPushButton * btnNxt = new QPushButton(this, "Next");
          QPushButton * btnPrev = new QPushButton(this, "Back");
          QButtonGroup * btnGrp = new QButtonGroup(this);

          hi bro
          i see many error in your code .
          u can use -> just in pointer no in variable for example btnGrp->buttons()->size() is mistake u should writebtnGrp->buttons().size() ;
          secondly you have 4 button but wen btn1 selected and click in back you emit 5 , but btn5 is not exist ["m_currentBtn = btnGrp->buttons()->size() + 1;"] remove +1;
          and verry error ...
          for using Signal and slot you can write like :

              connect(btnNxt, SIGNAL(clicked(bool)), this, SLOT(nextBtn()));
              connect(btnPrev, SIGNAL(clicked(bool)), this, SLOT(prevBtn()));
              connect(this, SIGNAL(buttonChanged(int)), this, SLOT(paintBtn(int)));
          

          in your class in Header (MainWindow.h ) you can write :

          private slots:
              void nextBtn();
              void prevBtn();
              void paintBtn(int id);
          signals:
              void buttonChanged(int);
          

          now when user click on next or prev (emit buttonChanged) run paintBtn(int id) .

          good luck

          Pl45m4P 1 Reply Last reply
          1
          • mr ZXM mr ZX

            @Pl45m4 said in Next/back buttons:

            QPushButton * btn1 = new QPushButton(this, "1");
            QPushButton * btn2 = new QPushButton(this, "2");
            QPushButton * btn3 = new QPushButton(this, "3");
            QPushButton * btn4 = new QPushButton(this, "4");
            QPushButton * btnNxt = new QPushButton(this, "Next");
            QPushButton * btnPrev = new QPushButton(this, "Back");
            QButtonGroup * btnGrp = new QButtonGroup(this);

            hi bro
            i see many error in your code .
            u can use -> just in pointer no in variable for example btnGrp->buttons()->size() is mistake u should writebtnGrp->buttons().size() ;
            secondly you have 4 button but wen btn1 selected and click in back you emit 5 , but btn5 is not exist ["m_currentBtn = btnGrp->buttons()->size() + 1;"] remove +1;
            and verry error ...
            for using Signal and slot you can write like :

                connect(btnNxt, SIGNAL(clicked(bool)), this, SLOT(nextBtn()));
                connect(btnPrev, SIGNAL(clicked(bool)), this, SLOT(prevBtn()));
                connect(this, SIGNAL(buttonChanged(int)), this, SLOT(paintBtn(int)));
            

            in your class in Header (MainWindow.h ) you can write :

            private slots:
                void nextBtn();
                void prevBtn();
                void paintBtn(int id);
            signals:
                void buttonChanged(int);
            

            now when user click on next or prev (emit buttonChanged) run paintBtn(int id) .

            good luck

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #5

            @mr-ZX

            I never had the intention to provide compilable code incl. headers etc... I wrote this to point OP ( @viniltc ) into the right direction.

            @mr-ZX said in Next/back buttons:

            writebtnGrp->buttons().size()

            But yes, you are right in this case :)

            @mr-ZX said in Next/back buttons:

            secondly you have 4 button but wen btn1 selected and click in back you emit 5 , but btn5 is not exist ["m_currentBtn = btnGrp->buttons()->size() + 1;"] remove +1;

            I messed it up, because I copied it from the if-statement above...

            @mr-ZX said in Next/back buttons:

            and verry error ...
            for using Signal and slot you can write like

            My signals and slots are correct... I used the newer, Qt5 syntax.

            @mr-ZX said in Next/back buttons:

            in your class in Header (MainWindow.h ) you can write :
            private slots:
            void nextBtn();
            void prevBtn();
            void paintBtn(int id);
            signals:
            void buttonChanged(int);

            As I said before, I never had the intention to write complete and fully compilable program code with de- and constructors, headers and so on...


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            5
            • V Offline
              V Offline
              viniltc
              wrote on last edited by
              #6

              @Pl45m4 Many thanks for the code to point me in the right direction :)
              @jsulm @mr-ZX Thanks a lot for your feedback :)

              1 Reply Last reply
              2

              • Login

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