我有这个代码
import sys
from PyQt5 import QtWidgets, QtGui, QtCore
class Content_wid(QtWidgets.QWidget):
def __init__(self, conect, parent=None):
super(Content_wid, self).__init__(parent)
self.content = conect
lay = QtWidgets.QVBoxLayout(self)
lay.setContentsMargins(0, 0, 0, 0)
self.radio = QtWidgets.QRadioButton()
lay.addWidget(self.radio)
self.animation = QtCore.QVariantAnimation()
self.animation.setDuration(1000)
self.animation.valueChanged.connect(self.on_value_changed)
self.animation.setStartValue(self.content.area.sizeHint().height())
self.animation.setEndValue(0)
@QtCore.pyqtSlot("QVariant")
def on_value_changed(self, val):
self.content.area.setMaximumHeight(val)
print(self.content.geometry())
def swap_values(self):
self.animation.blockSignals(True)
start_value = self.animation.startValue()
end_value = self.animation.endValue()
self.animation.setStartValue(end_value)
self.animation.setEndValue(start_value)
self.animation.blockSignals(False)
class Menu(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Menu, self).__init__(parent)
lay = QtWidgets.QVBoxLayout(self)
lay.setContentsMargins(0, 0, 0, 0)
lay.setSpacing(0)
self.group_buttons = QtWidgets.QButtonGroup(self, exclusive=True)
self.group_animation = QtCore.QSequentialAnimationGroup(self)
for x in range(3):
self.area = QtWidgets.QScrollArea(self)
self.area.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.area.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.area.setStyleSheet('background:yellow;border:none;')
self.area.setWidgetResizable(True)
self.content = QtWidgets.QWidget()
self.content.setStyleSheet('background:red')
self.area.setWidget(self.content)
lay_content = QtWidgets.QVBoxLayout(self.content)
lay_content.setContentsMargins(0, 0, 0, 0)
label = QtWidgets.QLabel(self.content)
label.setText('test')
label.setStyleSheet('background:green;padding:20')
lay_content.addWidget(label)
button = Content_wid(self)
setattr(self, "but_{}".format(x), button) # 1self 2name 3object
self.group_buttons.addButton(button.radio, x)
self.area.setMaximumHeight(0)
wid_but = QtWidgets.QWidget()
wid_but.setStyleSheet('background:black;')
lay_wid_but = QtWidgets.QHBoxLayout(wid_but)
lay_wid_but.setContentsMargins(0, 0, 0, 0)
si = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
lay_wid_but.addWidget(button)
lay_wid_but.addItem(si)
lay.addWidget(wid_but)
lay.addWidget(self.area)
# margin down
vs = QtWidgets.QSpacerItem(0, 0, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
lay.addItem(vs)
self.group_buttons.buttonToggled.connect(self.on_button_toggled)
self.last_widget = None
@QtCore.pyqtSlot(QtWidgets.QAbstractButton, bool)
def on_button_toggled(self, button, state):
if state:
wid = button.parent()
if self.group_animation.animationCount() > 0:
self.group_animation.takeAnimation(0)
if isinstance(self.last_widget, Content_wid):
# Invert
self.last_widget.swap_values()
self.group_animation.addAnimation(self.last_widget.animation)
# Invert
wid.swap_values()
self.group_animation.addAnimation(wid.animation)
self.group_animation.start()
self.last_widget = wid
class Test(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.resize(500, 500)
lay = QtWidgets.QVBoxLayout(self)
icon_wid_1 = Menu()
lay.addWidget(icon_wid_1)
parent_wid = QtWidgets.QWidget(self)
parent_wid.setStyleSheet("background:red")
parent_lay = QtWidgets.QVBoxLayout(parent_wid)
icon_wid_3 = Menu(self)
parent_wid.move(300, 300)
parent_lay.addWidget(icon_wid_3)
icon_wid_2 = Menu(self)
icon_wid_2.move(100, 300)
icon_wid_2.resize(50, 60)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Test()
w.show()
sys.exit(app.exec_())
我希望Menu()
小部件在动画过程中改变其大小,既处于自由位置又位于布局内部。我想在动画过程中获得Menu()
的几何形状。如果Menu()
超出了必要,那么一切都很好,但是如果少于 var group: FormGroup = new FormGroup({
...
services: new FormGroup({
service1:new FormControl(),
service2:new FormControl(),
service3:new FormControl(),
otherServices:new FormControl()
})
});
,则会出现问题
我该如何解决这个问题?