dongzhe tai
dongzhe tai

Reputation: 1

how to update QTableWidget item?

There is a thread running! it receives data from internet two times per second, then I set the data in qtablewidget?

How can I make the QTableWidget update itself ?
Now I must click the interface if I want to update the display!

DWORD WINAPI MyThreadProc1(LPVOID lpParameter){
    int data=receive();
    w.setVal(data);
    return 0;
}


void TradeSystem::setValue(int num){
    QTableWidgetItem *item = new     QTableWidgetItem(QString::number(num,10,1));
    item->setBackgroundColor(QColor(0,60,10));
    ui.tableWidget_3->item(0,0)->setText(QString::number(num,10,0));
}

Upvotes: 0

Views: 1606

Answers (2)

saeed hardan
saeed hardan

Reputation: 485

You should get used to signals in QT , its easy to use and can make your life easier .

DWORD WINAPI MyThreadProc1(LPVOID lpParameter){
    int data=receive();
    emit data_recieved(data);
    return 0;
}
void TradeSystem::setValue(int num){
    QTableWidgetItem *item = new QTableWidgetItem(QString::number(num,10,1));
    item->setBackgroundColor(QColor(0,60,10));
    ui.tableWidget_3->item(0,0)->setText(QString::number(num,10,0));
}

and before you activate MyThreadProc1 , connect the signal and handler :

connect(this,SLOT(setValue(int)),MyThreadProc1,SIGNAL(data_recieved(int)));

that way , you can connect basically every widget in qt with a signal/slot .
either in different Form,Threads .
This is helpful also Docs.

Upvotes: 0

Zaiborg
Zaiborg

Reputation: 2522

i guess this problem falls in the category 'i want to change gui from another thread' -> don't do it.

when setValue(int) is a slot, you can do the following:

DWORD WINAPI MyThreadProc1(LPVOID lpParameter)
{
    (void)lpParameter;
    int data = receive();
    QMetaObject::invokeMethod(w, "setValue", Q_ARG(int, data));
}

Upvotes: 1

Related Questions