PyQt4 Adding a custom widget derived from another custom widget to layout





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















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!










share|improve this question

























  • provide a Minimal, Complete, and Verifiable example

    – eyllanesc
    Jan 3 at 10:30











  • Here is what I try to do completely.

    – user100170
    Jan 3 at 20:47


















1















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!










share|improve this question

























  • provide a Minimal, Complete, and Verifiable example

    – eyllanesc
    Jan 3 at 10:30











  • Here is what I try to do completely.

    – user100170
    Jan 3 at 20:47














1












1








1








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!










share|improve this question
















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!







python oop inheritance pyqt pyqt4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 20:54







user100170

















asked Jan 3 at 10:14









user100170user100170

83




83













  • provide a Minimal, Complete, and Verifiable example

    – eyllanesc
    Jan 3 at 10:30











  • Here is what I try to do completely.

    – user100170
    Jan 3 at 20:47



















  • provide a Minimal, Complete, and Verifiable example

    – eyllanesc
    Jan 3 at 10:30











  • Here is what I try to do completely.

    – user100170
    Jan 3 at 20:47

















provide a Minimal, Complete, and Verifiable example

– eyllanesc
Jan 3 at 10:30





provide a Minimal, Complete, and Verifiable example

– eyllanesc
Jan 3 at 10:30













Here is what I try to do completely.

– user100170
Jan 3 at 20:47





Here is what I try to do completely.

– user100170
Jan 3 at 20:47












1 Answer
1






active

oldest

votes


















0














What is the minimum size of a widget that has no children? You can be very small and when you set it as a fixed size the widget will have a small size even if you have other widgets or layouts. So the solution is to remove the line self.setFixedSize(self.minimumSize()) and add to the end of the constructor of the 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





share|improve this answer
























  • Okay thanks a lot, actually I must have left it out there, but it didn't cause any problems using widget.show(), it was set to minimum size and that line was before adding push buttons etc, so I never thought that might be the case.

    – user100170
    Jan 3 at 21:30












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020200%2fpyqt4-adding-a-custom-widget-derived-from-another-custom-widget-to-layout%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














What is the minimum size of a widget that has no children? You can be very small and when you set it as a fixed size the widget will have a small size even if you have other widgets or layouts. So the solution is to remove the line self.setFixedSize(self.minimumSize()) and add to the end of the constructor of the 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





share|improve this answer
























  • Okay thanks a lot, actually I must have left it out there, but it didn't cause any problems using widget.show(), it was set to minimum size and that line was before adding push buttons etc, so I never thought that might be the case.

    – user100170
    Jan 3 at 21:30
















0














What is the minimum size of a widget that has no children? You can be very small and when you set it as a fixed size the widget will have a small size even if you have other widgets or layouts. So the solution is to remove the line self.setFixedSize(self.minimumSize()) and add to the end of the constructor of the 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





share|improve this answer
























  • Okay thanks a lot, actually I must have left it out there, but it didn't cause any problems using widget.show(), it was set to minimum size and that line was before adding push buttons etc, so I never thought that might be the case.

    – user100170
    Jan 3 at 21:30














0












0








0







What is the minimum size of a widget that has no children? You can be very small and when you set it as a fixed size the widget will have a small size even if you have other widgets or layouts. So the solution is to remove the line self.setFixedSize(self.minimumSize()) and add to the end of the constructor of the 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





share|improve this answer













What is the minimum size of a widget that has no children? You can be very small and when you set it as a fixed size the widget will have a small size even if you have other widgets or layouts. So the solution is to remove the line self.setFixedSize(self.minimumSize()) and add to the end of the constructor of the 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






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 21:08









eyllanesceyllanesc

87.5k103564




87.5k103564













  • Okay thanks a lot, actually I must have left it out there, but it didn't cause any problems using widget.show(), it was set to minimum size and that line was before adding push buttons etc, so I never thought that might be the case.

    – user100170
    Jan 3 at 21:30



















  • Okay thanks a lot, actually I must have left it out there, but it didn't cause any problems using widget.show(), it was set to minimum size and that line was before adding push buttons etc, so I never thought that might be the case.

    – user100170
    Jan 3 at 21:30

















Okay thanks a lot, actually I must have left it out there, but it didn't cause any problems using widget.show(), it was set to minimum size and that line was before adding push buttons etc, so I never thought that might be the case.

– user100170
Jan 3 at 21:30





Okay thanks a lot, actually I must have left it out there, but it didn't cause any problems using widget.show(), it was set to minimum size and that line was before adding push buttons etc, so I never thought that might be the case.

– user100170
Jan 3 at 21:30




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020200%2fpyqt4-adding-a-custom-widget-derived-from-another-custom-widget-to-layout%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith