如何使用QsCrollarea使卷轴出现
我正在尝试使用pyqt5的垂直布局来创建一个滚动区域,然后将其放入一些标签中。我知道,即使它是垂直布局,如果文本不合格,也应该水平滚动。但是,无论我尝试什么,都不会让我滚动。
这是我正在使用的代码:
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(300, 300, 803, 520)
self.init_ui()
def init_ui(self):
self.teacher_box = QScrollArea(self)
self.teacher_box.setGeometry(360, 10, 420, 181)
self.teacher_box.setWidgetResizable(True)
self.teacher_box.setObjectName("teacher_box")
self.teacher_box_widget = QWidget()
self.teacher_box_widget.setGeometry(QtCore.QRect(0, 0, 420, 181))
self.teacher_box_widget.setObjectName("teacher_box_widget")
self.verticalLayout = QVBoxLayout(self.teacher_box_widget)
self.verticalLayout.setObjectName("verticalLayout")
self.teacher_box.setWidget(self.teacher_box_widget)
self.teacher_label = QtWidgets.QLabel(self.teacher_box_widget)
self.teacher_label.setText("This is a new text label that i created using pyqt5's qscrollarea and now the label is going to get outside the boundaries")
self.teacher_label.adjustSize()
self.teacher_label.move(10, 10)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = window()
win.show()
sys.exit(app.exec_())
这是应该看起来的外观:
这就是外观:
我希望我的问题很清楚
I am trying to create a scroll area with a vertical layout using pyqt5, and I am putting inside some labels. I know that even if it is a vertical layout it is supposed to scroll horizontally if the text does not fit. But no matter what I try it does not let me scroll.
This is the code I am using:
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.setGeometry(300, 300, 803, 520)
self.init_ui()
def init_ui(self):
self.teacher_box = QScrollArea(self)
self.teacher_box.setGeometry(360, 10, 420, 181)
self.teacher_box.setWidgetResizable(True)
self.teacher_box.setObjectName("teacher_box")
self.teacher_box_widget = QWidget()
self.teacher_box_widget.setGeometry(QtCore.QRect(0, 0, 420, 181))
self.teacher_box_widget.setObjectName("teacher_box_widget")
self.verticalLayout = QVBoxLayout(self.teacher_box_widget)
self.verticalLayout.setObjectName("verticalLayout")
self.teacher_box.setWidget(self.teacher_box_widget)
self.teacher_label = QtWidgets.QLabel(self.teacher_box_widget)
self.teacher_label.setText("This is a new text label that i created using pyqt5's qscrollarea and now the label is going to get outside the boundaries")
self.teacher_label.adjustSize()
self.teacher_label.move(10, 10)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = window()
win.show()
sys.exit(app.exec_())
Here is how it should look:
Here is how it looks:
I hope my question is clear
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于许多问题想知道如何使用许多小部件拥有的QSCrollarea,因此我将获得许可证以详细说明各种形式,并将它们用作未来读者的规范答案。
qscrollarea只允许放置一个容器小部件,因此必须将其他小部件作为容器小部件的孩子放置。
要将小部件放置为另一个可能性:
1。使用qlayout:
qlayouts允许您根据qsizepolicy,strecth,sizehint等自动化小部件的几何形状。 true的窗口属性的属性。
2。直接设置小部件而无需布局:
在这种情况下,您必须计算包含内部窗口小部件的最小几何形状并将大小设置在容器中,也必须将widetgetresable属性设置为false:
Since many questions wonder how to use a QScrollArea that many widgets have, I will take the license to explain the various forms in detail and use them as a canonical answer for future readers.
QScrollArea only allows placing a container widget so the other widgets must be placed as children of the container widget.
And to place the widgets as children of another there are 2 possibilities:
1. Use a QLayout:
QLayouts allow you to automate the geometry of the widgets based on the QSizePolicy, strecth, sizeHint, etc. So in that case it's simple: Just set the widgetResizable property to True.
2. Set the widgets directly without layouts:
In this case you must calculate the minimum geometry that contains the internal widgets and set the size in the container, also you must set the widgetResizable property to False: