[PyQt] What am I doing wrong with QWidget?

David Boddie david at boddie.org.uk
Sat Sep 19 22:09:52 BST 2009


On Sat Sep 19 19:27:06 BST 2009, Kurt Schwarz wrote:

> I'm having problems displaying a widget as a child inside a window. The
> code below should create a window that is 500 x 500 with a red box inside
> 400 x 400.
>
> If I replace self.ChildWidget = AppWidget( self ) with self.ChildWidget =
> QtGui.QWidget( self ) I get the desired result, what am I doing wrong with
> AppWidget( ) that would cause it not to display correctly?

I don't think you're doing anything wrong. I quickly rewrote the example in
C++ and verified that it works.


#include <QtGui>

class AppWidget : public QWidget
{
public:
    AppWidget(QWidget *parent = 0) : QWidget(parent) {}
};

class AppWindow : public QMainWindow
{
public:
    AppWindow() : QMainWindow() {
        setGeometry( 30, 30, 500, 500 );

        ChildWidget = new AppWidget(this);
        ChildWidget->setGeometry( 10, 10, 400, 400 );
        ChildWidget->setStyleSheet( "background-color: #ff0000" );
    }

private:
    QWidget *ChildWidget;
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    AppWindow MainWindow;
    MainWindow.show( );
    return app.exec( );
}


In your Python version, if you change the base class of AppWidget to QFrame,
it works, so there's definitely something strange happening.

David


More information about the PyQt mailing list