[PyKDE] ListBoxItems are invisible

Jeffrey Barish jeff_barish at earthlink.net
Tue Mar 29 23:08:42 BST 2005


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.

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)
        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




More information about the PyQt mailing list