3

enter image description here

I need to create a custom table attribute inside a plugin where I can choose which layer I need to open and add new custom buttons to this custom table attribute

To become like this :

enter image description here

Vince
  • 20,017
  • 15
  • 45
  • 64
lackti
  • 31
  • 1

1 Answers1

2

Have a look at QgsAttributeTableView and QgsAttributeTableModel classes. These are used by QGIS to display the attribute table.

I've put together a small example script which shows the attribute table of a vector layer in a small custom dialog. Instead of the label you can insert any desired other elements into the layout.

class MyWindow(QDialog):
def __init__(self, parent, canvas, layer):
    super().__init__(parent)

    layout = QVBoxLayout()
    label = QLabel('Custom Widget')
    layout.addWidget(label)

    self.tableView = QgsAttributeTableView(self)
    self.layerCache = QgsVectorLayerCache(layer, layer.featureCount())
    self.tableModel = QgsAttributeTableModel(self.layerCache)
    self.tableModel.loadLayer()

    self.tableFilterModel = QgsAttributeTableFilterModel(canvas, self.tableModel, parent=self.tableModel)
    self.tableFilterModel.setFilterMode(QgsAttributeTableFilterModel.ShowAll)
    self.tableView.setModel(self.tableFilterModel)

    layout.addWidget(self.tableView)
    self.setLayout(layout)

layer = iface.activeLayer() dlg = MyWindow(iface.mainWindow(), iface.mapCanvas(), layer) dlg.show()

attr-table

CodeBard
  • 3,511
  • 1
  • 7
  • 20
  • Thanks for your answear , what I want is to replicate the , table attribute and all the actions above the table attribute like the edit mode , save ... – lackti Mar 01 '22 at 15:27
  • 1
    Unfortunately that doesn't seem possible to me. QGIS creates all the actions above the attribute table in QgsAttributeTableDialog class, which is not exposed through the python API. So I guess your only option would be to recreate those actions in python based on the original source file – CodeBard Mar 01 '22 at 16:02