I've made a custom ui form for entering data in a GIS database built with PostgreSQL and QGIS. This is a relational database and I use the Relations properties in QGIS to implement this relations (one-to-many or many-to-many relations).

Currently, my many to many relations are shown in the custom form via a QWidget with a dynamic property set to qgisRelation. This display the form of the child table in my parent table. However, I would like to have, for certain relations, a much simpler way of displaying the child table, i.e. without the sidebar and the standard buttons (add/delete child, link/unlink child feature etc.) that comes from QGIS. I'd rather have a simple table that shows just one field (the ID) of my child table with the possibility to populate the table.

Is it possible to this with QTableView or QTableWidget? Do I have to set dynamic properties and maybe add some Python code in init function to make it work?


You can indeed create a table of a single row that may do what you want with a QTableWidget

You should do something like that :

  1. create a QTableWidget

  2. create 2 QPushButton (add and delete for exemple)

  3. then do you request and put your ouput in a python list for exemple.

  4. iterate over your python list and create QTableWidgetItem with the content as text of your list http://doc.qt.io/archives/qt-4.8/qlistwidgetitem.html and add this QTableWidgetItem in your QTableWidget

  5. connect your button with some event to function that delete or add QTableWidgetItem along with your list.

I give you some code, I didn't test it but it should look like this if I try to do a sample of things I did for the interface of a plug-in python.

Also, go check documentation ;) http://doc.qt.io/qt-5/qtablewidget.html

code :


result = ['id1','id2','id3']

self.table = QListWidget()

for i in range (self.table.rowCount()):
    #insert the data in the table
    label = QTableWidgetItem()
def add(self) :
    #ask for a text or add a raw QTableWidgetItem
def delete(self) :
    # delete a QTableWidgetItem

I did something like this, this is a QTableWidget with some button to deal with it.

if you use QT designer you can drop some of the code and prepare your table with the .ui file too

You can also check QListWidget if you only need one column , could be easier to do.

give you this kind of thing

Good luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.