I am trying to write a plugin with a input control such as textbox on it. I looked around quite a bit for building plugins. But I can only find examples of how to build a plugin with buttons/icons in its toolbar.
How can I add other widgets such as a textbox on my plugin's toolbar?
For the button, there isn't much to handle except for a callback when it's clicked. I imagine it would be quite different for a textbox as one needs to associate the input control to e.g. a global variable??
For now, I need interaction with the textbox (QLineEdit
). I want to implement a plugin for going to the next feature where I can display the row number of the current record, and jump to any given row by entering its row number.
I am using QGIS2.99 and Qt5, the standard Plugin Builder
and pb_tool
to create
and deploy
the plugin template. I suppose I need to add the QLineEdit
in initGui()
of the main py
file(as shown below). I don't know much about Qt5, and only figured I might need addUserInputWidget()
and/or connect it with the add_action()
function? But I don't know how to put the pieces together, and register the events etc.
Here is what I have so far:
def initGui(self):
"""Create the menu entries and toolbar icons inside the QGIS GUI."""
# Create & add a textbox
self.textbox = QLineEdit(self.iface.mainWindow())
self.textbox.resize(80,20)
self.txtAction = self.toolbar.addWidget(self.textbox)
#should I use addUserInputWidget(self.textbox) #??
self.txtAction.setToolTip(self.tr(u'Current Row Number'))
#callback=self.runTextChange how to register an event handler?
The UI shows but there is no interaction.
Can someone help explain how to link the QLineEdit
to the toolbar so that I can get/change the text value?
Also, a minor issue I can't figure out is that the textbox takes up the full width of the toolbar no matter what I use in its .resize()
method. How should I limit its width?
Best Answer
Nice answer by @mgri (don't delete it as it is useful to use within QGIS!). I tested the following on QGIS 2.18 also with Qt4 but perhaps it might be useful too as the other answer. A couple of things to note:
self.textbox.setFixedWidth(80)
to set the width of the textbox.self.textbox.textChanged.connect(self.runTextChange)
to connect the textbox to a function whenever the text is changed.Here is the code used:
Example: