Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Fixed-size widget in a layout
QtWS25 Last Chance

Fixed-size widget in a layout

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 15.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 2 Offline
    2 Offline
    2r273gd
    wrote on 14 Jan 2014, 22:37 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 15 Jan 2014, 03:29 last edited by
      #2

      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.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kpks
        wrote on 15 Jan 2014, 09:19 last edited by
        #3

        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?

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dheerendra
          Qt Champions 2022
          wrote on 15 Jan 2014, 09:32 last edited by
          #4

          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.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • 2 Offline
            2 Offline
            2r273gd
            wrote on 15 Jan 2014, 09:38 last edited by
            #5

            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?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kpks
              wrote on 16 Jan 2014, 07:26 last edited by
              #6

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

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jeroentjehome
                wrote on 16 Jan 2014, 08:02 last edited by
                #7

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

                Greetz, Jeroen

                1 Reply Last reply
                0

                4/7

                15 Jan 2014, 09:32

                • Login

                • Login or register to search.
                4 out of 7
                • First post
                  4/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved