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. Set QWidget default width
Forum Updated to NodeBB v4.3 + New Features

Set QWidget default width

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 1.4k Views 2 Watching
  • 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.
  • B Offline
    B Offline
    Black Imp
    wrote on 20 Jan 2020, 18:09 last edited by Black Imp
    #1

    I need to set the initial width of a qwidget, which should be expandible horizontally, being inserted in a QSplitter, inside the parent widget constructor.
    I've tried to use rightPanel->resize(rightPanel->sizeHint()); but nothing changes
    I need to set a specific smaller widht.
    I find no way to do this. When parent widget shows the widget i'm intereset in is has a huge width much larger than its content.
    This is a problem I 've encountered in many different and more general situations NOT strictly related to qsplitter.

    ParentMdiSubWindow::ParentMdiSubWindow(): QMdiSubWindow()
    {
        ...
        //
        QSplitter *midSplitter = new QSplitter(Qt::Horizontal);
        midSplitter->setHandleWidth(1);
        midSplitter->setChildrenCollapsible(false);
    
        // plot on the left
        leftPanel = new QWidget();
        leftPanelLayout = new QVBoxLayout();
        leftPanel->setLayout(leftPanelLayout);
        leftPanelLayout->setSpacing(0);
        leftPanelLayout->setContentsMargins(0,0,0,0);
       
    
        // right panel
        rightPanel = new QWidget();
        rightPanelLayout = new QVBoxLayout();
        rightPanelLayout->setContentsMargins(0,0,0,0);
        rightPanelLayout->setSpacing(0);
        rightPanel->setLayout(rightPanelLayout);
        rightPanel->layout()->setAlignment(Qt::AlignCenter);
    
        // add support widget to subwindow
        setWidget(midSplitter);
        ...
    }
    

    right panel issue.png

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Jan 2020, 19:35 last edited by
      #2

      Hi,

      What about using QSplitter::setSizes ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      B 1 Reply Last reply 21 Jan 2020, 00:28
      2
      • S SGaist
        20 Jan 2020, 19:35

        Hi,

        What about using QSplitter::setSizes ?

        B Offline
        B Offline
        Black Imp
        wrote on 21 Jan 2020, 00:28 last edited by Black Imp
        #3

        @SGaist thank you but I don't know the main widget size - a qdmisubwindow in this case - and I need the plot on the left to simply expand as much as it can. only the right panel should have an initial precise width. it's a situation very common which can be managed in many platform and GUI I've used so far. A simple command for setting a specific size thou not fixed would be very helpful

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 21 Jan 2020, 20:30 last edited by
          #4

          You can also use setStretchFactor.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          B 1 Reply Last reply 22 Jan 2020, 16:42
          0
          • S SGaist
            21 Jan 2020, 20:30

            You can also use setStretchFactor.

            B Offline
            B Offline
            Black Imp
            wrote on 22 Jan 2020, 16:42 last edited by
            #5

            As workaround I tried, but when I maximize the QMdiSubWindow the stretch factor is different. I'd need at least the ratio is kept but it's not.

            J 1 Reply Last reply 22 Jan 2020, 16:56
            0
            • B Black Imp
              22 Jan 2020, 16:42

              As workaround I tried, but when I maximize the QMdiSubWindow the stretch factor is different. I'd need at least the ratio is kept but it's not.

              J Offline
              J Offline
              JonB
              wrote on 22 Jan 2020, 16:56 last edited by JonB
              #6

              @Black-Imp
              This is purely thrown in as thought for you: given you say it does not work inside parent widget constructor, could you set it in whatever "show" event instead when it's first shown and maybe your code will work there? It may be quite unrelated to your situation, but I have had problems resizing when containers & children have not yet been made visible.

              B 1 Reply Last reply 22 Jan 2020, 21:10
              1
              • J JonB
                22 Jan 2020, 16:56

                @Black-Imp
                This is purely thrown in as thought for you: given you say it does not work inside parent widget constructor, could you set it in whatever "show" event instead when it's first shown and maybe your code will work there? It may be quite unrelated to your situation, but I have had problems resizing when containers & children have not yet been made visible.

                B Offline
                B Offline
                Black Imp
                wrote on 22 Jan 2020, 21:10 last edited by Black Imp
                #7

                @JonB Thank you that's something I was searching for. It works. Unfortunately everytime that system calls qmdisubsindow's showEvent, size is applied again. I should intercept every call for minimizing, maximizing, tiling etc. for saving the current size of right panel and setting it again in showEvent.

                for those who may be interested in:

                void ParentMdiSubWindow::showEvent(QShowEvent * event)
                {
                    int w = midSplitter->width();
                    // screeSize is taken from primary screen in constructor
                    int labelPanelSize = screenSize.width() / 192 * 18; 
                    int width = this->width();
                    if (labelPanelSize >= width)
                    { labelPanelSize = width / 3; }
                
                    QList<int> list;
                    list << w - labelPanelSize;
                    list << labelPanelSize;
                    midSplitter->setSizes(list);
                
                }
                
                1 Reply Last reply
                1
                • B Offline
                  B Offline
                  Black Imp
                  wrote on 23 Jan 2020, 14:22 last edited by Black Imp
                  #8

                  ok I came up with the complete solution:

                  instead of overriding showEvent i override resizeEvent which is called before the showEvent.

                  This whole code lets me hold the right panel width manually set by handle when resizing, maximizing, minimizing parent window, starting with a width based on primary screen

                  class ParentMdiSubWindow: public QMdiSubWindow
                  {
                     //...
                  
                     protected slots:
                        void splitterHandleMoved(int pos, int index);
                     private:
                        bool inited; // initialized false in constructor
                        int labelPanelWidth; // initialized 0 in constructor 
                        QSplitter * midSplitter;
                  }
                  
                  ParentMdiSubWindow::ParentMdiSubWindow(QWidget *parent, Qt::WindowFlags flags):QMdiSubWindow(parent, flags),
                  inited(false),
                  labelPanelWidth(0)
                  {
                     midSplitter = new QSplitter();
                     midSplitter->setHandleWidth(1);
                     connect(midSplitter, SIGNAL(splitterMoved(int, int)), this, SLOT(splitterHandleMoved(int, int)));
                  }
                  
                  void ParentMdiSubWindow::resizeEvent(QResizeEvent * resizeEvent)
                  {   
                      int width = this->width();
                      int w = midSplitter->width();
                      if(!inited)
                      {
                          labelPanelWidth = screenSize.width() / 192 * 18;
                  
                          if (labelPanelWidth >= width)
                          { labelPanelWidth = width / 3; }
                      }
                      QList<int> list;
                      list << w - labelPanelWidth;
                      list << labelPanelWidth;
                      midSplitter->setSizes(list);
                      inited = true;
                      QMdiSubWindow::resizeEvent(resizeEvent);
                  }
                  
                  void ParentMdiSubWindow::splitterHandleMoved(int pos, int index)
                  {
                      labelPanelWidth = midSplitter->width() - pos - 1;
                  }
                  
                  1 Reply Last reply
                  1

                  1/8

                  20 Jan 2020, 18:09

                  • Login

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