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. Update a Qt SLOT
QtWS25 Last Chance

Update a Qt SLOT

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 4 Posters 3.8k Views
  • 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.
  • RajniR Offline
    RajniR Offline
    Rajni
    wrote on last edited by A Former User
    #1

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

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

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

      1 Reply Last reply
      1
      • RajniR Offline
        RajniR Offline
        Rajni
        wrote on last edited by
        #3

        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();
        }
        1 Reply Last reply
        0
        • jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          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).
          Does

          qDebug()<<"flag"<<flag;
          

          print something? You can place a break point there to see whether it is called.

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

          1 Reply Last reply
          0
          • jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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.

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

            1 Reply Last reply
            0
            • RajniR Offline
              RajniR Offline
              Rajni
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • RajniR Offline
                RajniR Offline
                Rajni
                wrote on last edited by
                #7

                and I have to transmit that updated value of flag in serial port so do i have also write on on_FuelspinBox_valueChanged(int) .

                1 Reply Last reply
                0
                • RajniR Offline
                  RajniR Offline
                  Rajni
                  wrote on last edited by
                  #8

                  please help me......

                  1 Reply Last reply
                  0
                  • jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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?

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

                    1 Reply Last reply
                    0
                    • RajniR Offline
                      RajniR Offline
                      Rajni
                      wrote on last edited by
                      #10

                      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()));

                      }

                      1 Reply Last reply
                      0
                      • E Offline
                        E Offline
                        euchkatzl
                        wrote on last edited by euchkatzl
                        #11

                        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#singleShot

                        as 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()));
                        }
                        
                        1 Reply Last reply
                        2
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          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.

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - 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