Bind Timer with a bool variable.
-
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(); }
-
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(); }
@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? -
@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... -
@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...@Christian-Ehrlicher Listen, at least it is initialized... ;-)
-
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"; }
-
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"; }
connect(butt, &QPushButton::clicked, z, &tWindow::onClicked);
Please read https://doc.qt.io/qt-6/signalsandslots.html
-
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"; }
@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 thebool
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 isz
so you will actually wantQObject::connect(butt, &QPushButton::clicked, z, &tWindow::onClicked);
-
@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 thebool
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 isz
so you will actually wantQObject::connect(butt, &QPushButton::clicked, z, &tWindow::onClicked);
-
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
-
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
-
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.
-
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 } }
-
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 } }
-
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 }
-
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 }
@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 ...