gpt4 book ai didi

python - 如何向创建的表单添加数据? PyQt/Python/SQL

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

我使用分割器 QListWidget 和 QTextBrowser 创建了一个基本的表单布局。我想知道如何从 SQL 数据库导入数据(或仅添加简单文本,即字符串)到 ListWidget 框中。此外,一旦用户单击 ListWidget 框中的一条数据,我如何(详细地)显示 TextBrowser 框中数据的扩展?另外,我该如何修复分离器,使用户无法移动,调整分离器?

代码如下:

import sys, random, time, math, re
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.grouplist = QtGui.QListWidget()
self.messageList = QtGui.QListWidget()
self.MessageView = QtGui.QTextBrowser()
self.messageSplitter = QtGui.QSplitter(QtCore.Qt.Vertical)
self.messageSplitter.addWidget(self.messageList)
self.messageSplitter.addWidget(self.MessageView)
self.mainSplitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.mainSplitter.addWidget(self.grouplist)
self.mainSplitter.addWidget(self.messageSplitter)
self.setCentralWidget(self.mainSplitter)


self.mainSplitter.setStretchFactor(0, 1)
self.mainSplitter.setStretchFactor(1, 3)
self.messageSplitter.setStretchFactor(0 ,1)
self.messageSplitter.setStretchFactor(1, 2)
def closeEvent(self, event):
if self.okToContinue():
settings = QtGui.QSettings()
settings.setValue("MainWindow/Size", QVariant(self.size()))
settings.setValue("MainWindow/Position", QVariant(self.pos()))
settings.setValue("MainWindow/State", QVariant(self.saveState()))
settings.setValue("MessageSplitter", QVariant(self.messageSplitter.saveState()))
settings.setValue("MainSplitter", QVariant(self.manSplitter.saveState()))
else:
event.ignore()

if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

最佳答案

那么您需要的是 QSqlTableModel ,并使用 ListView 而不是 ListWidget 。因此,您只需使用数据库文件加载模型并将模型连接到 ListView。然后在listView中使用setModelColumn()函数设置要在View中显示的模型列。

以下是如何执行此操作的示例。

from PyQt4 import QtCore, QtGui,QtSql
import os

try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
CONFIG_DATABASE_PATH = "./"
CONFIG_DATABASE_NAME = "comboboxexample.db"

class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(640, 480)
filename = os.path.join(CONFIG_DATABASE_PATH,CONFIG_DATABASE_NAME)
self.db = QSqlDatabase.addDatabase("QSQLITE")
self.db.setDatabaseName(filename)
self.model = QtSql.QSqlTableModel(self, self.db)
self.model.setTable('items') # enter the table name
self.model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
self.splitter = QtGui.QSplitter(Form)
self.splitter.setGeometry(QtCore.QRect(10, 30, 621, 441))
self.splitter.setOrientation(QtCore.Qt.Horizontal)
self.splitter.setObjectName(_fromUtf8("splitter"))
self.listView = QtGui.QListView(self.splitter)
self.listView.setObjectName(_fromUtf8("listView"))
self.listView.setModel(self.model)

# enter the column number which you want to display
self.listView.setModelColumn(1)

self.textEdit = QtGui.QTextEdit(self.splitter)
self.textEdit.setObjectName(_fromUtf8("textEdit"))

self.retranslateUi(Form)
QtCore.QObject.connect(self.listView, QtCore.SIGNAL("clicked(QModelIndex)"),self.ListClicked_2)
QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))

def ListClicked_2( self, qmodelindex ):
# then take the data from the model and display it in the textEdit
self.textEdit.setText(qmodelindex.data(QtCore.Qt.DisplayRole).toString())

关于python - 如何向创建的表单添加数据? PyQt/Python/SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22893622/

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