Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Update All Controls every 200ms



  • Hey, guys :D

    I was trying to search/get some hints to solve my problem, but what i've found was hard to me since i'm really new with Qt.

    So, what's my problem?
    I have a main window with 5 checkboxes. The thing is: checkbox methods are called just if i change the state of the checkbox. But i don't want it. In every checkbox i have a check like:

    if (ui->checkBox->isChecked())
    {
       Do X;
    }
    
    else
    {
        Do Y;
    }
    

    So i want my program calling the methods for checkboxes every 200ms, even considering i didn't touched it.

    I've read something about connect and QTimer, but i'm still confused. :(


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Out of curiosity, why exactly do you want to check their state like that ?

    In any case, just connect your QTimer to a slot that implements that logic and you're pretty much done.



  • Hi, SGaist :D

    I'm working with named pipes. The program in Qt is the server.

    if (ui->checkBox->isChecked())
    {
    ........ Make a call to write some infos in a named pipe;
    }

    else
    {
    ........ Make a call to write another infos in a named pipe;
    }

    In any case, just connect your QTimer to a slot that implements that logic and you're pretty much done.

    I was doing something like it inside MainWindow::MainWindow:

    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout, this, QOverload<>::of(&MyConditionCheckerHere));
    timer->start(1000);

    But it's not working, i'm a little confused...


  • Lifetime Qt Champion

    Sorry, I failed to see the relation between a named pipe and the fact that you are polling these widgets.

    How did you determine that the slot is not called ?



  • @SGaist said in Update All Controls every 200ms:

    Sorry, I failed to see the relation between a named pipe and the fact that you are polling these widgets.

    How did you determine that the slot is not called ?

    The thing i want is just make my program automatically check the state of the checkboxes EVERY 200ms. Just it. If checked, so write X to my pipe, if not checked, so write Y to my pipe.

    This is my problem, i don't know how to do it in a loop. :\


  • Lifetime Qt Champion

    You do not need a loop.

    QTimer is the right tool.

    What exactly is not working in your code ?



  • @SGaist said in Update All Controls every 200ms:

    You do not need a loop.

    QTimer is the right tool.

    What exactly is not working in your code ?

    I created a new checkbox now called Updater. This is my method for Updater:

    void MainWindow::on_checkBox_2_stateChanged(int)
    {
        QTimer *timer = new QTimer(this);
        connect(ui->checkBox, SIGNAL(clicked()), this, SLOT(on_checkBox_stateChanged()));
        timer->start(200);
    }
    

    My intention with the Updater checkbox is that it keeps calling MainWindow::on_checkBox_stateChanged(), because inside it i have the if conditions to write to my pipe with X or Y informations.

    The code from Updater isn't calling the another checkboxes methods.


  • Lifetime Qt Champion

    Your timer is not connected to anything.
    You create a new one every time that method is called.
    You duplicate the connection every time that method is called.

    Your logic is flawed.

    As I suggested before, write one slot into which you do all your checkbox value stuff that you dump into your pipe.

    Create only one timer, connect it to that slot and start it. That's all. If you want to toggle the timer from on to off based on one checkbox, then write a slot that you connect to the toggled signal of your checkbox and start/stop the timer based on the state of the checkbox.



  • @SGaist said in Update All Controls every 200ms:

    Your timer is not connected to anything.
    You create a new one every time that method is called.
    You duplicate the connection every time that method is called.

    Your logic is flawed.

    As I suggested before, write one slot into which you do all your checkbox value stuff that you dump into your pipe.

    Create only one timer, connect it to that slot and start it. That's all. If you want to toggle the timer from on to off based on one checkbox, then write a slot that you connect to the toggled signal of your checkbox and start/stop the timer based on the state of the checkbox.

    Thanks for all your help, friend!! =D
    I made it now with a tutorial about QTimer!

    #Solved


  • Lifetime Qt Champion

    You're welcome !

    Out of curiosity, why do you need to send the checkbox status every 200ms ?


Log in to reply