[PyKDE] Trouble refreshing tabbed table (example code included)

kgi iacovou at gmail.com
Tue Jun 7 16:46:02 BST 2005


I want to create a tabbed widget containing tables. It all works fine
when doing it all from within the constructor of my
QMainWindow-derived class (that is, before I call mainWin.show() and
app.exec_loop()), but I can't get it working dynamically. I've
stripped down the whole problem into a code snippet below.

I'm on Linux, X.Org 6.8.2, Qt 3.3.4, Python 2.3.5, PyQt 3.14.1.

As a last resort, I played around with Qt's Designer and looked at the
output of pyuic. It produced almost, but not quite, the same order of
code as my original attempt (more on that later). After I imported
some of the pyuic-isms into my own code, I ended up with this (which
almost works):

[code start]

import sys
import qt
import qttable

class MyMainWidget ( qt.QMainWindow ):
    
    def __init__ ( self ):

        super ( MyMainWidget, self ).__init__ ( None, 'example using menus' )
        menu = qt.QPopupMenu ( self )
        menu.insertItem ( "Tab", self.addTab )
        self.menuBar().insertItem ( "Add", menu )

        self.tables    = []
        self.count     = 0
        self.cw        = qt.QWidget ( self, "central widget" )
        self.setCentralWidget ( self.cw )
        self.layout    = qt.QVBoxLayout ( self.cw )
        self.tabWidget = qt.QTabWidget ( self.cw, "tabWidget")
        self.layout.addWidget ( self.tabWidget )


    def addTab ( self ):
        tab_name = "tab %d" % ( self.count )
        table_name = "table %s" % ( self.count )
        tab = qt.QWidget ( self.tabWidget, tab_name )
        tabLayout = qt.QVBoxLayout ( tab )
        table = qttable.QTable ( tab, table_name )
        table.setNumRows ( 3 )
        table.setNumCols ( 3 )
        tabLayout.addWidget ( table )
        self.tabWidget.insertTab ( tab, table_name )
        # table.show()
        # tab.show()
        self.tables.append ( table )
        self.count += 1


if __name__ == "__main__":
    app  = qt.QApplication ( sys.argv )
    mainWin = MyMainWidget()
    app.setMainWidget ( mainWin )
    mainWin.show()
    app.exec_loop()


[code end]


Run it and select "Add->Tab" several times. What is doesn't do is draw
the first tabbed widget properly; subsequent ones are OK, and clicking
between tabs works fine. If I uncomment the "table.show()" and
"tab.show()" lines, then the table gets drawn correctly, but it
doesn't honour resizing properly.

I'm also not sure I quite understand why pyuic generates those
QWidgets, either. Am I right in thinking that tabs can't contain
layout objects; they must contain QWidgets. So we create a QWidget
whose parent is the tab widget, create a layout object whose parent is
the QWidget we just created, insert a table into the layout object,
and insert the whole thing into the tab?

Cheers,

Ricky




More information about the PyQt mailing list