When I first used pyqt5, the following errors occurred during my operation:
pyqt5 ‘QWidget’ object has no attribute ‘setCentralWidget’
I used a method to solve this error:
In the running main function, it was originally like this
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widgets = QtWidgets.QWidget()
ui = MainWin()
ui.main_ui.setupUi(widgets)
widgets.show()
ui.run_function()
sys.exit(app.exec_())
Then modify it as follows:
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
widgets = QtWidgets.QMainWindow()
ui = MainWin()
ui.main_ui.setupUi(widgets)
widgets.show()
ui.run_function()
sys.exit(app.exec_())
In addition to this method, there are several other methods on stack overflow that I don’t know if they are feasible:
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
ui = Ui_MainWindow()
ui.setupUi(self)
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent=parent)
self.setupUi(self)
import sys
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
Similar Posts:
- QWidget:Must construct a QApplication before a QPaintDevice
- QT use of undeclared identifier ‘cout’
- Python Typeerror: super() takes at least 1 argument (0 given) error in Python
- Python: __ new__ Method and the processing of typeerror: object() takes no parameters
- ModuleNotFoundError: No module named ‘PyQt4’ [Spyder Import matplotlib Error]
- Solving the problem of QT connecting MySQL database in Windows system: qmmysql driver not loaded
- [Solved] Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
- QApplication:No such file or directory [How to Solve]
- The whole program exits when [QT] qmessagebox is closed
- [Solved] ReactNative Warning: Can’t perform a React state update on an unmounted component