1

I have created a basic form layout using splitters QListWidget and QTextBrowser. I wanted to know how i could import data from an SQL Database (or just add simple text i.e. String) to the ListWidget Boxes. Also once the user clicks on a piece of data within the ListWidget Box, how could i show (in detail) an expansion of the data in the TextBrowser box?? Also, how would i fix the splitter, so that the user cannot move, adjust the splitters??

The code is below:

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_())
1
  • What sql library are you using ? Commented Apr 6, 2014 at 12:19

1 Answer 1

1

Well what you require is QSqlTableModel , and use ListView rather than ListWidget. So you just load the model with the database file and connect the model to the ListView. Then in listView Set the Model Column that you want to display in the View, using the setModelColumn() function.

Here is an example of how you can do it.

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())
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Forgive me, im new to PyQt, but, how would i integrate the Object class with say a MainWindow class or a QWidget class. When typing in window = Ui_Form(), window.show() i get an error?? Is that because the Ui_Form has to be integrated with a MainWindow?? or QWidget??
instead of window.show() try using window.exec_() . I Think that may solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.