gpt4 book ai didi

python - QtDesigner 更改将在重新设计用户界面后丢失

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:29 24 4
gpt4 key购买 nike

我正在使用 Qt Designer 设计 GUI 在 python 中使用,在 Qt Designer 中设计我想要的 UI 之后,将其转换为 python 代码然后我更改生成的代码以在我的 python 代码中执行一些操作,但是如果我更改使用 Qt Designer 的 UI 并再次将其转换为 Python 代码,我丢失了之前对代码所做的更改。

我该如何解决这个问题?

我们可以在 python 中将一个类分布到多个文件中以在其他文件中编写代码吗?

最佳答案

为避免出现这些问题,建议不要修改此文件,而是创建一个新文件,我们在其中实现使用该设计的类。

例如,假设您在design.ui 文件中使用了MainWindow 模板,然后将其转换为Ui_Design.py 之类的以下结构:

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
def setupUi(self, MainWindow):
[...]

def retranslateUi(self, MainWindow):
[...]

然后我们将创建一个名为 logic.py 的新文件,我们将在其中创建处理逻辑并使用先前设计的文件:

class Logic(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)

所以即使你修改了设计,重新生成文件.py,你也不必修改逻辑的文件。

为了概括这个想法,我们必须有以下规则,但为此逻辑类必须有以下结构:

class Logic(PyQtClass, DesignClass):
def __init__(self, *args, **kwargs):
PyQtClass.__init__(self, *args, **kwargs)
self.setupUi(self)
  • PyQtClass:这个类取决于所选的设计。

Template PyQtClass
─────────────────────────────────────────────
Main Window QMainWindow
Widget QWidget
Dialog with Buttons Bottom QDialog
Dialog with Buttons Right QDialog
Dialog with Without Buttons QDialog
  • DesignClass: The name of the class that appears in your design.

The advantage of this implementation is that you can implement all the logic since it is a widget, for example we will implement the solution closing pyqt messageBox with closeevent of the parent window :

class Logic(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
def closeEvent(self, event):
answer = QtWidgets.QMessageBox.question(
self,
'Are you sure you want to quit ?',
'Task is in progress !',
QtWidgets.QMessageBox.Yes,
QtWidgets.QMessageBox.No)
if answer == QtWidgets.QMessageBox.Yes:
event.accept()
else:
event.ignore()

关于python - QtDesigner 更改将在重新设计用户界面后丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46544780/

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