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.0k 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

    Hello. How can i bind my Timer with that bool variable. I want to hide my Button when the bool variable is false else show().

    void Button::ButtonHide()
    {
        QTimer* time;
        
        time->start(2000);
        
        bool buttonTime=false;
        
        if(buttonTime==false)
        {
            this->hide();
        }
        else
        {
            this->show();
        }
        
    }
    
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #3

    @thomasGl
    In addition to @Christian-Ehrlicher.

    Since there is QWidget::visible() and QWidget::setVisible(bool visible), which does the same as show/hide(), if you want you do not need to keep a boolean state variable (visible() already provides this). To toggle between shown and hidden your slot method or lambda for the QTimer can just do

    this->setVisible(!this->visible());
    

    E.g. make the QTimer a class member variable and

        connect(&timer, &QTimer::timeout, [this]() { this->setVisible(!this->visible()); });
        timer.start(2000);
    
    1 Reply Last reply
    2
    • T Offline
      T Offline
      thomasGl
      wrote on last edited by
      #4

      Ok thx. Yes i want to hide and show it periodically.

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

        Now i have another Problem. I want to increase the counter, when i click the button. Is there a way to display the counter? Because qDebug does not work.

        int Button::ButtonclickedXtimes()
        {
          count=0;
          
        
          if(this->clicked());  //does not work i need something like that.
          {
              count+=1;
              qDebug()<<count;
          }
        
        
        }
        
        JonBJ 1 Reply Last reply
        0
        • T thomasGl

          Now i have another Problem. I want to increase the counter, when i click the button. Is there a way to display the counter? Because qDebug does not work.

          int Button::ButtonclickedXtimes()
          {
            count=0;
            
          
            if(this->clicked());  //does not work i need something like that.
            {
                count+=1;
                qDebug()<<count;
            }
          
          
          }
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #6

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

          Is there a way to display the counter? Because qDebug does not work.

          qDebug() does work, and qDebug()<<count; does work. Where it displays the output is a different matter, and depends on platform.

          Depending on your needs to "display" this counter, you might use qDebug(), printf/fprintf(stderr or put it on a UI element such as a QLabel.

          1 Reply Last reply
          0
          • SalemSabrehagenS Offline
            SalemSabrehagenS Offline
            SalemSabrehagen
            wrote on last edited by
            #7

            Did you #include <QDebug> ?
            qDebug() messages should be visible in Qt Creater in the "output" tab
            or external in a terminal if you set the hook "Run in terminal" in your project options.

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

              Ok, thx i forgot to include <QDebug>

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

                Hey Guys. Now i am trying to hide my label when my counter ist 5. My counter increases when i click the Button. I made a function where "this->hide(); <----"this" is my label. I am calling that function inside onother function in my Buttonclass, but the program crashes.

                QObject::connect(this,SIGNAL(clicked(bool)),this,SLOT(ButtonclickedXtimes()));  //i am connecting the Button with the function for counting
                
                void Button::ButtonclickedXtimes()   //slot function 
                {
                    Label* l;
                
                   count++;
                   qDebug()<<count;
                
                   if(count==5)
                   {
                       l->labelhide();   //here i am calling the function for the label to hide.
                   }
                
                
                }
                /*****************Label Class********************/
                
                Label::Label(QWidget *parent):QLabel(parent)
                {
                    this->setGeometry(200,200,200,200);       
                    this->setText("Hello World");        //i want that text to hide when counter is 5
                
                    
                 void Label::labelhide()    //thats my label hide function
                {
                    this->hide();
                }
                
                
                Christian EhrlicherC 1 Reply Last reply
                0
                • T thomasGl

                  Hey Guys. Now i am trying to hide my label when my counter ist 5. My counter increases when i click the Button. I made a function where "this->hide(); <----"this" is my label. I am calling that function inside onother function in my Buttonclass, but the program crashes.

                  QObject::connect(this,SIGNAL(clicked(bool)),this,SLOT(ButtonclickedXtimes()));  //i am connecting the Button with the function for counting
                  
                  void Button::ButtonclickedXtimes()   //slot function 
                  {
                      Label* l;
                  
                     count++;
                     qDebug()<<count;
                  
                     if(count==5)
                     {
                         l->labelhide();   //here i am calling the function for the label to hide.
                     }
                  
                  
                  }
                  /*****************Label Class********************/
                  
                  Label::Label(QWidget *parent):QLabel(parent)
                  {
                      this->setGeometry(200,200,200,200);       
                      this->setText("Hello World");        //i want that text to hide when counter is 5
                  
                      
                   void Label::labelhide()    //thats my label hide function
                  {
                      this->hide();
                  }
                  
                  
                  Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

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

                  but the program crashes.

                      Label* l;
                  
                     count++;
                     qDebug()<<count;
                  
                     if(count==5)
                     {
                         l->labelhide(); 
                  

                  I would be surprised if it would not crash.
                  The same as in your first post - you're using an unitialized pointer so what do you expect?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

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

                    i initialized it with NULL , it crashes after 5 .

                    Christian EhrlicherC 1 Reply Last reply
                    0
                    • T thomasGl

                      i initialized it with NULL , it crashes after 5 .

                      Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

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

                      i initialized it with NULL , it crashes after 5 .

                      Are you kidding me?
                      It must be a valid pointer to your label you want to show or hide...

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      JonBJ 1 Reply Last reply
                      2
                      • Christian EhrlicherC Christian Ehrlicher

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

                        i initialized it with NULL , it crashes after 5 .

                        Are you kidding me?
                        It must be a valid pointer to your label you want to show or hide...

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

                        @Christian-Ehrlicher Listen, at least it is initialized... ;-)

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

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

                          ;-)

                          xD i got this thx guys.

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

                            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";
                            }
                            
                            
                            
                            
                            
                            
                            jsulmJ JonBJ 2 Replies Last reply
                            0
                            • 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";
                              }
                              
                              
                              
                              
                              
                              
                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #16

                              @thomasGl

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

                              Please read https://doc.qt.io/qt-6/signalsandslots.html

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

                              1 Reply Last reply
                              1
                              • 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 Offline
                                JonBJ Offline
                                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

                                          • Login

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