有以下最低限度可重现的示例:
主要.py:
import sys
from PyQt5.QtWidgets import QApplication, QDialog, QPushButton
from PyQt5.uic import loadUi
class MyWindow(QDialog):
def __init__(self):
super().__init__()
loadUi('main.ui', self)
for i in range(15):
button = QPushButton(f'Button {i}')
self.verticalLayout.addWidget(button)
self.scrollArea.setWidgetResizable(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
主.ui:
<?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>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<widget class="QScrollArea" name="scrollArea">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>401</width>
<height>301</height>
</rect>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="scrollAreaWidgetContents">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>399</width>
<height>299</height>
</rect>
</property>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>401</width>
<height>301</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout"/>
</widget>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
需要注意的是,当用按钮QVBoxLayout( ) 填充 -a ( ) 时,它们(按钮)不会缩放,而是为( ) 添加滚动。那些。就像左边一样,而不是右边。verticalLayoutQPushButtonQScrollAreascrollArea

我找到了这个选项:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget, QScrollArea
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Пример QScrollArea без масштабирования виджетов')
self.setGeometry(100, 100, 300, 300)
# Создаем QScrollArea
self.scrollArea = QScrollArea(self)
self.scrollArea.setGeometry(10, 10, 280, 280)
# Создаем содержимое для QScrollArea
scroll_content_widget = QWidget()
scroll_content_layout = QVBoxLayout(scroll_content_widget)
# Добавляем кнопки в QVBoxLayout
for i in range(20):
button = QPushButton(f'Button {i}')
scroll_content_layout.addWidget(button)
# Устанавливаем содержимое для QScrollArea
self.scrollArea.setWidget(scroll_content_widget)
# Отключаем автоматическое масштабирование
self.scrollArea.setWidgetResizable(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
...它有效,但逻辑并没有与图形界面分离,我试图将第二个选项变成第一个,但在某个地方出现了障碍,没有任何效果。main.ui是在Qt Designer中制作的。
问题是什么?
你有一个额外的布局管理器并且
self.scrollArea.setWidgetResizable(True)更新:
我完全为您做了您想要的事情,并在代码中指出了错误所在。
下面的内容与我上面写的内容完全类似。
主要.py
q1572176.ui