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. How to resize child QWidget when parent resizes?

How to resize child QWidget when parent resizes?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 6.3k Views 1 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.
  • W Offline
    W Offline
    Wodzu
    wrote on last edited by Wodzu
    #1

    Hi,

    I've created a new Qt Widgets Application. I have a centralWidget to which I would like to add another custom-painted widget. The problem is that the added widget paints only limited portion of its parent.

    Here is the code:

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>374</width>
        <height>269</height>
       </rect>
      </property>
      <property name="sizePolicy">
       <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
        <horstretch>0</horstretch>
        <verstretch>0</verstretch>
       </sizepolicy>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <property name="sizePolicy">
        <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
         <horstretch>0</horstretch>
         <verstretch>0</verstretch>
        </sizepolicy>
       </property>
      </widget>
      <widget class="QMenuBar" name="menuBar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>374</width>
         <height>21</height>
        </rect>
       </property>
      </widget>
      <widget class="QToolBar" name="mainToolBar">
       <attribute name="toolBarArea">
        <enum>TopToolBarArea</enum>
       </attribute>
       <attribute name="toolBarBreak">
        <bool>false</bool>
       </attribute>
      </widget>
      <widget class="QStatusBar" name="statusBar"/>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <resources/>
     <connections/>
    </ui>
    
    
    
    #include "mainwindow.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        return a.exec();
    }
    
    #include "worldarea.h"
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtWidgets>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {    
        ui->setupUi(this);
        this->worldArea = new WorldArea(centralWidget());
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    #include "worldarea.h"
    #include <QPainter>
    #include <QPaintEvent>
    #include <QDebug>
    
    WorldArea::WorldArea(QWidget *parent)
        : QWidget(parent)
    {
        this->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
    }
    
    QSize WorldArea::minimumSizeHint() const
    {
        return QSize(100, 100);
    }
    
    QSize WorldArea::sizeHint() const
    {
        return QSize(this->parentWidget()->width(), this->parentWidget()->height());
    }
    
    void WorldArea::paintEvent(QPaintEvent * /*event*/)
    {
        QPainter painter(this);
        painter.fillRect(0, 0, this->width(), this->height(), Qt::white);
    }
    

    I did a lot of research before posting this question but couldn't find a solution. I've tried to add a layout in design time to centralWidget but this did not help. Thank you for any suggestions.

    As above so below...

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Wodzu said:
      Hi Do you mean that WorldArea
      does not occupy the whole area of
      centralWidget ?
      A layout should have worked.
      Did u add layout to central and
      WorldArea to layout?

      mrjjM 1 Reply Last reply
      0
      • mrjjM mrjj

        @Wodzu said:
        Hi Do you mean that WorldArea
        does not occupy the whole area of
        centralWidget ?
        A layout should have worked.
        Did u add layout to central and
        WorldArea to layout?

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Could you try this small sample?
        https://www.dropbox.com/s/bw53dz4760h0bw1/allofme.zip?dl=0
        It is a layout in Designer and to that added a widget. ( custom draw one)
        take all space. (except the 9 pixels defined by the layout as margin)

        W 1 Reply Last reply
        1
        • mrjjM mrjj

          Could you try this small sample?
          https://www.dropbox.com/s/bw53dz4760h0bw1/allofme.zip?dl=0
          It is a layout in Designer and to that added a widget. ( custom draw one)
          take all space. (except the 9 pixels defined by the layout as margin)

          W Offline
          W Offline
          Wodzu
          wrote on last edited by
          #4

          @mrjj
          Yes, exacly the WorldArea is not occupying the whole area of centralWidget.

          Thank you so much for preparing an example for me! It works.
          Could you be so kind and check for errors in my example? The thing is, that I would like learn how to do this at runtime.

          I've tried such code:

          MainWindow::MainWindow(QWidget *parent) :
              QMainWindow(parent),
              ui(new Ui::MainWindow)
          {    
              ui->setupUi(this);
              this->worldArea = new WorldArea(centralWidget());
              // Why this does not work??
              QVBoxLayout *layout = new QVBoxLayout;
              layout->addWidget(worldArea);
              this->setLayout(layout);
          }
          

          Here is my project: https://www.dropbox.com/s/vffprhosx396mr4/WorldPainterProblem.7z?dl=0

          Once again, thank you for your help.

          As above so below...

          mrjjM 1 Reply Last reply
          0
          • W Wodzu

            @mrjj
            Yes, exacly the WorldArea is not occupying the whole area of centralWidget.

            Thank you so much for preparing an example for me! It works.
            Could you be so kind and check for errors in my example? The thing is, that I would like learn how to do this at runtime.

            I've tried such code:

            MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
            {    
                ui->setupUi(this);
                this->worldArea = new WorldArea(centralWidget());
                // Why this does not work??
                QVBoxLayout *layout = new QVBoxLayout;
                layout->addWidget(worldArea);
                this->setLayout(layout);
            }
            

            Here is my project: https://www.dropbox.com/s/vffprhosx396mr4/WorldPainterProblem.7z?dl=0

            Once again, thank you for your help.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @Wodzu said:
            Ok .
            here you set on mainwindow and for all other widgets it would be correct but for mainwin
            it should be the centralWidget()
            this->setLayout(layout); <<<<
            becomes
            centralWidget()->setlayout(layout);

            Update:
            You must add a widget as central yourself when from code!: sorry. i forgot.

             setCentralWidget( new QWidget ()); /// seems NOT to Be needed
            QVBoxLayout *layout = new QVBoxLayout;
            TheWorldMap * wm = new TheWorldMap(centralWidget());
            layout->addWidget( wm );  
            centralWidget()->setLayout(layout);
            
            
            1 Reply Last reply
            1

            • Login

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