user671110
user671110

Reputation:

Can QListWidget be refreshed after an addition?

I'm adding an item to a QListWidget, and although I can get the specifics of the item back from the QListQidget, the new item never appears on the screen. Is it possible to refresh the QListWidget to have it update to display newer contents?

>>>myQListWidget.addItem("Hello")
>>>print self.myQListWidget.item(0).text()
Hello

I'm doing this in Python, but if you have the solution in C++ I can easily convert it.

Thanks!

--Erin

Upvotes: 2

Views: 6206

Answers (3)

TocToc
TocToc

Reputation: 4989

I saw similar behavior as user671110 with PyQt5 (5.9.2). In my case using the suggestion from this qt forum worked.

Using the adapted sample code from jkerian:

import sys

from PyQt5 import QtWidgets

a = QtWidgets.QApplication(sys.argv)

w = QtWidgets.QListWidget()

w.setWindowTitle('example')

w.show()
w.addItem("test 1")                       # Item 1 does NOT show up
w.repaint()                               # Item 1 does NOT show up
QtCore.QCoreApplication.processEvents()
   # Item 1 DOES show up

w.addItem("test 2")       
                # Item 2 does NOT show up
QtCore.QCoreApplication.processEvents()
   # Item 2 DOES show up

w.addItem("test 3")       
                # Item 3 does NOT show up
a.exec_()                                 # All items shows up

This is kind of expected as the GUI is refreshed with event processing. Still THe doc mentioned that repaint() could be use to perform an asynchronous refresh but it does not seems to be working, or I use it wrongly.

Upvotes: 0

vitakot
vitakot

Reputation: 3844

You can update the widget's view by calling update() or repaint(), the second function is asynchronous and forces widget to update immediately. But the QListWidget should update automatically after insertion without calling any extra functions, if not, then the problem could be that Qt can't process the paint events. Then you have to call QCoreApplication::processEvents(), but I am not sure if it is your problem.

Upvotes: 2

jkerian
jkerian

Reputation: 17056

Hmm... I'm not seeing that behavior.

import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
a = QtGui.QApplication(sys.argv)
w = QtGui.QListWidget()
w.setWindowTitle('example')
w.show()
w.addItem("test 1")          # shows up
w.addItem("test 2")          # also shows up

EDIT: Removed chevrons so the code can be copied/pasted

Upvotes: 0

Related Questions