gpt4 book ai didi

python - Qt QDialog渲染堆叠

转载 作者:行者123 更新时间:2023-11-28 17:43:26 30 4
gpt4 key购买 nike

这些是我在 Qt 中的第一步。我正在尝试像本教程中那样构建简单的文本编辑器:http://www.rkblog.rk.edu.pl/w/p/simple-text-editor-pyqt4/

因此,我在 Qt Designer 中创建了设计。这是预览:

QtDesignerPreview

完美:)

这里有 Qt 对象和类,可以清楚地说明布局是如何构建的:

QtObjCls

我已经编译了 .py 文件:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'G:\Google Drive\py_scripts\QTUI\simpleTextEditor_gui.ui'
#
# Created: Fri Jan 24 20:06:32 2014
# by: PyQt4 UI code generator 4.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)

class Ui_simpleTextEditor(object):
def setupUi(self, simpleTextEditor):
simpleTextEditor.setObjectName(_fromUtf8("simpleTextEditor"))
simpleTextEditor.resize(524, 413)
self.gridLayout = QtGui.QGridLayout(simpleTextEditor)
self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
self.verticalLayout = QtGui.QVBoxLayout()
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.editorWindow = QtGui.QTextEdit(simpleTextEditor)
self.editorWindow.setObjectName(_fromUtf8("editorWindow"))
self.verticalLayout.addWidget(self.editorWindow)
self.horizontalLayout = QtGui.QHBoxLayout()
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.buttonOpen = QtGui.QPushButton(simpleTextEditor)
self.buttonOpen.setObjectName(_fromUtf8("buttonOpen"))
self.horizontalLayout.addWidget(self.buttonOpen)
self.buttonClose = QtGui.QPushButton(simpleTextEditor)
self.buttonClose.setObjectName(_fromUtf8("buttonClose"))
self.horizontalLayout.addWidget(self.buttonClose)
self.verticalLayout.addLayout(self.horizontalLayout)
self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)

self.retranslateUi(simpleTextEditor)
QtCore.QObject.connect(self.buttonClose, QtCore.SIGNAL(_fromUtf8("clicked()")), simpleTextEditor.close)
QtCore.QMetaObject.connectSlotsByName(simpleTextEditor)

def retranslateUi(self, simpleTextEditor):
simpleTextEditor.setWindowTitle(_translate("simpleTextEditor", "Simple Text Editor", None))
self.buttonOpen.setText(_translate("simpleTextEditor", "Open", None))
self.buttonClose.setText(_translate("simpleTextEditor", "Close", None))

并创建了我非常简单的应用程序:

import sys
from PyQt4 import QtCore, QtGui
from simpleTextEditor_gui import Ui_simpleTextEditor

class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_simpleTextEditor()
self.ui.setupUi(self)
# tutaj dajemy wlasne polaczenia slotow
QtCore.QObject.connect(self.ui.buttonOpen,QtCore.SIGNAL("clicked()"), self.file_dialog)
def file_dialog(self):
fd = QtGui.QFileDialog(self)
self.filename = fd.getOpenFileName()
from os.path import isfile
if isfile(self.filename):
text = open(self.filename).read()
self.ui.editorWindow.setText(text)

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())

现在,当我运行该应用程序时,所有对象都堆叠在一起,如下所示:

ExecutedApp

感谢任何帮助!

最佳答案

代码的主要问题是您为 StartQT4 子类使用了错误的基类。它应该与 Qt Designer 中的顶级类相匹配,即 QDialog

您还可以通过将 ui 直接添加到您的子类并使用 new-style signal and slot syntax 来稍微简化您的代码。 .

完成这些更改后,您的代码将如下所示:

import sys
from PyQt4 import QtCore, QtGui
from simpleTextEditor_gui import Ui_simpleTextEditor

class StartQT4(QtGui.QDialog, Ui_simpleTextEditor):
def __init__(self, parent=None):
QtGui.QDialog.__init__(self, parent)
self.setupUi(self)
# tutaj dajemy wlasne polaczenia slotow
self.buttonOpen.clicked.connect(self.file_dialog)

def file_dialog(self):
fd = QtGui.QFileDialog(self)
self.filename = fd.getOpenFileName()
from os.path import isfile
if isfile(self.filename):
text = open(self.filename).read()
self.editorWindow.setText(text)

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())

关于python - Qt QDialog渲染堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21340748/

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