Qt simple count down
-
Good evening
Im developing a project for my Senior class and in my GUI at the moment im trying to create a simple countdown but I cant quite get it right. When I press start it increments the number by 1 value and stop does 1 decrement. I tried a simple for loop in the function but I get errors in my connect area.
Here I have it in the mainwindow.cpp above my other codes for different functions
this->connect(this->ui->pushButton_2, SIGNAL(released()), this,SLOT(button_Start_click())); this->connect(this->ui->pushButton_8, SIGNAL(released()), this,SLOT(button_Stop_click())); this->counter=10; this->ui->lcdNumber->display(counter);
These are the buttons
void Race::button_Start_click() { counter--; this->ui->lcdNumber->display(counter); } void Race::button_Stop_click() { terminate(); break; this->ui->lcdNumber->display(counter); }
-
Good evening
Im developing a project for my Senior class and in my GUI at the moment im trying to create a simple countdown but I cant quite get it right. When I press start it increments the number by 1 value and stop does 1 decrement. I tried a simple for loop in the function but I get errors in my connect area.
Here I have it in the mainwindow.cpp above my other codes for different functions
this->connect(this->ui->pushButton_2, SIGNAL(released()), this,SLOT(button_Start_click())); this->connect(this->ui->pushButton_8, SIGNAL(released()), this,SLOT(button_Stop_click())); this->counter=10; this->ui->lcdNumber->display(counter);
These are the buttons
void Race::button_Start_click() { counter--; this->ui->lcdNumber->display(counter); } void Race::button_Stop_click() { terminate(); break; this->ui->lcdNumber->display(counter); }
Hi,
first, you don't need to write
this->
in front of every expression.
Like// here this->connect // here this->counter=10; // or here this->ui->lcdNumber->display(counter);
Without
this
everywhere the code looks a little cleaner.
You only needthis
when addressing "this" instance, like in the connect statement, where you want to connect to the current class and object.
And when dealing with two identical variables... one local, the other a member of "this" class (in a setter function, for example)
Then it's:void setCounter(int counter) { this->counter = counter; }
Left-hand side your member, right-hand side the local variable that you pass in.
Without thethis
there, they have to have different names, or it won't work.break;
What should the
break;
do there?
What isterminate()
doing?You said
start_click()
should increment the counter, but in there you are doingcounter--;
?
And for what are you planning to use a loop?
If you want to continuously increase or decrease the counter without clicking, you need a timer (QTimer
)
Please explain what exactly you are trying to achieve.