Congratulations to our 2022 Qt Champions!

How to create a thread that permanently gives back values

  • Hello all
    I have a function that processes some values and returns them in an array so i can write them into two fields in my UI.

    QVector<QString> WindowObject::function_a(QVector<QString> values_in){
        QVector<QString> values_out; 
        values_out[0] = modify(values_in[0]);
        values_out[1] = modify(values_in[1]);
        return values_out;

    in Main:

    QVector<QString> get_values;
    get_values = function_a(QV_values);

    Now I want to generate a thread that updates the two fields permanently whe I start the thread and stops updating when I stop the thread. I read about working threads but do not seem to understand it.
    Can anybody help me with that?


  • @Tomax63

    Hi and welcome todevnet forum

    Probably the easiest is to follow that guide and add an additional signal sending results as required.

    Are you already familiar with Qt's signal and slot scheme?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    How are you modifying this array ?
    How long does it take ?
    Where does the input come from ?

  • Hi,

    Look at Mandelbrot example. You only have to override the "run()" method. Also you can use a boolean variable to let the loop exit(and the thread will exit too).

  • @koahnig

    I have NO IDEA about slots and signals. I am new to C++ and Qt.

    I generated an object representing a window with two lineEdits.Now I want to start a thread that updates those 2 lineEdits with values that are retrieved from a hardware device via USB (the reading off the device already works, but only each time I start it by clicking a button).

    Now I want to start a thread that does the updating automatically.


  • @Tomax63

    Probably you need to post some of your code.

    When you are using a button and clicking on it is retrieving the information from a usb, you might already use a (signal-)slot functionality.

    When you have done it in a Qt way you are probably only required to substitute the button trigger with a QTimer pulling the information on a regular time interval. However,that is a sort of a wild guess based on my experience and the way I would do.

    Best is posting the declaration of your class and at the routine doing the pulling of information and display.

    Did you use one of Qt templates to start off?

Log in to reply