gpt4 book ai didi

python - 我在 pyqt5 中遇到错误,同时连接按钮

转载 作者:太空狗 更新时间:2023-10-30 02:52:20 25 4
gpt4 key购买 nike

我是第一次使用 pyqt5。从 QT 设计器创建 UI,然后将 UI 文件转换为 python 文件后,出现错误,奇怪的是,有时当我尝试从 Designer 重新创建 UI 时,我没有收到错误,但有时会出现相同的错误逐行代码,我收到此错误。也许我在qt设计器中做错了什么,我不知道错在哪里。

QObject::connect: Cannot connect QPushButton::clicked(bool) to (null)::browse_slot()

TypeError: connect() failed between clicked(bool) and browse_slot()

我的代码:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(280, 200, 89, 25))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked.connect(self.browse_slot)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))

@QtCore.pyqtSlot( )
def browse_slot( self ):
options = QtWidgets.QFileDialog.Options()
options |= QtWidgets.QFileDialog.DontUseNativeDialog
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
None,
"QFileDialog.getOpenFileName()",
"",
"All Files (*);;Python Files (*.py)",
options=options)
if fileName:
self.debugPrint( "setting file name: " + fileName )
self.model.setFileName( fileName )
self.refreshAll()

if __name__ == "__main__":
import sys
app = QApplication(sys.argv)
Window = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(Window)
Window.show()
sys.exit(app.exec_())

最佳答案

如果您想使用@QtCore.pyqtSlot() 建立连接,您必须在继承自QObject 的类中使用它,另一方面the PyQt docs表明建议您创建一个继承自适当小部件的类,在本例中为 QMainWindow 并将该类用作接口(interface),这也解决了第一点,因为 QMainWindow 继承自 QObject。

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(280, 200, 89, 25))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))

class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.pushButton.clicked.connect(self.browse_slot)

@QtCore.pyqtSlot()
def browse_slot(self):
options = QtWidgets.QFileDialog.Options()
options |= QtWidgets.QFileDialog.DontUseNativeDialog
fileName, _ = QtWidgets.QFileDialog.getOpenFileName(
None,
"QFileDialog.getOpenFileName()",
"",
"All Files (*);;Python Files (*.py)",
options=options)
if fileName:
self.debugPrint( "setting file name: " + fileName )
self.model.setFileName( fileName )
self.refreshAll()

if __name__ == "__main__":
import sys
app =QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

关于python - 我在 pyqt5 中遇到错误,同时连接按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53520225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com