Reputation: 55
I have a simple class based on QTreeWidget. In some cases (when value one of columns updated), I need to repaint it. I have a function which invokes when I need to update my widget:
void TreeWidget::updated()
{
/* some actions with cells */
/* here need to repaint widget */
this->update();
/* also I'm tried this->repaint(); */
}
But line this->update();
(or this->repaint();
) gave no results. Widget repaint only when I click on it.
So how can I repaint my widget?
Upvotes: 4
Views: 1172
Reputation: 243897
The classes that inherit from QAbstractScrollArea
as QTreeWidget
have viewport()
which is the widget that must be updated, so in your case the solution is:
viewport()->update();
If you want to call update from another thread you can use QMetaObject::invokeMethod():
QMetaObject::invokeMethod(viewport(), "update", Qt::QueuedConnection)
Upvotes: 5
Reputation: 55
I learned one interesting thing. As it turned out, you can update widgets in Qt only from the main thread. My function updated()
was called by another thread, so this->update()
did not work. However, all slots in Qt are executed just in the main thread, wherever they are called from. In this case, the correct solution would be to wrap this->update()
inside the slot. Like this:
TreeWidget::TreeWidget()
{
/* ... */
connect(this, SIGNAL(signal_update()), this, SLOT(slot_update()));
/* ... */
}
void TreeWidget::updated()
{
/* some actions with cells */
emit signal_update();
}
void TreeWidget::slot_update()
{
this->update();
}
Yeah, it's a less beautiful solution than this->viewport()->update()
but more correct.
Upvotes: 0