[PyKDE] Using QScrollView to manage a changing list of widge

Jim Bublitz jbublitz at nwinternet.com
Tue Feb 18 06:12:00 GMT 2003


On 18-Feb-03 Jonathan Gardner wrote:
> I'm working on some software for a client here, and I've been> 
> thinking of several ways to handle a situation that has come up
> more than once.
 
> A single patient can have several insurance companies they are
> subscribed to.  Not just the insurances that are current, but
> previous insurances as well. Most common is only one, but
> having two or three is very common.
 
> One idea I came up with was using QScrollView.  It would have
> only one child -- a QVBox. This is similar to the first method
> of using it mentioned in the Qt documentation.
 
> That QVBox would have the list of patient insurance widgets.The
> patient insurance list needs to be able to grow and shrink (as
> I add or remove insurance companies) or disappear altogether
> and be repopulated (when I switch what patient I am looking at).
 
> I implemented something that just lists QLabels rather than
> patient insurance widgets.
 
> One more note: The insurance list is just one tab among many on
> the main widget.
 
> class Insurance_Tab(QWidget):
 
>     def __init__(self, parent=None, name=None, fl=0):
>         QWidget.__init__(self, parent, name, fl)
 
>         lo = QVBoxLayout(self,0,6)
 
>         self.sv = QScrollView(self, "Insurance_Tab_ScrollView")
>         self.sv.setHScrollBarMode(QScrollView.AlwaysOff)
 
>         self.vbox = QVBox(self.sv.viewport())
>         self.sv.addChild(self.vbox)
 
>         lo.addWidget(self.sv)
 
>     def set_patient(self, patient):
>         self.sv.removeChild(self.vbox)
>         del self.vbox
>         self.vbox = QVBox(self.sv.viewport())
 
>         if patient:
>             sql = """
>                 SELECT object_id
>                 FROM c_patient_insurance
>                 WHERE patient=%d
>             """ % patient.object_id
>             for i in db.query(sql).dictresult():
>                 print i
>                 QLabel(str(i['object_id']), self.vbox)
 
> Anyways, it works fine as long as I am looking at a different tab
> as I change the patient. If I am looking at it, it clears, but
> won't show what has been added.
 
> Any pointers would be great. Thanks in advance.
 
After just taking a quick look, there's a couple of things I'd try:
first, you might want to try calling the show () methods for the
QLabels or QVBox, or otherwise forcing a redraw (show () is usually
the best way). Second, you might want to look at QWidgetStack, which
is designed for adding/removing widgets (there's an example in the
Qt docs). I've had some flakiness (but nothing related to an
example like yours) with removeChild, but could just be my code too.


Jim




More information about the PyQt mailing list