qwidget:cannot creat a qwidget without qapplication

遇到这种问题一般两种情况:

  1. 有GUI界面:你的代码中用了QWidget的类,但是你main函数中用的不是QApplication,这个时候只需要将QGuiApplication或者QCoreApplication改为QApplication即可。产生这个错误的原因是这个QWidget 是建立在QApplication 上的。
  2. 无GUI界面:这种情况的应用程序肯定是要用QCoreApplication类的,不然后续有新的问题找不到display。所以肯定是你的代码中用了QWidget的类或者继承自QWidget的类,找到并将其删除即可。

QApplication、QGuiApplication、QCoreApplication三者之间的关系:

  1. QCoreApplication用于non-GUI的应用程序(不需要依赖QtGui库),QApplication用于包含GUI的应用程序(需要用到QtGui库)。
  2. QApplication继承了QGuiApplication类,而QGuiApplication继承了QCoreApplication类,而QCoreApplication又继承QObject的,而QObject就是QT中最基本的基类,也就是QT的根基。

QApplication的作用:

QApplication 类管理GUI程序的控制流和主设置。QApplication 包含主事件循环,所有来自窗口系统和其他源的事件将被处理和分配。它也处理程序的初始化,析构和提供会话管理。对于非GUI的用QCoreApplication 代替QApplication,它不依赖QtGui库。qApp是一个全局的指针,指向QApplication的对象。

from PySide6.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QCheckBox, QPushButton, QTableWidgetItem, \ QTableWidget import sys from PySide6.QtCore import Qt class MyWidget(QWidget): def init(self): super().init() self.initUI() def initUI(self): # 设置窗口大小和标题 self.setGeometry(300, 300, 500, 300) self.setWindowTitle('My Widget') # 创建组件 self.checkbox1 = QCheckBox('Option 1') self.checkbox2 = QCheckBox('Option 2') self.checkbox3 = QCheckBox('Option 3') self.checkbox4 = QCheckBox('Option 4') self.checkbox5 = QCheckBox('Option 5') self.pushbutton = QPushButton('Create Table') self.pushbutton.clicked.connect(self.createTable) # 创建布局 hbox1 = QHBoxLayout() hbox1.addWidget(self.checkbox1) hbox1.addWidget(self.checkbox2) hbox1.addWidget(self.checkbox3) hbox2 = QHBoxLayout() hbox2.addWidget(self.checkbox4) hbox2.addWidget(self.checkbox5) hbox3 = QHBoxLayout() hbox3.addWidget(self.pushbutton) vbox = QVBoxLayout() vbox.addLayout(hbox1) vbox.addLayout(hbox2) vbox.addLayout(hbox3) self.setLayout(vbox) def createTable(self): is_checked_1 = self.checkbox1.isChecked() is_checked_2 = self.checkbox2.isChecked() is_checked_3 = self.checkbox3.isChecked() is_checked_4 = self.checkbox4.isChecked() is_checked_5 = self.checkbox5.isChecked() # 先清空之前的表格 for i in reversed(range(self.layout().count())): widgetToRemove = self.layout().itemAt(i).widget() if widgetToRemove: self.layout().removeWidget(widgetToRemove) widgetToRemove.setParent(None) # 获取勾选项的数量 rows = 0 if is_checked_1: rows += 1 if is_checked_2: rows += 1 if is_checked_3: rows += 1 columns = 2 # 创建表格 tableWidget = QTableWidget() tableWidget.setRowCount(rows) tableWidget.setColumnCount(columns) tableWidget.setHorizontalHeaderLabels(['Column 1', 'Column 2']) # 添加表格到布局中 self.layout().addWidget(tableWidget) if name == 'main': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec());以上代码中,在creatTable环节,需要根据checkbutton4和chechbutton5的勾选数量情况,动态添加对应数量的tablewidget,当无勾选时,不添加表格,勾选checkbutton4时,填加一个编号为4的tablewidgr=et,勾选checkbutton5时,填加一个编号为5的tablewidgr=et.帮我改一下
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值