Results 1 to 4 of 4

Thread: QT - Strange layout problem with QStackedWidget

  1. #1
    Join Date
    Jun 2007
    Location
    Manchester, UK
    Beans
    174
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    QT - Strange layout problem with QStackedWidget

    Hi. I'm having a strange problem using QStackedWidget.

    I'm trying to create a window with a QGLWidget and QStackedWidget next to one-another. The stacked widget should be of fixed size and the glwidget should expand to fill all the remaining available space.

    What actually happens is that the glwidget takes up the minimum width, the stacked widget remains fixed in size and the rest of the space is empty. I get different results depending on whether I ask the stacked widget to be fixed size or its contents but I can't get it to act the way I'd like.

    I've posted a simplified bit of code, the glwidget hasn't been inherited so it will look strange if you run it but it illustrates the problem i'm having/. If anyone can illuminate me as to what I'm doing wrong it would be a great help.

    Thanks,

    James

    Code:
    #include <QHBoxLayout>
    #include <QGLWidget>
    #include <QPushButton>
    #include <QStackedWidget>
    #include <QMainWindow>
    #include <QApplication>
    #include <QString>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        // create main window
        QMainWindow *mainwindow = new QMainWindow() ;
    
        // create main widget
        QWidget *mainwidget = new QWidget() ;
    
        // create gl widget
        QGLWidget *glwidget = new QGLWidget() ;
        glwidget->setMinimumWidth(200) ;
    
        // create button
        QPushButton *button = new QPushButton("Button") ;
        button->setFixedWidth(200) ;
    
        // create stacked widget and add button
        QStackedWidget *stack = new QStackedWidget() ;
        stack->addWidget(button) ;
    
        // create layout
        QHBoxLayout *mainlayout = new QHBoxLayout() ;
        mainlayout->setMargin(5) ;
        mainlayout->addWidget(glwidget) ;
        mainlayout->addWidget(stack) ;
        mainwidget->setLayout(mainlayout) ;
        mainwindow->setCentralWidget(mainwidget) ;
        mainwindow->show();
        return a.exec();
    }
    "We have nothing to fear but fear itself. And Chuck Norris." - President Roosevelt

  2. #2
    Join Date
    Jun 2007
    Location
    Manchester, UK
    Beans
    174
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: QT - Strange layout problem with QStackedWidget

    The same sort of thing seems to happen if I replace the QGLWidget with say a QPushButton so I suspect I'm doing something fairly obviously wrong. So any ideas would be useful.

    Thanks again. James
    "We have nothing to fear but fear itself. And Chuck Norris." - President Roosevelt

  3. #3
    Join Date
    Feb 2006
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: QT - Strange layout problem with QStackedWidget

    Well, this would be easy if you'd use Qt Creator / Qt Designer to build GUI.

    Anyway, I think you need to tell the glwidget to expand (with setSizePolicy()). This is just a guess but I think it should be something like this (the first parameter is horizontal and second vertical):
    glwidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding)

    edit. setSizePolicy() is inherited from QWidget
    Vera! Vera! What has become of you?

  4. #4
    Join Date
    Jun 2007
    Location
    Manchester, UK
    Beans
    174
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: QT - Strange layout problem with QStackedWidget

    Thanks, that worked, I knew it must be something simple.
    "We have nothing to fear but fear itself. And Chuck Norris." - President Roosevelt

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •