I am making the GUI of my software with QT Designer.
My GUI has some tabs which I successfully managed to auto-resize in full width when the user makes fullscreen the GUI on his computer.
Then inside each tab, I need to insert a Toolbox. But this time, I didn't succeed in auto-resize this toolbox when the user makes the GUI full screen.
I think the best way to explain to you my issue is to have a look at my screenshots:
My GUI in QT designer:
The preview with the issue:
You can see on the preview that the toolbox is keeping the same size when I go for fullscreen.
Solution
You need to add a layout to tab_Run, e.g. by selecting tabWidget and pressing Ctrl+L (on Windows), so your tree would look like this:
Note: In general, you have to set a layout for every widget, the size of whos children you want to be managed automatically by Qt.
Example
As a reference, here is the ui-file I have created:
<?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>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="tab_Run">
<attribute name="title">
<string>Run</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QToolBox" name="toolBox">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="page">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>758</width>
<height>443</height>
</rect>
</property>
<attribute name="label">
<string>Page 1</string>
</attribute>
</widget>
<widget class="QWidget" name="page_2">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>758</width>
<height>443</height>
</rect>
</property>
<attribute name="label">
<string>Page 2</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_Campaigns">
<attribute name="title">
<string>Campaigns</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_Settings">
<attribute name="title">
<string>Settings</string>
</attribute>
</widget>
<widget class="QWidget" name="tab_Help">
<attribute name="title">
<string>Help</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Result
Here is the produced result:
Related
My colleague created a C++ Qt Project on Linux and I "transposed" it on Windows. Now, I am a functional GUI but I don't know why, when I open the .ui file with QtDesigner, the file is empty, all widgets disappeared.
Any idea how to get back my widgets or is it only because the file was created on QtDesigner on Linux ?
Here is a part of the ui file opened on Windows with a text editor:
<?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>874</width>
<height>497</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>20</x>
<y>110</y>
<width>541</width>
<height>331</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>860</width>
<height>700</height>
</size>
</property>
<property name="currentIndex">
<number>1</number>
</property>
<widget class="QWidget" name="buildMotionTab_3">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<widget class="QFrame" name="centralFrame_3">
<property name="geometry">
<rect>
<x>-10</x>
<y>0</y>
<width>860</width>
<height>851</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>860</width>
<height>851</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>810</width>
<height>851</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QFrame" name="motorFrameP_3">
<property name="geometry">
<rect>
<x>260</x>
<y>10</y>
<width>281</width>
<height>280</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>250</width>
<height>280</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>300</width>
<height>280</height>
</size>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<widget class="QSpinBox" name="LineDist">
<property name="geometry">
<rect>
<x>190</x>
<y>150</y>
<width>81</width>
<height>32</height>
</rect>
</property>
<property name="minimum">
<number>1</number>
</property>
<property name="maximum">
<number>470</number>
</property>
<property name="singleStep">
<number>1</number>
</property>
<property name="value">
<number>50</number>
</property>
</widget>
[...]
<widget class="QPushButton" name="StandDownButton">
<property name="geometry">
<rect>
<x>20</x>
<y>30</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>StandDown</string>
</property>
</widget>
<widget class="QPushButton" name="Homing1Button">
<property name="geometry">
<rect>
<x>120</x>
<y>70</y>
<width>101</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Homing X</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>874</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
Looks like some thing went wrong in transporting the file (I can't say, but could be copy was done in binary mode? or some lines were not expanded in case you copied and pasted the text ..)
The ui xml you shared is broken at line 127 where it shows [...] which is not valid, in place of this it should show closing xml tags for </widget> , I think 4 closing tags </widget> are missing, then it should be ok.
I have an issue with getting a QTreewidget full height inside a QFormLayout. I'm on Windows 10 and use QT 5.7
Things I tried:
all possible Vertical policy's without success. Change the
FieldGrowthPolicy of the layout to AllNonFixedFieldsGrow
Use another widget, all fail to make this work
That's all the options I found to possibly make this work.
Here's what I try to achieve:
Here's my ui file:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>760</width>
<height>747</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Please grow the treeview:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QTreeWidget" name="treeWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
Please read the Qt tutorials about layouts.
I added a two layouts and a spacer to your ui file.
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>489</width>
<height>215</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Please grow the treeview:</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QTreeWidget" name="treeWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
Based on this answer: Qt formlayout not expanding qplaintextedit vertically which worked for me, setting the Vertical Stretch attribute to something other than 0 should do the trick.
Following .ui file, created with Qt 5.7 Designer, shows a text box and a spacer:
<?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>400</width>
<height>310</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPlainTextEdit" name="plainTextEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="plainText">
<string>abc</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
For example, if the text field has "abc", it should be one line high; if it has two lines (say "abc\nabc"), OR one long line that requires two lines to display, then the text box should be two lines high.
Minimum height is 0. I tried changing the sizing policy of the text box so that the widget is as small as possible, but none do this.
Equivalent Python the code to do this is:
app=QApplication([])
widget = QWidget()
widget.setLayout(QVBoxLayout())
text_widget = QPlainTextEdit('abc')
# text_widget.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.MinimumExpanding)
widget.layout().addWidget(text_widget)
spacer = QSpacerItem(20, 40, vPolicy=QSizePolicy.Expanding)
widget.layout().addSpacerItem(spacer)
widget.show()
app.exec_()
So i have a small program i've made with Qt Designer, and for the most part, it's completely fine. But there is one niggling problem;
I have a QLabel, for which the text is sometimes too long, so i want to make the text wrap. This should be a simple task.
Here is what it looks like with no wordwrap
And this is fine. But when the text gets too long, it puts in a horrible scroll bar. I don't want this, so i enable wordwrap. But then this happens:
Which at first i thought was just Qt Designer being crap, so compiled and run hoping it would go away. Sometimes this happens, so i thought it was a reasonable assumption.
It wasn't.
Why the hell is this happening?!
EDIT: enabling word wrap manually in the widget initialiser also causes the same behaviour - so it's not caused by Qt Designer, it's an issue elsewhere. Any help would be greatly appreciated!
Here is the content of 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>705</width>
<height>347</height>
</rect>
</property>
<property name="windowTitle">
<string>Title</string>
</property>
<property name="windowIcon">
<iconset resource="icons.qrc">
<normaloff>:/icons/icons/3/bonus48x48_20.png</normaloff>:/icons/icons/3/bonus48x48_20.png</iconset>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout" columnstretch="0,1,1">
<item row="1" column="0">
<widget class="QLabel" name="label_molecule">
<property name="text">
<string>Molecule:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineEdit_molecule"/>
</item>
<item row="2" column="0" colspan="2">
<widget class="QToolBox" name="toolBox_modelDetails">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="page_isomer">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>373</width>
<height>154</height>
</rect>
</property>
<attribute name="label">
<string>Isomer</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="listWidget_isomers">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_model">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>357</width>
<height>231</height>
</rect>
</property>
<attribute name="label">
<string>Model</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,0">
<item>
<widget class="QListWidget" name="listWidget_models">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<stylestrategy>PreferDefault</stylestrategy>
</font>
</property>
<property name="lineWidth">
<number>1</number>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_description">
<property name="text">
<string>Howdy!</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="page_basisSet">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>373</width>
<height>154</height>
</rect>
</property>
<attribute name="label">
<string>Basis Set</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QListWidget" name="listWidget_basisSets">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item row="3" column="0" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout_buttons">
<item>
<spacer name="horizontalSpacer_buttons">
<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="pushButton_save">
<property name="text">
<string>Save Input File</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_open">
<property name="text">
<string>Open</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="2" rowspan="2">
<widget class="QGroupBox" name="groupBox_MoleculeViewer">
<property name="title">
<string>Molecule</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources>
<include location="icons.qrc"/>
</resources>
<connections/>
</ui>
QLabel in combination with word wrap and Layouts give some funny behaviour.
See https://bugreports.qt.io/browse/QTBUG-37673.
Why are you nesting your widgets (the QLabel and the QListWidget) into a QWidget?
That's why you are getting two scrollbars. One from the QListWidget, because it has more items than can be viewed. And one for the parent QWidget.
You should be using layouts.
This is what you have:
Should be something like this:
I have a simple form: QDockWidget on the right side, QGroupBox on the left side of QMainWindow. QGroupBox is layed out using vertical layout.
I want QDockWidget to be expanded and occupy all the free space when the main window is enlarged or maximized.
How do I do it?
Bellow is the source code of my .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>653</width>
<height>381</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>GroupBox</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QDockWidget" name="dockWidget">
<attribute name="dockWidgetArea">
<number>2</number>
</attribute>
<widget class="QWidget" name="dockWidgetContents"/>
</widget>
</widget>
<resources/>
<connections/>
</ui>
You will need to reimplement QWidget::changeEvent (or a resizeEvent for resizing) to react to QEvent::WindowStateChange. If one of those events occurs, get the QWidget::windowState and resize your dock to whatever you want.