user3501179
user3501179

Reputation: 43

How to edit/add data in a Form in PyQt?? (Python)

Using the code below, i have created a form to hold different questions to be used to select and add to a test. I wanted to know how i would add data (Questions) to the form (the top box) (Assuming that i wanted to add a single question which is a named variable 'Question') how would i go about doing this??? Also, although i know how to add data (by data i mean text) to the bottom box, Is there some sort of a way where i could select a piece of text in the top box and view an expansion of the text/variable in the bottom box without having to click on any button???

The code is as follows:

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

class AddTst(QtGui.QWidget):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(464, 409)
        self.verticalLayout_2 = QtGui.QVBoxLayout(Form)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.listView = QtGui.QListView(Form)
        self.horizontalLayout.addWidget(self.listView)
        self.verticalLayout = QtGui.QVBoxLayout()
        self.EditButton = QtGui.QPushButton(Form)
        self.verticalLayout.addWidget(self.EditButton)
        self.DeleteButton = QtGui.QPushButton(Form)
        self.verticalLayout.addWidget(self.DeleteButton)
        spacerItem = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacerItem)
        self.AddButton = QtGui.QPushButton(Form)
        self.verticalLayout.addWidget(self.AddButton)
        self.horizontalLayout.addLayout(self.verticalLayout)
        self.verticalLayout_2.addLayout(self.horizontalLayout)
        self.textBrowser = QtGui.QTextBrowser(Form)
        self.verticalLayout_2.addWidget(self.textBrowser)
        self.textBrowser.setPlainText('Hello')

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        self.EditButton.setText(QtGui.QApplication.translate("Form", "Edit", None, QtGui.QApplication.UnicodeUTF8))
        self.DeleteButton.setText(QtGui.QApplication.translate("Form", "Delete", None, QtGui.QApplication.UnicodeUTF8))
        self.AddButton.setText(QtGui.QApplication.translate("Form", "Add", None, QtGui.QApplication.UnicodeUTF8))

class AddTest(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = AddTst()
        self.ui.setupUi(self)
    def execute_event(self):
        pass
    def execute_all_event(self):
        pass
    def reload_event(self):
        pass

def main():
    app = QtGui.QApplication(sys.argv)
    ex = AddTest()
    ex.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Upvotes: 1

Views: 1422

Answers (1)

ekhumoro
ekhumoro

Reputation: 120678

To answer the specific question about how to link list-items with the text-box: you should connect to an appropriate signal so that when the current list-item changes, the text-box gets populated.

But before dealing with this issue, there is something more important to deal with. It seems that you have created your form uing Qt Designer and then generated a gui module using pyuic. However, you have then made the mistake of directly editing this module. This is a bad idea, because when you make changes to the form, you will lose everything when you have to re-generate the gui module.

The correct approach is to import the gui module into your main application, like this:

from PyQt4 import QtCore, QtGui
from mainwindow_ui import Ui_MainWindow

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.listWidget.currentRowChanged.connect(self.handleRowChanged)

    def handleRowChanged(self, row):
        item = self.ui.listWidget.item(row)
        # do stuff with list-item...

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

This assumes that you have generated a gui file called mainwindow_ui.py and your main application script is saved in the same directory. Also note that I have used a QListWidget rather than a QListView, and a QMainWindow rather than a QWidget. I would suggest you make similar changes, as it will simplify the development of your application.

A few other things to think about:

  • You are currently using a text-browser to display content, but this widget is read-only. A QTextEdit might be a better choice.
  • There is currently no way to add/edit the name/title of each item. You could add a QLineEdit above the text-box for this.
  • What is the format of the source data, and how do you load/save it? If you haven't got this part worked out yet, you should do so as soon as possible, because it could have a big influence on the overall design of your application.

Upvotes: 1

Related Questions