[PyKDE] ListBoxItems are invisible

John Ridley ojokimu at yahoo.co.uk
Wed Mar 30 20:11:51 BST 2005

--- Jeffrey Barish <jeff_barish at earthlink.net> wrote:
> I hope that someone can explain something to me.  In the following
> code
> sample, I expect to see 3 lines of text in a listbox.  When I
> uncomment
> the line that is commented out and comment out the 2 lines above it,
> I
> get the expected result.  With the code as shown, I get a listbox,
> but
> no text is visible in it.  In both case, however, the print loop
> confirms that there are 3 lines of text and the text is what was set.
> So why don't I see anything with the code as shown?  Is there a way
> to
> make the text visible when I put it in a QListBoxItem?  When working
> with a QListViewItem, it works to create a QListViewItem and then set
> its text (in fact, one must because unlike QListBox.insertItem,
> QListView.insertItem does not accept a text argument).  Why does it
> work with views but not boxes?
> Incidentally, when I first wrote this sample, I had 
> self.insertItem(lbi, -1)
> after the two lines in question.  In that case, I wound up with 6
> lines
> of text, and the last 3 were all "some text 2".  What I believe I was
> supposed to learn is that the constructor for QListBoxItem connects
> the
> resulting lbi to the parent specified, so the insertItem was making
> redundant connections.  I'm not clear on why the lines of text were
> numbered 0, 1, 2, 2, 2, 2 (rather than 0, 0, 1, 1, 2, 2), but what I
> really want to know is what insertItem(lbi) is properly used for.  I
> suppose it must be for situations in which the lbi being inserted has
> a
> different parent, but I can't imagine such a situation.

QListBox.insertItem allows you to insert items which have no parent
(see below), or to re-insert items which have been removed using

> import sys
> from qt import *
> class MainWindow(QMainWindow):
>     def __init__(self, *args):
>         QMainWindow.__init__(self, *args)
>         self.mlb = MyListBox(self)
>         self.setCentralWidget(self.mlb)
> class MyListBox(QListBox):
>     def __init__(self, parent):
>         QListBox.__init__(self, parent)
>         for i in range(3):
>             lbi = QListBoxItem(self)
>             lbi.setText("some text %d" % i)
> ##            self.insertItem("some text %d" % i, -1)

Replace the above loop with the one below and things should work

        for i in range(3):
            lbi = QListBoxText("some text %d" % i)

Note that QListBoxItem is an abstract base class designed for the
creation of custom listbox items, so I have used QListBoxText instead.
This class can also be given a parent listbox in the constructor,
making the above insertItem call redundant:

        for i in range(3):
            lbi = QListBoxText(self, "some text %d" % i)

Hope this is of some help to you!

John Ridley

>         for i in range(self.count()):
>             print "text at position", i, self.text(i)
> def main(args):
>     app = QApplication(args)
>     win = MainWindow()
>     app.setMainWidget(win)
>     win.show()
>     app.connect(app, SIGNAL("lastWindowClosed()"), app,
> SLOT("quit()"))
>     app.exec_loop()
> if __name__ == "__main__":
>     main(sys.argv)
> -- 
> Jeffrey Barish
> _______________________________________________
> PyKDE mailing list    PyKDE at mats.imk.fraunhofer.de
> http://mats.imk.fraunhofer.de/mailman/listinfo/pykde

Send instant messages to your online friends http://uk.messenger.yahoo.com 

More information about the PyQt mailing list