Update a Qt SLOT
-
Hello guys , I am new to Qt.
Actually ,I am trying to show some different number of images based upon the value in spinbox .i had assign a spinbox value to variable and connect to aslot whenever the value changed in spinbox. i got the update value in slot but not in other function. what to do for that?
how to assign a spinbox values to variable , update it everytime whenever the value changed at runtime and transmit that value to serial port.
Please help me out.
Thankyou in advance.. -
So, you already have a slot connected to a spinbox signal (I guess valueChanged(int i)), right?
Then in this slot you can assign the new value which is passed to the slot to a variable in your class which you then can use anywhere in your class:void MyClass::spinboxChanged(int newValue) { value = newValue; }
For searial port see http://doc.qt.io/qt-5/qserialport.html
-
ThankYou Jsulm, for your quick reply.
I am doing the same as you suggested but it doesn't work.
At run time when i am changing the value of spinbox it will not showing any changes in value .```
as shown below in code flag is a variable where i want to store the value of qspinbox and switch that value in function fuelbar() to show some images.//your code here
#include "widget.h" #include "ui_widget.h" #include<QtTest/QTest> #include<QDebug> #include<QSerialPortInfo> int flag ; Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); connect(ui->FuelspinBox, SIGNAL(valueChanged(int)),this, SLOT(on_FuelspinBox_valueChanged(int))); fuelbar(); } void Widget:: fuelbar() { ui->bar2->setVisible(false); ui->bar3->setVisible(false); ui->bar4->setVisible(false); ui->bar5->setVisible(false); ui->bar6->setVisible(false); ui->bar7->setVisible(false); ui->bar8->setVisible(false); ui->bar9->setVisible(false); ui->bar10->setVisible(false); // qDebug()<<"flag in fuelbar"<<flag; switch (flag) { case 0: ui->bar1->setVisible(true); ui->led->setVisible(true); ui->FuelIcon->setVisible(true); // lit=false; timer1s->start(1000); connect(timer1s,SIGNAL(timeout()),this,SLOT(barOFF())); break; case 1: ui->bar1->setVisible(true); ui->bar2->setVisible(true); ui->led->setVisible(false); ui->FuelIcon->setVisible(true); timer1s->start(1000); connect(timer1s,SIGNAL(timeout()),this,SLOT(bar2OFF())); break; case 2: ui->bar1->setVisible(true); ui->bar2->setVisible(true); ui->bar3->setVisible(true); ui->led->setVisible(false); break; case 3: ui->bar1->setVisible(true); ui->bar2->setVisible(true); ui->bar3->setVisible(true); ui->bar4->setVisible(true); ui->led->setVisible(false); break; case 4: ui->bar1->setVisible(true); ui->bar2->setVisible(true); ui->bar3->setVisible(true); ui->bar4->setVisible(true); ui->bar5->setVisible(true); ui->led->setVisible(false); break; case 5: ui->bar1->setVisible(true); ui->bar2->setVisible(true); ui->bar3->setVisible(true); ui->bar4->setVisible(true); ui->bar5->setVisible(true); ui->bar6->setVisible(true); ui->led->setVisible(false); break; case 6: ui->bar1->setVisible(true); ui->bar2->setVisible(true); ui->bar3->setVisible(true); ui->bar4->setVisible(true); ui->bar5->setVisible(true); ui->bar6->setVisible(true); ui->bar7->setVisible(true); ui->led->setVisible(false); break; case 7: ui->bar1->setVisible(true); ui->bar2->setVisible(true); ui->bar3->setVisible(true); ui->bar4->setVisible(true); ui->bar5->setVisible(true); ui->bar6->setVisible(true); ui->bar7->setVisible(true); ui->bar8->setVisible(true); ui->led->setVisible(false); break; case 8: ui->bar1->setVisible(true); ui->bar2->setVisible(true); ui->bar3->setVisible(true); ui->bar4->setVisible(true); ui->bar5->setVisible(true); ui->bar6->setVisible(true); ui->bar7->setVisible(true); ui->bar8->setVisible(true); ui->bar9->setVisible(true); ui->led->setVisible(false); break; case 9: ui->bar1->setVisible(true); ui->bar2->setVisible(true); ui->bar3->setVisible(true); ui->bar4->setVisible(true); ui->bar5->setVisible(true); ui->bar6->setVisible(true); ui->bar7->setVisible(true); ui->bar8->setVisible(true); ui->bar9->setVisible(true); ui->bar10->setVisible(true); ui->led->setVisible(false); break; } } void Widget::on_FuelspinBox_valueChanged(int arg1) { flag=arg1; // flag=ui->FuelspinBox->value(); qDebug()<<"flag"<<flag; update(); }
-
Make sure your connect was successful:
qDebug() << connect(ui->FuelspinBox, SIGNAL(valueChanged(int)),this, SLOT(on_FuelspinBox_valueChanged(int)));
this should print true.
Is on_FuelspinBox_valueChanged(int) declared as slot?
Another thing to check: in on_FuelspinBox_valueChanged(int) you do not call fuelbar()! So a new flag value will not change anything! You have to call fuelbar() in on_FuelspinBox_valueChanged(int).
DoesqDebug()<<"flag"<<flag;
print something? You can place a break point there to see whether it is called.
-
One more thing: your flag is not initialized! It could contain any number. You should assign it a value. And it would be better to put it in your Widget class as private property and initialize it in constructor.
-
Thanks again Jsulm ..:)
yes I have put on_FuelspinBox_valueChanged(int) in SLOT.
when I call fuelbar() in on_FuelspinBox_valueChanged(int) flag value changes and it shows the respective images but in case when flag value is 0 and 1 where i have to toggle image and i am using 1 sec timer for that is misbehaving it blinks for all the cases.
-
What do you mean by "that is misbehaving it blinks for all the cases"?
One thing I see is: you connect the timeout() signal each time case 0 or 1 is executed!
You should do it once (preferably in the constructor).
What does barOFF() do? -
I want to to toggle the image if flag is 0 and 1, but if i am calling fuelbar() in on_FuelspinBox_valueChanged(int) then the both image are toggling for all the case either the flag is value is 2 to 9.I have include the code below may be this will help you to understand .
Please give me some solution how can i handle it.
Thankyou..:)void Widget:: barOFF()
{ui->bar1->setVisible(false); ui->led->setVisible(false); ui->FuelIcon->setVisible(false); timer1s->stop(); QTest::qWait(500); timer1s->start(500); connect(timer1s,SIGNAL(timeout()),this,SLOT(barON()));
}
void Widget:: barON()
{
;
ui->bar1->setVisible(true);
ui->led->setVisible(true);
ui->FuelIcon->setVisible(true);
timer1s->stop();
QTest::qWait(500);
timer1s->start(500);
connect(timer1s,SIGNAL(timeout()),this,SLOT(barOFF()));}
void Widget:: bar2OFF()
{
ui->bar1->setVisible(false);
ui->bar2->setVisible(false);
ui->led->setVisible(false);
ui->FuelIcon->setVisible(false);
timer1s->stop();
QTest::qWait(500);
timer1s->start(500);
connect(timer1s,SIGNAL(timeout()),this,SLOT(bar2ON()));}
void Widget:: bar2ON()
{
ui->bar1->setVisible(true);
ui->bar2->setVisible(true);
ui->led->setVisible(false);
ui->FuelIcon->setVisible(true);
timer1s->stop();
QTest::qWait(500);
timer1s->start(500);
connect(timer1s,SIGNAL(timeout()),this,SLOT(bar2OFF()));}
-
as @jsulm said :
put these lines :
connect(timer1s,SIGNAL(timeout()),this,SLOT(bar2OFF())); connect(timer1s,SIGNAL(timeout()),this,SLOT(barON()));
in your constructor because you should do that only once !!! When you call connect multiple times then your slot get called as often as your code executes the lines above !
For your approach is ist better not to use a single Timer because you have to call different slots !
Take a look at QTimer single shot function ! That ist a static function so you dont need a QTimer object :
http://doc.qt.io/qt-5/qtimer.html#singleShotas a example for your code
void Widget:: barOFF() { ui->bar1->setVisible(false); ui->led->setVisible(false); ui->FuelIcon->setVisible(false); // timer1s->stop(); --> not needed anymore QTest::qWait(500); // timer1s->start(500); //connect(timer1s,SIGNAL(timeout()),this,SLOT(barON())); QTimer::singleShot(500,this,SLOT(barON())); }
-
Hi,
Important side note: Don't ever use QTest in production code. Like the name of the module suggests, it's only to be used for writing unit tests.