博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
pyqt5自定义控件不显示的问题
阅读量:4096 次
发布时间:2019-05-25

本文共 2824 字,大约阅读时间需要 9 分钟。

最近在学习pyqt5编程,需要使用自定义控件,定义了一个setGroup的类,继承自QWidget。在AutoMainWin类中实例化setGroup类,期望在QMainWindow上显示setGroup的内容,发现自定义的类setGroup并不能显示。把setGroup类作为主窗口在__name__ == '__main__'中实例化,发现也不能正常显示。确定是自定义控件的问题,而不是放在AutoMainWin中才不能显示的。排查setGroup类内容,最终发现是因为缺少了self.setLayout(self.Hbox)。附代码如下:

 

​#!import sysimport serialfrom PyQt5.QtWidgets import *from PyQt5.QtCore import *from PyQt5.QtGui import *class SetWin(QWidget):    def __init__(self):        super(SetWin, self).__init__()        self.initUI()    def initUI(self):        comNum = ['Com1', 'Com2', 'Com3', 'Com4']        self.setGeometry(200, 200, 100, 150)        self.setWindowTitle('Set Com Port')        self.sltPortLB = QLabel('Select Com Port')        self.sltPort = QComboBox()        self.sltPort.addItems(comNum)        vbox = QVBoxLayout()        vbox.addWidget(self.sltPortLB)        vbox.addWidget(self.sltPort)        vbox.addStretch(1)        self.setLayout(vbox)        self.setWindowModality(Qt.ApplicationModal)        self.show()        print('init setwin')class setGroup(QWidget):    def __init__(self):        super(setGroup, self).__init__()        self.grp = QGroupBox()        self.grp.setTitle('Set Com Port')        self.sltlb = QLabel('Select Port')        self.startbtn = QPushButton('Start')        self.combx = QComboBox()        self.combx.addItem('Com1')        self.HboxSlt = QHBoxLayout()        self.HboxSlt.addWidget(self.sltlb)        self.HboxSlt.addWidget(self.combx)        self.HboxSlt.addStretch(1)        self.Vbox = QVBoxLayout()        self.Vbox.addLayout(self.HboxSlt)        self.Vbox.addWidget(self.startbtn)        self.grp.setLayout(self.Vbox)        self.Hbox = QHBoxLayout()        self.Hbox.addWidget(self.grp)        self.Hbox.addStretch(1)        self.setLayout(self.Hbox)        print('set group')class AutoMainWin(QMainWindow):    def __init__(self):        super(AutoMainWin, self).__init__()        self.initUI()    def initUI(self):        self.setGeometry(100, 100, 640, 480)        self.setWindowTitle('test tool')        comAction = QAction(QIcon('set.png'), 'Set Com', self)        comAction.setShortcut('Ctrl+T')        comAction.triggered.connect(self.openSetWin)        self.toolbar = self.addToolBar('Set')        self.toolbar.addAction(comAction)        self.toolbar.setIconSize(QSize(42, 36))        lb = QLabel('name1')        setgrp = setGroup()        self.Vbox = QVBoxLayout()        self.Vbox.addWidget(setgrp)        self.Vbox.addWidget(lb)        self.Vbox.addStretch(1)        #self.setLayout(self.Vbox)        wgt = QWidget()        wgt.setLayout(self.Vbox)        self.setCentralWidget(wgt)    def openSetWin(self):        self.setwin = SetWin()        print('set window')if __name__ == '__main__':    app = QApplication(sys.argv)    gAutoMain = AutoMainWin()    gAutoMain.show()    sys.exit(app.exec_())​

 

转载地址:http://zjmii.baihongyu.com/

你可能感兴趣的文章
卧槽!Java 虚拟机竟然还有这些性能调优技巧...
查看>>
听说玩这些游戏能提升编程能力?
查看>>
7 年工作经验,面试官竟然还让我写算法题???
查看>>
被 Zoom 逼疯的歪果仁,造出了视频会议机器人,同事已笑疯丨开源
查看>>
上古语言从入门到精通:COBOL 教程登上 GitHub 热榜
查看>>
再见,Eclipse...
查看>>
超全汇总!B 站上有哪些值得学习的 AI 课程...
查看>>
如果你还不了解 RTC,那我强烈建议你看看这个!
查看>>
神器面世:让你快速在 iOS 设备上安装 Windows、Linux 等操作系统!
查看>>
沙雕程序员在无聊的时候,都搞出了哪些好玩的小玩意...
查看>>
太赞了!GitHub 标星 2.4k+,《可解释机器学习》中文版正式开放!
查看>>
程序员用 AI 修复百年前的老北京视频后,火了!
查看>>
漫话:为什么你下载小电影的时候进度总是卡在 99% 就不动了?
查看>>
我去!原来大神都是这样玩转「多线程与高并发」的...
查看>>
当你无聊时,可以玩玩 GitHub 上这个开源项目...
查看>>
B 站爆红的数学视频,竟是用这个 Python 开源项目做的!
查看>>
安利 10 个让你爽到爆的 IDEA 必备插件!
查看>>
自学编程的八大误区!克服它!
查看>>
GitHub 上的一个开源项目,可快速生成一款属于自己的手写字体!
查看>>
早知道这些免费 API,我就可以不用到处爬数据了!
查看>>