1

I'm trying to get to know how to make my own widgets to be used in the Edit Widget. I have looked at this plugin here and it is very usefull: https://github.com/elpaso/qgis-formawarevaluerelationwidget

I'm trying to make a custom widget which will have a combobox and a few small buttons (like flash feature, zoom to, pan to)

In the example i have created from the form aware relation widget i can succesfully create a combobox.

class FormToolsRelationWidgetWrapper(QgsEditorWidgetWrapper):

    def __init__(self, vl, fieldIdx, editor, parent):
        """
        QgsVectorLayer* vl, int fieldIdx, QWidget* editor, QWidget* parent
        """
        some init stuff here...



    def createWidget(self, parent):
        #self.mComboBox = TestWidget(parent)
        #self.mComboBox = TestWidget()
        self.mComboBox = QComboBox(parent)
        QObject.connect( self.mComboBox, SIGNAL( "currentIndexChanged( int )" ), self, SLOT( "valueChanged()" ) )
        return self.mComboBox

The result is this:

combobox looks ok

Looks very good. And i know i have not yet populated the combobox with data.

If i instead activate a widget i have created with this code (with TestWidget(parent) ):

class TestWidget(QWidget):
    def __init__(self, parent=None):
        super(TestWidget, self).__init__(parent)

        # create a combobox
        combobox_1 = QComboBox(self)
        combobox_1.addItem("Item 1")

        # create a combobox with a parent widget
        combobox_2 = QComboBox(self)
        combobox_2.addItem("Item A")
        combobox_2.addItem("Item B")
        combobox_2.addItem("Item C")

        # layout
        layout = QHBoxLayout(self)
        layout.addWidget(combobox_1)
        layout.addWidget(combobox_2)
        layout.addStretch()

        # sets the layout
        self.setLayout(layout)

I get his result: Misplaced comboboxes

I'm not sure what is wrong. I have test by not assigning QComboBox and QHBoxLayout to self. Without any luck.​ Here the widget does not replace the ID field. i thought this would be replaced with my widget. Like the example with qcombobox.

Any suggestions to make this work?

I can not find any examples/tutorials explaning this. Maybe if it get this sorted i will make a small introduction to this.

  • QgsEditorWidgetWrapper has a valid function which needs to return true, else the widget will not be shown in the correct place. This solved my problem. – Vittrup Jul 14 '17 at 6:58
1

QgsEditorWidgetWrapper has a valid function which needs to return true, else the widget will not be shown in the correct place. This solved my problem.

eg.

def valid(self):
    return  isinstance(self.editor, QComboBox) or \
            isinstance(self.editor, QListWidget) or \
            isinstance(self.editor, QLineEdit) or \
            isinstance(self.editor, QWidget)

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.