Nominate our 2022 Qt Champions!

How does threads access ui component

  • Hi

    My question is more on C++ language.

    I used one main window and one threads for my application in Freescale imx6.
    in the main window, i can use ui->setbutton(), to control the ui.successfully.

    I am not able to used ui in my threads, since threads class does not inherit ui.
    what should i do

  • Moderators

    I do not understand your question, so I will just post the general rule in Qt: do not modify or create Ui elements from a thread. Qt does not support that and it can and will fail in many ways.

  • In addition to sierdzio here is "an explanation": why you should not access UI elements directly from a non-GUI thread.

    To do modifications you can emit a signal from a thread and react on the signal in UI thread.
    Take a look on "QThread description":

  • If so, how do i call a dialog boxes from the thread?
    my thread is used to prompt user information dialog boxes when it receives signals from another terminal
    By using signals and slots? please post an example.

    since, the background is prompting user information dialog boxes, i need the dialog boxes to disappear like 5 second later, since it is only for information, it should not require user input. I require the dialog boxes to blink or the working to blink/ showing different color to attract attendant attention.

    Can someone post an example pls?

  • The link is very good.
    But how to show a user informative dialog boxes that disappear/invisible after 5 sec.

    After i compile and put my target into imx6, i saw my dialog boxes. when i touched the panel, it is like erasing the dialog boxes. Exposing background GUI. Why?

    do i need tslib for touchscreen? and what is the solution?

  • You cannot access GUI elements directly from other threads. You have to send signals to the main thread. For example, the class for your main window my provide slots like these:

    public slots:
    void showMessage(QString msg);
    void enableStartButton();
    void disableStartButton();

    The other threads can send signals. You connect the signals to these slots. When the other thread wants to display a message on screen, then it emits the signal that is connected to the showMessage() slot passing over the text as argument.

    So the background threads send a signal to the main thread saying "please display that message for me". And the main thread displays the message.

    If you think about a yes/no dialog box, then the main thread can send the selection (yes or no) back to the background thread again via signals.

    Be aware that you cannot pass GUI objects between threads, only simple objects like QString, QByteArray or more complicated self-created objects that dont contain GUI elements.

  • bq. But how to show a user informative dialog boxes that disappear/invisible after 5 sec.

    create a pair of signal/slot that take the message text and time as arguments:

    public slots:
    void displayMessage(QString message, int seconds);

    The main class then displays the message and starts a timer that removes the message after the time elapses.

    Or you send one signal to display the text and later another signal to hide the text.

Log in to reply