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?
Best Answer
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 :
create a
QTableWidget
create 2
QPushButton
(add and delete for exemple)then do you request and put your ouput in a python list for exemple.
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 thisQTableWidgetItem
in yourQTableWidget
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 :
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.Good luck