PDA

View Full Version : Shrinking / extending functionality a widget generated by Qt designer



donelron
5th December 2016, 21:45
Dear all,

I am trying to add some functionality to an already existing widget: some of the GUI elements should be shown or hidden depending on if the user wishes to see them or not. Also, the widget should extend or shrink in order to give just as much space as possible for the GUI elements. Basically, I am trying to do the same thing that is shown in this example:
http://doc.qt.io/archives/qt-5.5/qtwidgets-dialogs-extension-example.html

However, since the widget I am working on already exists and was created by means of the Qt designer as a .ui file I can not just copy the code from the example.
A minimal working example of what I am trying to do is the following:

--------------- Start of MainWindow.ui ---------------


<?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>746</width>
<height>494</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>331</width>
<height>181</height>
</rect>
</property>
<layout class="QGridLayout" name="mainLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item row="1" column="0">
<widget class="QPushButton" name="pbtExtend">
<property name="text">
<string>extend</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QWidget" name="extensionWidget" native="true">
<widget class="QWidget" name="gridLayoutWidget_2">
<property name="geometry">
<rect>
<x>60</x>
<y>30</y>
<width>160</width>
<height>81</height>
</rect>
</property>
<layout class="QGridLayout" name="extensionLayout">
<item row="1" column="0">
<widget class="QLabel" name="lbl2">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lbl1">
<property name="text">
<string>label 1</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QDoubleSpinBox" name="dsb1"/>
</item>
<item row="1" column="1">
<widget class="QDoubleSpinBox" name="dsb2"/>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>746</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

--------------- end of MainWindow.ui ---------------


--------------- Start of MainWindow.h ---------------


#include <QtWidgets>
#include <QMainWindow>
#include <ui_MainWindow.h>

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QMainWindow *parent = 0);
private:
Ui::MainWindow * m_pMainWindow;
};

--------------- end of MainWindow.h ---------------


--------------- Start of MainWindow.cpp ---------------


#include <iostream>
#include "MainWindow.h"

MainWindow::MainWindow(QMainWindow *parent) :
QMainWindow(parent)
{
m_pMainWindow = new Ui::MainWindow;
m_pMainWindow->setupUi(this);
m_pMainWindow->extensionWidget->setVisible(true);
m_pMainWindow->mainLayout->setSizeConstraint(QLayout::SetFixedSize); //if this line is uncommented, then the extensionWidget is not visible at all
QObject::connect(m_pMainWindow->pbtExtend, &QPushButton::toggled, m_pMainWindow->extensionWidget, &QWidget::setVisible);
}

--------------- end of MainWindow.cpp ---------------


In main.cpp I am calling it like:



QApplication app(argc, argv);
MainWindow w;
w.show();
app.exec();


In forums / posts I keep reading that I need to do sth. like
m_pMainWindow->mainLayout->setSizeConstraint(QLayout::SetFixedSize);

in order to achieve that the window size extends and shrinks as necessary, however, if I use this line, then the extensionWidget is not visible at all.
I already tried to do the same thing in another widget that subclasses QDialog because I was suspecting that it maybe only works in a QDialog, but the result was the same.
What am I doing wrong???

Thanks in advance!