Fixed-size widget in a layout



  • Hi, I need to have a fixed-size widget in a layout and it isn't working that well. The simplified example would be:

    @#include <QApplication>
    #include <QMainWindow>
    #include <QHBoxLayout>
    #include <QTextEdit>
    #include <QPushButton>

    int main(int argc, char * * argv)
    {
    QApplication application(argc, argv);

    QMainWindow mainWindow;
    
    QWidget * const widget = new QWidget;
    mainWindow.setCentralWidget(widget);
    
    QHBoxLayout * const layout = new QHBoxLayout;
    layout->setMargin(0);
    layout->setSpacing(0);
    widget->setLayout(layout);
    
    QTextEdit * const textEdit = new QTextEdit;
    layout->addWidget(textEdit);
    
    QPushButton * const pushButton = new QPushButton("A");
    pushButton->setFixedSize(25, 25);
    layout->addWidget(pushButton);
    
    mainWindow.show();
    
    return application.exec&#40;&#41;;
    

    }@

    Which gives me some unexpected results:

    !http://i.imgur.com/OzpG6M0.png!

    Am I not supposed to put fixed-size widgets in layouts? Should I create my own class for every fixed-size widget I need and override sizeHint() method?

    I'd be grateful if someone could clarify that for me.



  • You can do it. In general use the sizePolicy of Layout. This will help you to do the required thing. Set the sizePolicy of QPushButton to setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed). This should work. You don't have to sub-class.



  • I have a similar problem, only difference is I am using qtcreator 2.8.1;

    I have 3 panels within the MainWindow, I want to set the relative size of the panels, the panel with start/quit button small, the status log button also small and the main instructions panel relatively large.

    How do I do that?



  • As guidline, you can use the stretch factor while adding the widgets to layout and use the sizepolicy to handle your case.

    If you can give your code snippet it would help. I can make it work and share you the piece of sample for you.



  • Unfortunately sizePolicy didn't do it. I added

    @pushButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);@

    to the previous code and the result is exactly the same.

    As I understand it, sizePolicy relates to sizeHint, so for it to work I would have to subclass the widget and override the sizeHint() method like I said before. Am I wrong?



  • I had a similar issue with the QDial; I simply set the min/max sizes to be the same in QtCreator 2.8.1;



  • Hi,
    Try this one:
    @
    minimumSizeHint : const QSize
    @
    QWidget docs -> also set sizepolicy to something else then ignore. That should do the trick ;-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.