[PyKDE] Generating dropdown list dynamically

Jeffrey Barish jeff_barish at earthlink.net
Mon Apr 11 05:07:54 BST 2005


I would like to generate a readonly dropdown list dynamically.  I catch
the focusInEvent, which gets triggered when I click on the down arrow
of the box.  At this point, I would generate the contents of the
listbox and use QListBoxText to enter it.  However, when I select one
of the items in the dropdown list, I get another focusInEvent.  It's
critical that I not get a second focusInEvent after the listbox has
been created as I don't want to create the list a second time.  I tried
setting the focus proxy on the combobox to None, figuring that the
second focusInEvent was coming from the listbox.  I tried setting the
focus policy to NoFocus (on both the the combobox and the listbox).  I
tried setting the focus policy of the combobox to NoFocus after
receiving the first focusInEvent and then turning it on again after the
selection of the item.  I tried setting the focus policy of the main
window to NoFocus.  Nothing I do suppresses the second focusInEvent. 
I'm apparently barking up the wrong tree.  

Remember that I'm the troglodyte using version 2.3.2 (because that's all
that's available for the Zaurus).  But at least I found the
documentation for this version at Trolltech.

import sys
from qt import *

class MainWindow(QMainWindow):
    def __init__(self, *args):
        QMainWindow.__init__(self, *args)
        self.setFocusPolicy(QWidget.NoFocus)

        self.cb = MyComboBox(self)
        for i in range(3): self.cb.insertItem("item %d" % i)

        QObject.connect(self.cb, SIGNAL('activated(int)'), self.clicked)
        QObject.connect(self.cb, PYSIGNAL('focusInEvent'),
                        self.focusEvent)

    def clicked(self, index):
        print 'Received click on index =', index, 'with text:',
              self.cb.text(index)
        self.cb.setFocusPolicy(QWidget.ClickFocus)

    def focusEvent(self):
        print 'Received focusInEvent'

class MyComboBox(QComboBox):
    def __init__(self, parent):
        QComboBox.__init__(self, parent)
        self.setFocusPolicy(QWidget.ClickFocus)
        self.setFocusProxy(None)
        self.lb = self.listBox()
        self.lb.setFocusPolicy(QWidget.NoFocus)
        self.lb.setFocusProxy(None)

    def focusInEvent(self, event):
        print 'in focusInEvent'
        self.setFocusPolicy(QWidget.NoFocus)
        self.emit(PYSIGNAL('focusInEvent'), ())

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