How to every time check variable value?



  • Hello Everyone,

    How i can check every time a value of variable so that if it crosses a threshold Qcheckbox should be checked.
    For example
    IF a function is running and I want to start check the variable value which is not in that function and if that variable value reaches a certain threshold Qcheckbox should be checked.
    Thanks in advance.


  • Moderators

    @rockon209

    Without a couple of more details there is little to answer besides the obvious (some if statements and an update shall do).



  • @koahnig

    For example
    IF a function is running and I want to start check the variable value which is not in that function and if that variable value reaches a certain threshold Qcheckbox should be checked.
    I know if statement should be used but how I know or check that the variable value is changed?



  • @rockon209 That is still a super generic question.

    QCheckBox *cBox = new QCheckBox();
    cBox->show();
    
    for(int i =0 i < 100000; i++) {
         if(i == 100){
              cBox->setChecked(true);
              break;
         }
    }
    

    thats the most generic answer I could think of!?

    Edit:
    Rereading your 2nd post I think you may want something along this?
    A QSpinBox that when a certain value is reached a checkbox ist checked?

    //Constructor
    connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(checkValue(int));
    
    //Check function
    void checkValue(int value){
        if( value == 100)
           ui->checkBox->setChecked(true);
        else 
          ui->checkBox->setChecked(false);
    }
    

  • Moderators

    @rockon209 Do you want to know whether the variable was changed or do you want to know whether that variable reached a threshold? It is not really clear from your description. And: do you want to check that variable once in the function or in a loop?



  • @jsulm
    I want to check if the variable is changed or not. But the varaible will be updated somewhere else not in the function
    Letme explain in detail
    if in an application there is a start button. I pressed the start button and some tasks are going on. When these tasks are going on, with a temperature sensor which am using i got a signal and this signal means that i want to stop the application how can i instruct the application to stop? the temp sensor is connected to PC via USB and I can communicate with the sensor


  • Moderators

    @rockon209 I still don't understand the problem. To stop call http://doc.qt.io/qt-5/qcoreapplication.html#quit...


  • Moderators

    @rockon209 said in How to every time check variable value?:

    @koahnig

    For example
    IF a function is running and I want to start check the variable value which is not in that function and if that variable value reaches a certain threshold Qcheckbox should be checked.
    I know if statement should be used but how I know or check that the variable value is changed?

    But there is exactly the point. One can suggest many different things which are not going to suitable to what you are actually looking for.

    Some suggestions are when you are having changed something in another QObject derived class (@J-Hilk). That might be already sufficient. When you have something completely different other means are required. For instance you may required a QTimer which triggers to check your values on a regular basis.



  • @koahnig

    Yes I know that with Qtimer i can check the value on regular basis. But is there any other way intead of Qtimer because I have used lot of Qtimers in my Application.


  • Moderators

    @rockon209 Can't you just emit a signal each time this variable is changed? Then everyone interested to this variable can connect to this signal and do what ever needs to be done. This way there is no need to check actively at all...



  • @J-Hilk @koahnig @jsulm
    I will explain again
    I pressed the start button, and I start recording some values from other sensors. And suddenly the temp value is 100 degree so therefore I want to stop the application. So when I am recording the values from other sensor I should everytime keep an eye on the temp to check the value. How can i do it. One way i know is use Qtimmer and every one seconnd check the value of the Temp sensor. Is there any other way instead of this?


  • Moderators

    This post is deleted!

  • Moderators

    @rockon209 If you don't get the temperature already on a regular basis then you need to check it periodically using a QTimer. Alternative would be to have a thread where you read the temperature sleep for some time and read again in a loop. But using a QTimer is way easier!



  • @jsulm
    if I want to use Qthread how i can do it. I havnt used thread till now so dont know how to used it. I read about it but not able to understand.


  • Moderators

    @rockon209 Why not just use a QTimer for that?
    If you really want to use multithreading for that then you should read:
    https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/
    http://doc.qt.io/qt-5/thread-basics.html



  • @jsulm
    I dont have only one sensors but many sensors are there to measure differnt data then i think it doesnt make any sense to create different timers everytime to get the data from it. Thats why i thought to use thread.


  • Qt Champions 2016

    @rockon209
    There would be nothing to stop from reusing a timer. If you fear you will get too many.
    If your design can work with 1 thread, then it can also work with one timer.

    How many sensors are we talking about?

    We have a system running where it uses QTimer. I think we use around 60 most of the time and have seen
    no issue so far.



  • @mrjj
    i am using around 10 sensors so may be i will need around 8 timers to get the data from them. I dont know which one is the optimum solution. Thats why i started the topic. But if you say that 60 timers are also working fine then i think i will go for timers



  • @rockon209 how exactly to do communicate with your sensors? QIODevice, a file that is written, serial port, bluetooth, vendor specific library etc. Maby theres a prefabricated way/Signal that you can use as a notifier for new data available.


  • Moderators

    @rockon209 How many timers you need depends on how you read all these sensors. If you read all of them at the same frequency then one timer is enough.



  • @J-Hilk
    I am using different NI lib functions to communicate with these sensors.

    @jsulm
    I want to read the data from these sensors at different time and sometimes simultaneously. Also they have different frequencies.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.