'QDialog with QScrollArea adjustSize on widget hide
File: search.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '/home/chris/Documents/papinhio-player/ui/menu-3/radio-stations/search/search.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(837, 556)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/main-window/assets/images/main-window/search.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Dialog.setWindowIcon(icon)
Dialog.setStyleSheet("QScrollArea{\n"
" border:0px;\n"
"}")
self.gridLayout = QtWidgets.QGridLayout(Dialog)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.scrollArea = QtWidgets.QScrollArea(Dialog)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 837, 556))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setObjectName("verticalLayout")
self.search_frame = QtWidgets.QFrame(self.scrollAreaWidgetContents)
self.search_frame.setStyleSheet("QFrame{\n"
" border:none;\n"
"}")
self.search_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.search_frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.search_frame.setObjectName("search_frame")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.search_frame)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.search_phrase = QtWidgets.QLineEdit(self.search_frame)
self.search_phrase.setObjectName("search_phrase")
self.horizontalLayout.addWidget(self.search_phrase)
self.search_button = QtWidgets.QPushButton(self.search_frame)
self.search_button.setIcon(icon)
self.search_button.setObjectName("search_button")
self.horizontalLayout.addWidget(self.search_button)
self.verticalLayout.addWidget(self.search_frame)
self.loading_frame = QtWidgets.QFrame(self.scrollAreaWidgetContents)
self.loading_frame.setStyleSheet("QFrame{\n"
" border:none;\n"
"}")
self.loading_frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.loading_frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.loading_frame.setObjectName("loading_frame")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.loading_frame)
self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
spacerItem = QtWidgets.QSpacerItem(291, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem)
self.loading_label = QtWidgets.QLabel(self.loading_frame)
self.loading_label.setMinimumSize(QtCore.QSize(220, 20))
self.loading_label.setMaximumSize(QtCore.QSize(220, 20))
self.loading_label.setText("")
self.loading_label.setObjectName("loading_label")
self.horizontalLayout_2.addWidget(self.loading_label)
spacerItem1 = QtWidgets.QSpacerItem(290, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.horizontalLayout_2.addItem(spacerItem1)
self.verticalLayout.addWidget(self.loading_frame)
self.results_label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.results_label.setStyleSheet("QLabel{\n"
" font-weight:bold;\n"
" text-decoration:underline;\n"
"}")
self.results_label.setAlignment(QtCore.Qt.AlignCenter)
self.results_label.setObjectName("results_label")
self.verticalLayout.addWidget(self.results_label)
self.table = QtWidgets.QTableWidget(self.scrollAreaWidgetContents)
self.table.setObjectName("table")
self.table.setColumnCount(3)
self.table.setRowCount(0)
item = QtWidgets.QTableWidgetItem()
self.table.setHorizontalHeaderItem(0, item)
item = QtWidgets.QTableWidgetItem()
self.table.setHorizontalHeaderItem(1, item)
item = QtWidgets.QTableWidgetItem()
self.table.setHorizontalHeaderItem(2, item)
self.table.horizontalHeader().setCascadingSectionResizes(False)
self.table.horizontalHeader().setDefaultSectionSize(150)
self.table.horizontalHeader().setStretchLastSection(True)
self.verticalLayout.addWidget(self.table)
self.label_2 = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.label_2.setStyleSheet("QLabel{\n"
" font-weight:bold;\n"
" color:green;\n"
"}")
self.label_2.setAlignment(QtCore.Qt.AlignJustify|QtCore.Qt.AlignVCenter)
self.label_2.setWordWrap(True)
self.label_2.setObjectName("label_2")
self.verticalLayout.addWidget(self.label_2)
self.ok = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap(":/rest-windows/assets/images/rest-windows/ok.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.ok.setIcon(icon1)
self.ok.setObjectName("ok")
self.verticalLayout.addWidget(self.ok)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Εύρεση ραδιοφωνικών συνδέσεων"))
self.search_phrase.setPlaceholderText(_translate("Dialog", "Αναζήτηση βάση τίτλου ραδιοφωνικής σύνδεσης"))
self.search_button.setText(_translate("Dialog", "Αναζήτηση"))
self.results_label.setText(_translate("Dialog", "Αποτέλεσμα αναζήτησης: Η αναζήτηση ολοκληρώθηκε επιτυχώς. Βρέθηκαν ... ραδιοφωνικές συνδέσεις"))
item = self.table.horizontalHeaderItem(0)
item.setText(_translate("Dialog", "Τίτλος"))
item = self.table.horizontalHeaderItem(1)
item.setText(_translate("Dialog", "Radio hostname"))
item = self.table.horizontalHeaderItem(2)
item.setText(_translate("Dialog", "Ενέργεια"))
self.label_2.setText(_translate("Dialog", "Σημείωση 1: Η αναζήτηση πραγματοποιείτε με κριτήριο τον τίτλο των ραδιοφωνικών συνδέσεων.\n"
"Σημείωση 2: Αφήστε κένο πεδίο για εύρεση όλων των ραδιοφωνικών συνδέσεων.\n"
"Σημείωση 3: Η αναζήτηση είναι case insensitive δηλαδή θα πρέπει να δοκιμάσετε και με κεφαλαίους χαρακτήρες εφόσον δεν βρεθούν αποτελέσματα."))
self.ok.setText(_translate("Dialog", "Εντάξει - Κλείσιμο παραθύρου"))
import sys
sys.path.append('../../../')
#import importlib
#icons = importlib.import_module('compiled-ui.icons')
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
File: search_support.py
from PyQt5 import QtCore, QtGui, QtWidgets
import search
import sys
class Run_me:
def __init__(self):
self.app = QtWidgets.QApplication(sys.argv)
self.Dialog = QtWidgets.QDialog()
self.ui = search.Ui_Dialog()
self.ui.setupUi(self.Dialog)
self.Dialog.show()
self.ui.table.setColumnWidth(0, 350)
self.ui.table.setColumnWidth(1, 120)
self.ui.loading_frame.hide()
self.ui.table.hide()
self.ui.results_label.hide()
self.Dialog.adjustSize()
self.Dialog.hide()
self.Dialog.show()
self.Dialog.update()
sys.exit(self.app.exec_())
if __name__ == "__main__":
programm = Run_me()
Try to run search_support.py
The result is:
but the desired result is:
How can i adjustSize of QDialog which has inside a QScrollArea? Which size policies should i use?
Here is the search.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>837</width>
<height>556</height>
</rect>
</property>
<property name="windowTitle">
<string>Εύρεση ραδιοφωνικών συνδέσεων</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>:/main-window/assets/images/main-window/search.png</normaloff>:/main-window/assets/images/main-window/search.png</iconset>
</property>
<property name="styleSheet">
<string notr="true">QScrollArea{
border:0px;
}</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<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>837</width>
<height>556</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QFrame" name="search_frame">
<property name="styleSheet">
<string notr="true">QFrame{
border:none;
}</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="search_phrase">
<property name="placeholderText">
<string>Αναζήτηση βάση τίτλου ραδιοφωνικής σύνδεσης</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="search_button">
<property name="text">
<string>Αναζήτηση</string>
</property>
<property name="icon">
<iconset>
<normaloff>:/main-window/assets/images/main-window/search.png</normaloff>:/main-window/assets/images/main-window/search.png</iconset>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QFrame" name="loading_frame">
<property name="styleSheet">
<string notr="true">QFrame{
border:none;
}</string>
</property>
<property name="frameShape">
<enum>QFrame::StyledPanel</enum>
</property>
<property name="frameShadow">
<enum>QFrame::Raised</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>291</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="loading_label">
<property name="minimumSize">
<size>
<width>220</width>
<height>20</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>220</width>
<height>20</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>290</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QLabel" name="results_label">
<property name="styleSheet">
<string notr="true">QLabel{
font-weight:bold;
text-decoration:underline;
}</string>
</property>
<property name="text">
<string>Αποτέλεσμα αναζήτησης: Η αναζήτηση ολοκληρώθηκε επιτυχώς. Βρέθηκαν ... ραδιοφωνικές συνδέσεις</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QTableWidget" name="table">
<attribute name="horizontalHeaderCascadingSectionResizes">
<bool>false</bool>
</attribute>
<attribute name="horizontalHeaderDefaultSectionSize">
<number>150</number>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<column>
<property name="text">
<string>Τίτλος</string>
</property>
</column>
<column>
<property name="text">
<string>Radio hostname</string>
</property>
</column>
<column>
<property name="text">
<string>Ενέργεια</string>
</property>
</column>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="styleSheet">
<string notr="true">QLabel{
font-weight:bold;
color:green;
}</string>
</property>
<property name="text">
<string>Σημείωση 1: Η αναζήτηση πραγματοποιείτε με κριτήριο τον τίτλο των ραδιοφωνικών συνδέσεων.
Σημείωση 2: Αφήστε κένο πεδίο για εύρεση όλων των ραδιοφωνικών συνδέσεων.
Σημείωση 3: Η αναζήτηση είναι case insensitive δηλαδή θα πρέπει να δοκιμάσετε και με κεφαλαίους χαρακτήρες εφόσον δεν βρεθούν αποτελέσματα.</string>
</property>
<property name="alignment">
<set>Qt::AlignJustify|Qt::AlignVCenter</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="ok">
<property name="text">
<string>Εντάξει - Κλείσιμο παραθύρου</string>
</property>
<property name="icon">
<iconset>
<normaloff>:/rest-windows/assets/images/rest-windows/ok.png</normaloff>:/rest-windows/assets/images/rest-windows/ok.png</iconset>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
Solution 1:[1]
QScrollArea caches the size hint of the child widget when sizeHint()
is called the first time, so you can use a promoted widget for the main scroll area and re-implement it so that it always returns the current size hint.
class ScrollArea(QtWidgets.QScrollArea):
def sizeHint(self):
if self.widget() is not None:
f = self.frameWidth() * 2
return self.widget().sizeHint() + QtCore.QSize(f, f)
return super().sizeHint()
Solution 2:[2]
I tried this:
- QDialog:
Horizontal policy: preferred
Vertical policy: preferred
- QScrollArea:
Horizontal policy: Expanding
Vertical policy: Expanding
- scrollAreaWidgetContents:
Horizontal policy: Ignored
Vertical policy: Fixed
- After that i tried the following code:
q_dialog_width = self.Dialog.width()
self.Dialog.adjustSize()
self.Dialog.hide()
self.Dialog.show()
self.Dialog.adjustSize()
search_frame_height = self.ui.search_frame.height()
label_2_height = self.ui.label_2.height()
ok_height = self.ui.ok.height()
q_dialog_height = 11 + search_frame_height + 11 + label_2_height + 11 +ok_height + 11 - 8 #I don't know why - 8 is neccessary
self.Dialog.setGeometry(50,50,q_dialog_width,q_dialog_height)
And that's it. The QDialog height it's adjusted to it's contents.
Print Screen:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | musicamante |
Solution 2 | marc_s |