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