QScrollAreaclass provides a scrolling view onto another widget. 更多 …
def
alignment
()
def
ensureVisible
(x, y[, xmargin=50[, ymargin=50]])
def
ensureWidgetVisible
(childWidget[, xmargin=50[, ymargin=50]])
def
setAlignment
(arg__1)
def
setWidget
(widget)
def
setWidgetResizable
(resizable)
def
takeWidget
()
def
widget
()
def
widgetResizable
()
A scroll area is used to display the contents of a child widget within a frame. If the widget exceeds the size of the frame, the view can provide scroll bars so that the entire area of the child widget can be viewed. The child widget must be specified with
setWidget()。例如:imageLabel = QLabel() image = QImage("happyguy.png") imageLabel.setPixmap(QPixmap.fromImage(image)) scrollArea = QScrollArea() scrollArea.setBackgroundRole(QPalette.Dark) scrollArea.setWidget(imageLabel)The code above creates a scroll area (shown in the images below) containing an image label. When scaling the image, the scroll area can provide the necessary scroll bars:
![]()
![]()
![]()
The scroll bars appearance depends on the currently set
scroll bar policies. You can control the appearance of the scroll bars using the inherited functionality fromQAbstractScrollArea.For example, you can set the
horizontalScrollBarPolicyandverticalScrollBarPolicyproperties. Or if you want the scroll bars to adjust dynamically when the contents of the scroll area changes, you can use thehorizontalScrollBar()andverticalScrollBar()functions (which enable you to access the scroll bars) and set the scroll bars’ values whenever the scroll area’s contents change, using thesetValue()函数。You can retrieve the child widget using the
widget()function. The view can be made to be resizable with thesetWidgetResizable()function. The alignment of the widget can be specified withsetAlignment().Two convenience functions
ensureVisible()andensureWidgetVisible()ensure a certain region of the contents is visible inside the viewport, by scrolling the contents if necessary.
When using a scroll area to display the contents of a custom widget, it is important to ensure that the
size hintof the child widget is set to a suitable value. If a standardQWidgetis used for the child widget, it may be necessary to callsetMinimumSize()to ensure that the contents of the widget are shown correctly within the scroll area.If a scroll area is used to display the contents of a widget that contains child widgets arranged in a layout, it is important to realize that the size policy of the layout will also determine the size of the widget. This is especially useful to know if you intend to dynamically change the contents of the layout. In such cases, setting the layout’s
size constraintproperty to one which provides constraints on the minimum and/or maximum size of the layout (e.g.,SetMinAndMaxSize) will cause the size of the scroll area to be updated whenever the contents of the layout changes.For a complete example using the
QScrollAreaclass, see the Image Viewer example. The example shows how to combineQLabelandQScrollArea以显示图像。
QScrollArea
(
[
parent=None
]
)
¶
- param parent
Constructs an empty scroll area with the given
parent
.
另请参阅
PySide2.QtWidgets.QScrollArea.
alignment
(
)
¶
Alignment
另请参阅
PySide2.QtWidgets.QScrollArea.
ensureVisible
(
x
,
y
[
,
xmargin=50
[
,
ymargin=50
]
]
)
¶
x
–
int
y
–
int
xmargin
–
int
ymargin
–
int
Scrolls the contents of the scroll area so that the point (
x
,
y
) is visible inside the region of the viewport with margins specified in pixels by
xmargin
and
ymargin
. If the specified point cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels.
PySide2.QtWidgets.QScrollArea.
ensureWidgetVisible
(
childWidget
[
,
xmargin=50
[
,
ymargin=50
]
]
)
¶
childWidget
–
QWidget
xmargin
–
int
ymargin
–
int
Scrolls the contents of the scroll area so that the
childWidget
of
widget()
is visible inside the viewport with margins specified in pixels by
xmargin
and
ymargin
. If the specified point cannot be reached, the contents are scrolled to the nearest valid position. The default value for both margins is 50 pixels.
PySide2.QtWidgets.QScrollArea.
setAlignment
(
arg__1
)
¶
arg__1
–
Alignment
另请参阅
PySide2.QtWidgets.QScrollArea.
setWidget
(
widget
)
¶
widget
–
QWidget
Sets the scroll area’s
widget
.
widget
becomes a child of the scroll area, and will be destroyed when the scroll area is deleted or when a new widget is set.
The widget’s
autoFillBackground
property will be set to
true
.
If the scroll area is visible when the
widget
is added, you must
show()
it explicitly.
Note that You must add the layout of
widget
before you call this function; if you add it later, the
widget
will not be visible - regardless of when you
show()
the scroll area. In this case, you can also not
show()
the
widget
later.
另请参阅
PySide2.QtWidgets.QScrollArea.
setWidgetResizable
(
resizable
)
¶
resizable
–
bool
另请参阅
PySide2.QtWidgets.QScrollArea.
takeWidget
(
)
¶
Removes the scroll area’s widget, and passes ownership of the widget to the caller.
另请参阅
PySide2.QtWidgets.QScrollArea.
widget
(
)
¶
Returns the scroll area’s widget, or
None
若没有。
另请参阅
PySide2.QtWidgets.QScrollArea.
widgetResizable
(
)
¶
bool
另请参阅