简体   繁体   中英

QThread, creating GUI widget element on a thread

I've been trying that for a while and seems it's not something common as I didn't find much information about.

I have a QTree in which I put item, on my Item i have a check box on the first column.

Problem: Checkbox is not optimised to be treated as so and takes quite some time as soon as there is several elements.

So i'm using a thread to create the checkbox before putting in my list, but seems impossible to get the checkbox back on the GUI thread.

void CheckItemThread::run()
{
    setPriority(QThread::IdlePriority);
    QCheckBox     *m_check;
    m_check = new QCheckBox();
    emit done(m_check);
}

My main thread:

myCheckItem::myCheckItem(QTreeWidget *parent, QStringList columnNames ):
  myWidgetItem(parent)
{
  m_parent = parent;
  m_columnNames = columnNames;

    connect(&TheThread,SIGNAL(done(QCheckBox *)), this, SLOT(retThread(QCheckBox *)));
    connect(&TheThread,SIGNAL(terminated()), this, SLOT(endThread()));
    TheThread.setdata(columnNames,parent, this);
    TheThread.start();    //run thread
}
    void myCheckItem::endThread()
    {
        m_check->setParent(m_parent);
        connect(m_check, SIGNAL(stateChanged(int)), this, SLOT(onCheckBox(int)));
    }

void myCheckItem::retThread(QCheckBox *check)
{
    m_check = check;
}

Maybe I'm missing something or it's simple not possible to reattach the thread ?

Thanks

You must not create, edit or work with UI elements in other threads. UI elements must be worked with in the main thread (UI thread). If you have time-consuming prerequisites before "drawing" a checkbox, do your work in a thread (eventually QtConcurrent ) and send a signal to the main thread for creating the corresponding checkbox.

You can change GUI elements only in main thread. How many checkboxes do you have? Maybe you should create a limited count of checkboxes and reuse them when needed?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM