[Qt-interest] QScrollArea needs a new widget to update?

qt-interest at status.e4ward.com qt-interest at status.e4ward.com
Mon Jul 27 02:28:03 CEST 2009


Hello all.

	I have a list of QLabels, containing IP address and client names. The  
number of QLabels in this list is unknown, as is the amount of text in  
the labels. So, I put these labels inside the layout (say it's called  
m_layout) of a widget (say it's called m_widget), which was then given  
to a QScrollArea (say it's m_scrollArea) via setWidget(). This allowed  
the list to grow both in length and in width and scroll bars were  
added as needed. The data used for the labels is also kept in a QMap  
(say it's m_labels) allowing access to them elsewhere inside the  
program. The QMap is split into addresses as the key, and names as the  
value. While this works alright, I know I can do it better. See, the  
QLabels in the layout change dynamically every few seconds (depending  
on who's online), and the list needs to be updated correspondingly.  
The basics of what I currently have is the following:

	For each new address-name pair:
		Check to see if a label with the same address is already in  
m_labels. If so, it's already in the layout as well.
			Remove it from m_layout.
			Remove it from m_labels.
		Create a new QLabel (however is needed) and show() it.
		Add the new QLabel to m_labels.
		Add the new QLabel to m_layout.
	End

	Finally do m_scrollArea->setWidget(m_widget).

I know... not a very efficient method– it was mainly a proof-of- 
concept, and pretty hacked together. Every time a single label needs  
to change, the entire list is re-rendered. Now that I've got things  
working, I want to fix this. Is there any way to have a QScrollArea  
(or something with a similar concept) while just using individual  
QLabels? I would like to update each QLabel as-needed, rather than the  
whole list. Ideally, the basics would be something like:

	For each new address-name pair:
		Check to see is a label with the same address already exists.
		If so:
			Update label that already exists
		If not:
			Create a new label and show it (preferably in the correct position  
of a sorted-by-address list)
	End

If someone can help me do something like that and still maintain a  
scroll area around where these labels are being rendered, I would be  
forever grateful!

	Thank you,

		Kyle Fazzari



More information about the Qt-interest-old mailing list