Resizable layouts/frames in Qt, per widget by the user - c++

I use QGridLayout very often, and there's a requirement I don't know how to or if I can achieve with this kind of layouts.
My question: Imagine I have two normal widgets (derived from QWidget) on the left and right (on something like QHBoxLayout or QGridLayout), and I would like to have the line separating them movable by the user. Is that possible?
More information:
To give an example, imagine the default Windows registry editor. You have the part on the left, where there are keys and paths, and on the right, where there are values to be edited.
I would like to emphasize that I'm not asking for an explorer view. What I have basically is a plot widget on the right, and a QTableView widget on the left, and I would like the user to be able to conveniently scale with his mouse, which widget should be horizontally bigger.
Is there some kind of Layout that is scalable by mouse?
Please ask for more information of you require it.

I think you should use a QSplitter.
According to the documentation:
A splitter lets the user control the size of child widgets by dragging
the boundary between the children. Any number of widgets may be
controlled by a single splitter.
For example, using Qt Creator, if we have two QGridLayout with a QPushButton on each one, we can select both QGridLayout and use the Lay Out Horizontally in Splitter option.
After that, we could move the boundary between them to control the size of child widgets:
I made an example. Here you have the code for the ui file:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>435</width>
<height>105</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QSplitter" name="splitter">
<property name="geometry">
<rect>
<x>9</x>
<y>10</y>
<width>411</width>
<height>71</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QWidget" name="gridLayoutWidget">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="gridLayoutWidget_2">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>

Related

How does the Auto-Exclusion work with QRadioButtons with nested widgets?

I am building a QWidget with QRadioButtons at different levels. In other words, my widget contains some radio buttons and a subwidget (labeled groupBox in the screenshot) that also contains radio buttons.
Here is my problem: the radio buttons inside groupBox seem to interfere with the top level radio buttons (radioButton_1 and radioButton_2). I would expect that exactly one of radioButton_1 and radioButton_2 is checked at any given time, but it is now possible to uncheck these by clicking on the currently checked radio button.
The fix I came up with is to add setChecked(true) to the signal handler for radioButton_1.clicked() and radioButton_2.clicked(), but this seems a bit hacky.
connect(ui->radioButton_1, &RadioButton::clicked, [this]() {
ui->radioButton_1.setChecked(true);
});
connect(ui->radioButton_2, &RadioButton::clicked, [this]() {
ui->radioButton_2.setChecked(true);
});
Is there a better way to get the functionality back? Perhaps a function like setRadioButtonGroup({ui->radioButton_1, ui->radioButton_2}).
EDIT:
Per request for a MCVE, below is the form mainwindow.ui. Other files (mainwindow.cpp, main.cpp, mainwindow.h) are just the boilerplate provided when a QWidget Application is created in Qt Creator.
<?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>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QRadioButton" name="radioButton_1">
<property name="text">
<string>radioButton_1</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_2">
<property name="text">
<string>radioButton_2</string>
</property>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>groupBox</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QRadioButton" name="radioButton_3">
<property name="text">
<string>radioButton_3</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_4">
<property name="text">
<string>radioButton_4</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
To address this, I suggest you create a QButtonGroup and add all four radio button to it.
See https://doc.qt.io/qt-5/qbuttongroup.html

Dynamically created QWidget in QFormLayout does not have frame, even if explicitly asked to do so

I have a little problem with this bit of code:
DatasetWidget::DatasetWidget(QWidget *parent) : QWidget(parent),
ui(new Ui::DatasetWidget)
{
ui->setupUi(this);
// cut non-ui related stuff here ...
// Add widgets
for(int loop=0;loop<theAmountOfItemsWeAdd;++loop)
{
QLabel * ql=new QLabel("Caption");
QLineEdit * qle=new QLineEdit(this);
qle->setSizePolicy(QSizePolicy::Policy::Expanding,QSizePolicy::Policy::Fixed);
ui->formDisp->addRow(ql,qle); // ui->formDisp is of type QFormLayout*
}
// more non-UI related code
I'm creating a QLineEdit widget and adding it to a QFormLayout, "formDisp". For some weird reason that I can't figure out, the created QLineEdit does not have any frame and setFrame doesn't enable it.
I furthermore append the relevant parts of the .ui file here:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>DatasetWidget</class>
<widget class="QWidget" name="DatasetWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>723</width>
<height>591</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Datensatz</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="lblCaptionDatensatz">
<property name="text">
<string>Datensatz:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="lblDatensatz">
<property name="font">
<font>
<weight>75</weight>
<bold>true</bold>
<underline>true</underline>
</font>
</property>
<property name="text">
<string>C-XXXX-XXX</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="leDescriptor">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip">
<string extracomment="ServiceTag"/>
</property>
<property name="maxLength">
<number>7</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="cmdDelete">
<property name="text">
<string/>
</property>
<property name="icon">
<iconset resource="hawams.qrc">
<normaloff>:/icons/res/trash-3x.png</normaloff>:/icons/res/trash- 3x.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QFormLayout" name="formDisp"/>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</widget>
<connections/>
</ui>
As widgets added in QtCreator look exactly how they are supposed to look, I suppose something about my code must be wrong.
Update: Problem solved: Surrounding QWidget was being used inside a QListView, where it replaced an older, fixed-size version of the widget. In the code that fills the QListView with items, there was a hard-coded size hint which forced Qt to resize the new (bigger) version of the QWidget and "squeeze" it vertically.
Conclusion: When you embed a QWidget derivate in buggy old code that resizes it so there isn't enough space anymore to display all items inside the QWidget's layout, things get messy.
The setFrame(true) is a no-op since the frame is set by default.
Alas, I can't reproduce - the following has both line edits looking identical, save for their size, and their minimum sizes are set to different values, with the second one being larger than first one. A QFormLayout does not enforce a fixed row height!
// https://github.com/KubaO/stackoverflown/tree/master/questions/lineedit-frame-37831979
#include <QtWidgets>
#include <QtUiTools>
const char ui[] = R"EOF(
<ui version="4.0">
<class>DatasetWidget</class>
<widget class="QWidget" name="DatasetWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="lblCaptionDatensatz">
<property name="text">
<string>Datensatz:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="lblDatensatz">
<property name="text">
<string>C-XXXX-XXX</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="leDescriptor">
<property name="maxLength">
<number>7</number>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formDisp"></layout>
</item>
</layout>
</widget>
<connections/>
</ui>)EOF";
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QBuffer buf;
buf.setData(ui, sizeof(ui));
auto w = QUiLoader().load(&buf);
auto & layout = *w->findChild<QFormLayout*>("formDisp");
QLineEdit edit1, edit2;
layout.addRow("Edit1", &edit1);
layout.addRow("Edit2", &edit2);
edit1.setMinimumHeight(50);
edit2.setMinimumHeight(100);
w->show();
return app.exec();
}
Problem solved. First Widget added to QFormLayout is a QLabel. The row height is then set to the height of the label. All following widgets get stuffed into the "too narrow" rows and if Qt attempted to draw a frame around them, the text line wouldn't fit into them anymore - so there is no frame. Solution: e.g. setMinimumHeight(32) on the first element that is being added to the QFormLayout.
There was also a second mistake. The whole QWidget is the graphical representation of a dataset, and in the main application many instances of this widget are used in a QListView to display the result of a database query. Before the use of the dynamically created QWidget, I had used a static widget with a fixed size. This size was used as sizeHint inside the code that filled the QListView, and I totally forgot about that. After removing the wrong sizeHint in the code of the application's main window, also the setMinimumHeight(someInt) wasn't needed anymore.

Resizing Qt UI elements

I have a simple qt ui that contains
|- central widget
|- label
|- button
I would like to manually place and resize the label and button freely (using the mouse), and I just can't seem to be able to do that.
I can only resize the entire window, and the label/button stretch with it. I tried all combinations of size policies
Thanks!
<?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>326</width>
<height>254</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Bla</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="startButton">
<property name="text">
<string>Start</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
try to remove
<layout class="QVBoxLayout" name="verticalLayout">
and
</layout>
but if you are working under qt creator or qt designer,
you can easily do this thing. You need to choose centralWidget and click to button Break Layout that placed under menu bar.
Check this documentation. This should be helpful to utilize Qt Designer components. It is quite possible that you don't have to do lot of work. I never used this so my help ends here.

QTabWidget Content Not Expanding

Attached is an example of the QT Designer with my issue. My goal is to get any content inside (in particular the lineEdit) to expand to the far right of QTabWidget its within.
If you notice I have the tab selected in the picture and it says there is no layout currently (as indicated by the red no smoking looking symbol on it). When I try to add a layout to it using the buttons above the tabs (for horizontal, vertical, grid, form, etc...), no matter what I do it doesn't change the tabs layout but changes the very top level widgets layout instead which I don't want because that is giving the QTabWidget its ability to take on whatever the size of the window is.
I tried giving a horizontal and vertical layout to the elements within the tab thinking that might work but it didn't work either as my next attached image shows. My hunch is because the tab has no layout, any layouts within will not be honored.
Any help is appreciated. I've spent hours and can't figure it out for the life of me...!
Below is the UI XML:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>xMarket</class>
<widget class="QWidget" name="xMarket">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>737</width>
<height>421</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTabWidget" name="xMarketTabWidget">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>240</x>
<y>30</y>
<width>113</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>150</x>
<y>30</y>
<width>59</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>Search:</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
To set the layout on a tab, you first need to select its parent tab-widget.
You can do this either by selecting it by name in the Object Inspector (i.e. by clicking on xMarketTabWidget in your example), or by simply clicking on an appropriate tab in its tab-bar. The parent tab-widget must have a selection rectangle around it before you can set a layout on one of its tabs.
Once you've done that, you can click the Lay Out Horizontally or Lay Out in a Grid button on the Designer toolbar to set an appropriate layout for your tab. But note that a tab must have at least one child widget before you can set a layout on it - the layout buttons will all be disabled for empty tabs.

Setting QScrollArea as central widget in Qt

In Qt I have removed the central widget in an xml editor and replaced it with a QScrollArea, this works when I preview it in QtDesigner:
but when I run the program it looks like this:
Are you not meant to remove the central widget or is there a sizePolicy I have to change?
Here is the ui file:
<?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>179</width>
<height>191</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QScrollArea" name="scrollArea">
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>224</width>
<height>628</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<pointsize>75</pointsize>
</font>
</property>
<property name="midLineWidth">
<number>2</number>
</property>
<property name="text">
<string>label
</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>179</width>
<height>19</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>
<include location="1.qrc"/>
</resources>
<connections/>
</ui>
Check your ui_mainwindow.h. See if there is a line like:
MainWindow->setCentralWidget(scrollArea);
You can use QMainWindow::setCentralWidget function in your mainwindow.cpp to set a central widget to your main window:
setCentralWidget(myScrollArea);
I don't think there is any way of changing the central widget from the designer.
I am getting proper window when I ran the program with your ui file content. I don't know why it is not creating problem like you have.
I would recommend you to use Qt designer inbuilt in Qt Creator which I felt easier to use.
Also, I think the problem can be one of these:
Generally central widget is good as it provides a base on which you can place all your layouts. So, it is good to have it in your UI.
Here, take care that after inserting widgets into layouts you don't simply break those layouts. The designer will reset the widget width and height. So you have to set them again.
Next, this may have occured if the ui file loaded in designer is not up-to-date while it is changed on disk.
Keep track of all the layouts, the geometry and sizePolicy of widgets.
I won't be able to answer in code since when I used the ui file content which you have given, it doesn't cause problem here.
Create a layout on your mainwindow in which you would put your QScrollArea. This would expand it according to the layout.
P.S. : I open it in Qt Creator.
So please give some more info whether it is fixed when you set the geometry of scrollarea manually.