Adding textbox to QGIS plugin toolbar?

This would be more a comment than an answer (and most probably a starting point rather than a solution), but I post it here since there is some code.

If you want to set up a basic textbox, you may use this code:

from PyQt4.QtGui import *

input_dialog = QInputDialog()
title = "Dialog Title"
label = "Enter some text here: "
enum_type = QLineEdit.Normal
default_text = "<your text here>"

res = QInputDialog.getText(input_dialog, title, label, enum_type, default_text)

The res variable is a tuple that stores your text (as a string) and a boolean which is used to verify that an input was typed. You can also change the last line in:

text_var, typed = QInputDialog.getText(input_dialog, title, label, enum_type, default_text)

for directly having your text stored in the text_var variable.

Note that this works for Qt4, but I think it should follow the same syntax also for Qt5.


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:

  • You can use self.textbox.setFixedWidth(80) to set the width of the textbox.
  • You can use self.textbox.textChanged.connect(self.runTextChange) to connect the textbox to a function whenever the text is changed.

Here is the code used:

def initGui(self):
    """Create the menu entries and toolbar icons inside the QGIS GUI."""
    # Create & add a textbox
    self.textbox = QLineEdit(self.iface.mainWindow())
    # Set width
    self.textbox.setFixedWidth(80)
    # Add textbox to toolbar
    self.txtAction = self.toolbar.addWidget(self.textbox)
    # Set tooltip
    self.txtAction.setToolTip(self.tr(u'Current Row Number'))
    # Set callback
    self.textbox.textChanged.connect(self.runTextChange)


def runTextChange(self, rownum):
    # Set active layer
    layer = iface.activeLayer()
    # If textbox is empty or 0 then deselect all features
    if rownum == '' or int(rownum) == 0:
        layer.deselect([feat.id() for feat in layer.getFeatures()])
    else:
        # Else select row number matching the entered value
        for feat in layer.getFeatures():
            if feat.id() == int(rownum) - 1:
                layer.setSelectedFeatures([feat.id()])

Example:

Example