2

I'm a beginner with Qt and I haven't understand yet the layout on centralWidget.

I have a custom QWidget subclass (with a .ui and the cpp class) that I want to add to the central Widget.

I'd like to understand how to say to the QMainWindow subclass to resize and fit the content whenever I add something.

I've tried with adjustSize method both on mainwindow and on centralWidget objects but nothing change..

Anyway, I'm adding the widget in this way:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    MyWidget *w = new Undistort();
    w->setParent(this->centralWidget());
}

some advice?

nkint
  • 11,513
  • 31
  • 103
  • 174
  • do you have a layout in the QMainWindow class? – headsvk Sep 02 '13 at 10:45
  • I have tried with both. A grid layout and no layout at all.. the main window does not changed it size.. – nkint Sep 02 '13 at 10:48
  • so you add the widget to the window programatically? you should show some code for us to help you – headsvk Sep 02 '13 at 10:52
  • you are right, see the edit – nkint Sep 02 '13 at 11:18
  • you need to add the widget to the window's layout, setParent() doesn't do it – headsvk Sep 02 '13 at 11:23
  • 2
    If `MyWidget` is supposed to be inside the central widget, you should add it to its layout: `this->centralWidget()->layout()->addWidget(w);`. Remember to set a layout to your central widget if you haven't already. If you want `MyWidget` to be the central widget: `this->setCentralWidget(w);` – thuga Sep 02 '13 at 11:27
  • both of those solutions does not resize the mainwindow. maybe i have setted something wrong somewherelesE? – nkint Sep 02 '13 at 12:34
  • @nkint Is your mainwindow too small to fit your widget? – thuga Sep 02 '13 at 12:51
  • The `QLayout` managers won't resize your window, they resize the widgets to fit within your window. – Nicholas Smith Sep 02 '13 at 13:04
  • @NicholasSmith When the window is too small for the contents to fit its layout, it will be resized. This of course depends of the size policy and the maximum/minimum sizes. You can test this by having a window that contains a button. Add a widget to the window's layout every time the button is pressed. When there is no space left for more widgets, the window will start to grow. – thuga Sep 02 '13 at 13:20
  • Interesting, last time I was monkeying with layouts and non-maximised by default it didn't. Maybe a configuration issue. – Nicholas Smith Sep 02 '13 at 13:27
  • yes the main window is the standard one (i haven't touched it with qt designer): very small, my widget is bigger – nkint Sep 02 '13 at 13:57
  • @nkint Plesae check following link, May useful http://stackoverflow.com/questions/18304355/centering-widgets-in-a-dynamic-layout/18308219#18308219 – Ashif Sep 02 '13 at 14:09
  • If you are using Custom widget in a layout. You need to implement "virtual QSize QWidget::sizeHint() const", for your custom widget. – Ashif Sep 02 '13 at 14:16

1 Answers1

1

Given example, depending on Pixmap size, QMainWindow will resize. Generally this is not the ideal case, as a user MainWindow need to display on the desktop, It should not be more that your desktop screen size. I am not sure you are actually looking for this. Copied fromSO Ans

#include "mainwindow.h"
#include <QLabel>
#include <QHBoxLayout>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindowClass)
{
    ui->setupUi(this);

    QPixmap pix;
    pix.load("C:\\Users\\user\\Desktop\\Uninstallation failure2.png");

    //Replace with  ImageLabel2
    QLabel* image = new QLabel(this);
    image->setPixmap(pix);

    QHBoxLayout* hbox = new QHBoxLayout(this);
    hbox->addWidget(image);
    QWidget* centreWidget = new QWidget();

    //QMainwindow, having a feature called centreWidget, to set the layout.
    centreWidget->setLayout( hbox ); 
    setCentralWidget( centreWidget );
}
Community
  • 1
  • 1
Ashif
  • 1,652
  • 14
  • 30