我的一个选项卡的结构QTabWidget
如下所示:
小部件的其余选项卡已经响应,所以我想让这个也响应。如果它可以在长度上伸展,那么就没有什么可以在高度上伸展了。
因此,我决定在调整表单大小时可以简单地缩放所有元素,并且我已经问过这个问题,但之后我有新问题:
添加任何
styleSheet
(甚至是空的,例如QLineEdit { }
)时,代码self.setFont(...)
也不起作用self.font.setPointSize(...)
。是否可以将除文本之外的所有元素缩放
QTabWidget
?关于如何正确确保这些选项卡上的响应性,还有其他选择吗?
主.py:
import sys
from PyQt5 import QtGui, uic
from PyQt5.Qt import *
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
uic.loadUi('scale.ui', self)
self.font = QtGui.QFont()
self.font.setFamily("Consolas")
self.font.setPointSize(8)
self.setFont(self.font)
def resizeEvent(self, event):
w = self.size().width()
if w < 150:
self.font.setPointSize(8)
self.setFont(self.font)
elif 150 <= w < 300:
self.font.setPointSize(10)
self.setFont(self.font)
elif 300 <= w < 600:
self.font.setPointSize(12)
self.setFont(self.font)
elif 600 <= w < 800:
self.font.setPointSize(15)
self.setFont(self.font)
elif 800 <= w < 1000:
self.font.setPointSize(20)
self.setFont(self.font)
elif 1000 <= w < 1200:
self.font.setPointSize(25)
self.setFont(self.font)
elif w >= 1200:
self.font.setPointSize(30)
self.setFont(self.font)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
规模.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>546</width>
<height>421</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true">QLineEdit { }</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QTabWidget" name="tabWidget">
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>GroupBox1</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label_2">
<property name="styleSheet">
<string notr="true">font-size: 24px;</string>
</property>
<property name="text">
<string>Задача организации</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="text">
<string>Повседневная практика </string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>GroupBox3</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QLabel" name="label_4">
<property name="styleSheet">
<string notr="true">font-size: 24px;</string>
</property>
<property name="text">
<string>Задача организации</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_3">
<property name="text">
<string>Не следует, однако забывать, что сложившаяся структура</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Идейные соображения высшего порядка</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_3">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>GroupBox2</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QLabel" name="label_3">
<property name="styleSheet">
<string notr="true">font-size: 24px;</string>
</property>
<property name="text">
<string>Задача организации</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_5">
<property name="text">
<string>С другой стороны сложившаяся </string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_2">
<property name="text">
<string>Идейные соображения высшего порядка</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_5">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_2">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="1">
<widget class="QGroupBox" name="groupBox_4">
<property name="title">
<string>GroupBox4</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QLabel" name="label_5">
<property name="styleSheet">
<string notr="true">font-size: 24px;</string>
</property>
<property name="text">
<string>Задача организации</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_6">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit_4">
<property name="text">
<string>Равным образом новая модель организационной</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_4">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_7">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>546</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
如果您删除.ui文件,此代码将起作用styleSheet
"QLineEdit { }"
。
试试这样: