New Thread to add new tab when menu item is clicked



  • Hello there,

    I want to use threads to create tabs in my qt application. Currently, I have a main menu, in which when an item is clicked a new tab is added.
    The code I am using to create a tab is as follows:
    void Home::on_actionItem_triggered()
    {
    ui->tabWidget->addTab(new tabScreen(), "tabtitle");
    }

    Can I use QFuture<void> future = QtConcurrent::run(&pool, aFunction) for the above addTab function or there might be any other way of creating a thread for the same?

    Thanks,
    Vidushi


  • Qt Champions 2016

    Hi
    All widgets must live in the main thread. So you cannot run a tab in QtConcurrent.
    You would normally run a calculation in another thread and "report back" to the widgets/GUI via signals.

    Perhaps you can describe what you are trying to build?
    Its unclear to me how adding tabs from threads could benefit an application so what is the effect
    you hope to obtain from this? :)



  • The problem is that I have a grid in a tab that is taking time to create. As a result, when a user opens that tab containing the grid, the entire screen freezes and the user is incapable of performing operations on other tabs in the meantime. We would like to give users the benefit of working on the other tabs while the grid is being is loaded in a separate tab.

    In my above mentioned code, tabScreen is the grid class with its own tabscreen.h and tabscreen.cpp files which is independent from other code.

    Can you please help me achieve this functionailty?

    Thanks
    Vidushi


  • Moderators

    @vsukhwal As @mrjj said it is not possible to use Qt GUI classes in non main threads.
    Why does it takes so long to create those tabs?
    Do you perform any heavy calculations?
    If so then move those calculations to other threads and pass the results to the UI via signals/slots.



  • Most of the time if taken by the http request sending large amount of data. As a result, until the data is not received, the grid cannot be created and updated



  • @vsukhwal said in New Thread to add new tab when menu item is clicked:

    Most of the time if taken by the http request sending large amount of data. As a result, until the data is not received, the grid cannot be created and updated

    Well that is nice and easy then. Put the data loading/socket communication in a separate thread and when the data is ready update the gui tab.


Log in to reply
 

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