python - PyQt4 Adding a custom widget derived from another custom widget to layout
问题描述
I'm trying to build a widget structure which will have a base widget which is derived from QtGui.QWidget, then I'll put the base features I want in every widget I create. Once the base widget is done, i'll create other types of widgets derived from the base widget.
here is an example
class TurquoiseWidget(QtGui.QWidget):
def __init__(self, title="Widget", enable_title=False, font_size=9):
super(TurquoiseWidget, self).__init__()
self.setFixedSize(self.minimumSize())
self.widget_area = QtGui.QFrame()
self.widget_area.setFrameShape(QtGui.QFrame.Box)
self.widget_area.setFrameShadow(QtGui.QFrame.Sunken)
title_lbl = QtGui.QLabel(title)
font = title_lbl.font()
font = QtGui.QFont()
# font.setFamily("Quicksand")
font.setPointSize(font_size)
title_lbl.setFont(font)
title_lyt = QtGui.QHBoxLayout()
title_lyt.setAlignment(QtCore.Qt.AlignCenter)
title_lyt.addWidget(title_lbl)
main_lyt = QtGui.QVBoxLayout()
main_lyt.setAlignment(QtCore.Qt.AlignTop)
if enable_title:
main_lyt.addLayout(title_lyt)
main_lyt.addWidget(self.widget_area)
self.setLayout(main_lyt)
class ArmControlWidget(TurquoiseWidget):
def __init__(self):
super(ArmControlWidget, self).__init__()
self.arm_button = QtGui.QPushButton("Arm")
self.disarm_button = QtGui.QPushButton("Disarm")
self.arm_button.clicked.connect(lambda: self.Arm(True))
self.disarm_button.clicked.connect(lambda: self.Arm(False))
self.srv = rospy.ServiceProxy("/mavros/cmd/arming", CommandBool)
lyt = QtGui.QHBoxLayout()
lyt.setAlignment(QtCore.Qt.AlignRight)
lyt.addWidget(self.disarm_button)
lyt.addWidget(self.arm_button)
self.widget_area.setLayout(lyt)
def Arm(self, arm):
srv_msg = CommandBoolRequest()
srv_msg.value = arm
print self.srv(srv_msg)
class StatusToolbar(TurquoiseWidget):
"""docstring for StatusToolbar."""
def __init__(self):
super(StatusToolbar, self).__init__()
self.lyt = QtGui.QGridLayout()
self.a = ArmControlWidget()
self.lyt.addWidget(self.a)
# self.lyt.addWidget(ArmControlWidget())
self.widget_area.setLayout(self.lyt)
In this case the TurquoiseWidget is the base widget (every base widget has a frame and a title label in it.), ArmControlWidget is a widget performs spesific task and the StatusToolbar is the toolbar object, which has a set of widgets inside of it that derives from TurquoiseWidget or base widget.
So when I just do some_widget = ArmControlWidget() some_widget.show() it works perfectly. But when I create this widget inside of toolbar widget and display it in there, nothing shows.
Sorry if I'm not clear. Thanks in advance!
解决方案
没有孩子的小部件的最小尺寸是多少?您可以非常小,当您将其设置为固定尺寸时,即使您有其他小部件或布局,小部件的尺寸也会很小。所以解决的办法就是去掉这一行self.setFixedSize(self.minimumSize())
,加到StatusToolbar
self.setFixedSize(self.sizeHint())
.
class TurquoiseWidget(QtGui.QWidget):
def __init__(self, title="Widget", enable_title=False, font_size=9):
super(TurquoiseWidget, self).__init__()
# self.setFixedSize(self.minimumSize()) delete this line
self.widget_area = QtGui.QFrame()
# ...
# ...
class StatusToolbar(TurquoiseWidget):
"""docstring for StatusToolbar."""
def __init__(self):
super(StatusToolbar, self).__init__()
self.lyt = QtGui.QGridLayout()
self.a = ArmControlWidget()
self.lyt.addWidget(self.a)
self.widget_area.setLayout(self.lyt)
self.setFixedSize(self.sizeHint()) # add this line
推荐阅读
- php - 419 页面在 laravel 7 中已过期
- spring-boot - SpringBoot - *_max *_count *_sum 指标的可观察性
- javascript - Datatable AJAX 数据参数不刷新
- python - python中的矩阵乘法与分类
- java - 如何更改 android studios jdk 版本(特别是 jdk 8)
- css - Bootstrap 4网格未对齐
- sql - 使用 OPTION (MAXDOP 1) 来减少 SQL Server 中的并行性是否安全?
- uml - UML 序列图:如何绘制创建对象数组?
- react-native - 堆栈导航器标题按钮在按下时不会导航到路由
- nestjs - Nestjs with pm2, port 在用 pm2 stop 杀死进程后仍然可以使用?