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. Bind Timer with a bool variable.
Forum Updated to NodeBB v4.3 + New Features

Bind Timer with a bool variable.

Scheduled Pinned Locked Moved Unsolved General and Desktop
36 Posts 6 Posters 4.1k 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.
  • T thomasGl

    Hi, how do i connect that button to the slots onclicked();?

    #include "twindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QMainWindow* wind = new QMainWindow();
        wind->resize(500,500);
    
    
        tWindow*z=new tWindow;
    
        //create Button
        QPushButton* butt = new QPushButton(wind);  
        butt->setText("click");
    
    
        QLabel* labb = new QLabel(wind);
        labb->setText("hello World");
        labb->setGeometry(200,200,200,200);
    
        //QObject::connect(button,SIGNAL(clicked(bool)),&a,SLOT(quit()));
    
        QObject::connect(butt,SIGNAL(clicked(bool)),tWindow,SLOT(onClicked())); //<-----this 
    
    
    
    #include "twindow.h"
    
    tWindow::tWindow(QWidget *parent):QMainWindow(parent)
    {
    
    
    }
    
    tWindow::~tWindow()
    {
    
    }
    
    void tWindow::onClicked()    //<----to this slot.
    {
        qDebug()<<"HELLO";
    }
    
    
    
    
    
    
    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #17

    @thomasGl said in Bind Timer with a bool variable.:

    QObject::connect(butt,SIGNAL(clicked(bool)),tWindow,SLOT(onClicked())); //<-----this

    Please do yourself a favor and only use New Signal Slot Syntax. "Old style" with SIGNAL/SLOT() macros is like a decade out of date nowadays!

    QObject::connect(butt, &QPushButton::clicked, tWindow, &tWindow::onClicked);
    

    It is then optional whether you make your tWindow::onClicked() method take the bool parameter --- if you don't care about the "checked" state you don't have to bother. A slot may have fewer parameters than the signal, but never more.

    Having said this: your error is that tWindow is your class (shame you start it with a lowercase letter, very confusing), your instance is z so you will actually want

    QObject::connect(butt, &QPushButton::clicked, z, &tWindow::onClicked);
    
    T 1 Reply Last reply
    1
    • JonBJ JonB

      @thomasGl said in Bind Timer with a bool variable.:

      QObject::connect(butt,SIGNAL(clicked(bool)),tWindow,SLOT(onClicked())); //<-----this

      Please do yourself a favor and only use New Signal Slot Syntax. "Old style" with SIGNAL/SLOT() macros is like a decade out of date nowadays!

      QObject::connect(butt, &QPushButton::clicked, tWindow, &tWindow::onClicked);
      

      It is then optional whether you make your tWindow::onClicked() method take the bool parameter --- if you don't care about the "checked" state you don't have to bother. A slot may have fewer parameters than the signal, but never more.

      Having said this: your error is that tWindow is your class (shame you start it with a lowercase letter, very confusing), your instance is z so you will actually want

      QObject::connect(butt, &QPushButton::clicked, z, &tWindow::onClicked);
      
      T Offline
      T Offline
      thomasGl
      wrote on last edited by
      #18

      i understand thx.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        thomasGl
        wrote on last edited by
        #19

        Because of Q_Object i am getting an error: undefined reference to vtable for lablos and error: Id returned 1 exit status. How can i fix that. I need Q_Object.

        #ifndef LABLOS_H
        #define LABLOS_H
        
        #include <QLabel>
        
        class lablos:public QLabel
        {
            Q_OBJECT      //<----because of Q_Object i am getting an error..
        
        
        public:
            lablos(QWidget* parent);
        
        
        };
        
        #endif // LABLOS_H
        
        
        Pl45m4P 1 Reply Last reply
        0
        • T thomasGl

          Because of Q_Object i am getting an error: undefined reference to vtable for lablos and error: Id returned 1 exit status. How can i fix that. I need Q_Object.

          #ifndef LABLOS_H
          #define LABLOS_H
          
          #include <QLabel>
          
          class lablos:public QLabel
          {
              Q_OBJECT      //<----because of Q_Object i am getting an error..
          
          
          public:
              lablos(QWidget* parent);
          
          
          };
          
          #endif // LABLOS_H
          
          
          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #20

          @thomasGl

          Re-run qmake / cmake after you added the Q_OBJECT macro


          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
          2
          • T Offline
            T Offline
            thomasGl
            wrote on last edited by
            #21

            @Pl45m4 qmake / cmake? what do u mean?

            Pl45m4P 1 Reply Last reply
            0
            • T thomasGl

              @Pl45m4 qmake / cmake? what do u mean?

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

              @thomasGl

              You are using probably one of them. It's the build tool that "Makes" your code and builds your app / executable from your source code.

              Especially when you add the QObject macro to some classes, you have to re-run it, to ensure that everything is updated. Sometimes your IDE (like QtCreator) notices, sometimes it doesn't.


              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
              4
              • T Offline
                T Offline
                thomasGl
                wrote on last edited by
                #23

                Thank you it works.

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  thomasGl
                  wrote on last edited by
                  #24

                  Hi, i stuck again. I am trying to show my label after 5 clicks on my Pushbutton else label should hide. How can i count 1 click after 1 click? My problem is the loop i have only managed to get 5 counts with no click. I need 1 count with 1 click on the button. I tried to put QObject::connect(but, &Button::clicked, but,&Button::count); in my loop instead of "but->count" but it does not work. Do i need a mousepressevent here?

                  void Button::count()              //thats my method i have connected with my Button
                  {
                      counter++;
                       qDebug()<<counter;
                  }
                  
                  
                  
                  QObject::connect(but, &Button::clicked, but,&Button::count);    //Connection
                  
                  
                  
                      while (but->counter!=5)   //my loop
                      {
                          if(but->counter==5)
                          {
                              labb->show();
                          }
                          else
                          {
                             but->count();    //here i want to click and count
                          }
                      }
                  
                  
                  
                  
                  Pl45m4P 1 Reply Last reply
                  0
                  • T thomasGl

                    Hi, i stuck again. I am trying to show my label after 5 clicks on my Pushbutton else label should hide. How can i count 1 click after 1 click? My problem is the loop i have only managed to get 5 counts with no click. I need 1 count with 1 click on the button. I tried to put QObject::connect(but, &Button::clicked, but,&Button::count); in my loop instead of "but->count" but it does not work. Do i need a mousepressevent here?

                    void Button::count()              //thats my method i have connected with my Button
                    {
                        counter++;
                         qDebug()<<counter;
                    }
                    
                    
                    
                    QObject::connect(but, &Button::clicked, but,&Button::count);    //Connection
                    
                    
                    
                        while (but->counter!=5)   //my loop
                        {
                            if(but->counter==5)
                            {
                                labb->show();
                            }
                            else
                            {
                               but->count();    //here i want to click and count
                            }
                        }
                    
                    
                    
                    
                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by
                    #25

                    @thomasGl

                    The while loop blocks your counting on click. You dont need it.

                    Add something like

                    QObject::connect(but, &Button::clicked, this ,&MainWindow::onCount);
                    
                    
                    MainWindow::onCount{
                    if(but->counter==5)
                            {
                                labb->show();
                            }
                    
                    }
                    

                    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
                    2
                    • T Offline
                      T Offline
                      thomasGl
                      wrote on last edited by
                      #26

                      Hey. I how can i make that label to show my counter?

                      void mainWin::onCount()
                      {
                          if(counter==4)
                          {
                              labb->show();
                              labb->setText("Hello World");
                          }
                      
                      
                          counter++;
                          
                          qDebug()<<counter;    //i want that counter to be shown with that label2
                          
                          label2->setText(counter);  //i want that label to show my  counter
                      
                      
                      
                      }
                      
                      jsulmJ JonBJ 2 Replies Last reply
                      0
                      • T thomasGl

                        Hey. I how can i make that label to show my counter?

                        void mainWin::onCount()
                        {
                            if(counter==4)
                            {
                                labb->show();
                                labb->setText("Hello World");
                            }
                        
                        
                            counter++;
                            
                            qDebug()<<counter;    //i want that counter to be shown with that label2
                            
                            label2->setText(counter);  //i want that label to show my  counter
                        
                        
                        
                        }
                        
                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #27

                        @thomasGl said in Bind Timer with a bool variable.:

                        //i want that label to show my counter

                        Then convert it to a string first using https://doc.qt.io/qt-6/qstring.html#number ...

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

                        1 Reply Last reply
                        0
                        • T thomasGl

                          Hey. I how can i make that label to show my counter?

                          void mainWin::onCount()
                          {
                              if(counter==4)
                              {
                                  labb->show();
                                  labb->setText("Hello World");
                              }
                          
                          
                              counter++;
                              
                              qDebug()<<counter;    //i want that counter to be shown with that label2
                              
                              label2->setText(counter);  //i want that label to show my  counter
                          
                          
                          
                          }
                          
                          JonBJ Online
                          JonBJ Online
                          JonB
                          wrote on last edited by
                          #28

                          @thomasGl
                          As you learn you need to read the documentation for yourself, not ask at every stage. QLabel::setText() accepts a QString as its argument. You have a number in counter. So look through/search the QString Class documentation page for a suitable method to convert a number to string....

                          EDIT
                          It seems @jsulm has already given this away. Shame! :( It's better you learn how to do programming than just ask and be told....

                          jsulmJ 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @thomasGl
                            As you learn you need to read the documentation for yourself, not ask at every stage. QLabel::setText() accepts a QString as its argument. You have a number in counter. So look through/search the QString Class documentation page for a suitable method to convert a number to string....

                            EDIT
                            It seems @jsulm has already given this away. Shame! :( It's better you learn how to do programming than just ask and be told....

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

                            @JonB Agree. I really wonder why so many people ask questions which can be easily answered by simply looking at documentation...

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

                            1 Reply Last reply
                            1
                            • T Offline
                              T Offline
                              thomasGl
                              wrote on last edited by
                              #30

                              Hi guys. I dont understand why the button is sometimes not clickable while moving. So i am starting the Timer with one button click and after that the button is moving but sometime its unclickable.

                              void mainWindow::timerStart()   //timer is starting
                              {
                                  if(counter==0)
                                  {
                                      sTimer->start(1000);
                              
                              void MainWindow::buttonMove()  //Pushbutton random position
                              {
                                  int x = rand() % 230;
                                  int y = rand() % 245;
                                  but->setGeometry(x, y, 50, 70);
                              }
                              
                              
                              QObject::connect(buttton,&Button::clicked,this,&MainWindow::timerStart);  //starts the timer
                              
                              
                              QObject::connect(sTimer,&timer::timeout,this,&MainWindow::buttonMove); //every second the button is moving but i cant always click it because its unclickable i dont understand why? its a Pushbutton 
                              
                              jsulmJ 1 Reply Last reply
                              0
                              • T thomasGl

                                Hi guys. I dont understand why the button is sometimes not clickable while moving. So i am starting the Timer with one button click and after that the button is moving but sometime its unclickable.

                                void mainWindow::timerStart()   //timer is starting
                                {
                                    if(counter==0)
                                    {
                                        sTimer->start(1000);
                                
                                void MainWindow::buttonMove()  //Pushbutton random position
                                {
                                    int x = rand() % 230;
                                    int y = rand() % 245;
                                    but->setGeometry(x, y, 50, 70);
                                }
                                
                                
                                QObject::connect(buttton,&Button::clicked,this,&MainWindow::timerStart);  //starts the timer
                                
                                
                                QObject::connect(sTimer,&timer::timeout,this,&MainWindow::buttonMove); //every second the button is moving but i cant always click it because its unclickable i dont understand why? its a Pushbutton 
                                
                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #31

                                @thomasGl Not possible to say with the code you posted. You can create a minimal compilable application which shows this issue and share it, so others can take a look.

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

                                1 Reply Last reply
                                0
                                • T Offline
                                  T Offline
                                  thomasGl
                                  wrote on last edited by
                                  #32

                                  how can i do that?

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • T thomasGl

                                    how can i do that?

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

                                    @thomasGl What exactly?

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

                                    1 Reply Last reply
                                    0
                                    • T Offline
                                      T Offline
                                      thomasGl
                                      wrote on last edited by thomasGl
                                      #34

                                      creating a minimal compilable application and post it. Do you mean my full code?

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • T Offline
                                        T Offline
                                        thomasGl
                                        wrote on last edited by
                                        #35

                                        Ok my fault it works now. I need make more timer in my code.

                                        1 Reply Last reply
                                        0
                                        • T thomasGl

                                          creating a minimal compilable application and post it. Do you mean my full code?

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

                                          @thomasGl said in Bind Timer with a bool variable.:

                                          Do you mean my full code?

                                          No, just a MINIMAL compilable application which shows the problem...

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

                                          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