5

I am developing a QGIS plugin, using python and Plugin Builder plugin of QGIS. I have created a textEdit text box on the plugin. I want to show a list of matching words from the database as user will type in the textEdit and words will be shown in drop down as google and any other search engine provides.

Basically I want to build a search engine. It's like I want to use 'LIKE' operator and based on that I want to show matching results in drop down in the plugin. How is it possible in QGIS plugin. What event I have to use with the textEdit to list down the search. If there is any other open source plugin with same functionality available that would also be helpful.

  • 2
    You can use on change listener and when that event trigger get value from text box and use like command in query on backend. – user4906240 Jul 4 '17 at 9:29
  • i have modified my question a little bit, please read again. – Ahsan Mukhtar Jul 4 '17 at 9:33
  • are you using postgres with postgis as your database? – LaughU Jul 4 '17 at 9:37
2

with the text edit you would listen to the textChanged event like this:

self.dlg.textedit.textChanged.connect(self.my_function)

I suppose you are using postgres. So I propose you could try this:

def my_function(self, txt):
      search_in =  txt
      query = "SELECT myfield1, myfield2 FROM my_table WHERE '%s' LIKE '%' || search_field || '%';" % (search_in)
      # access your db and run the query 
      # run the query with while query.next() and store values in a list 
      # feed list to resiver (combox.addItems(myList)

The query part is taken from this answer.

EDIT

To get an auto-completion you would use the QCompleter class. I assume you have a list of words which is created from the input as shown above. The you do it like this. I use a line edit as an example.

    completer = QCompleter ()
    self.dlg.mylineedit.setCompleter(completer)
    model = QStringListModel()
    completer.setModel(model)
    model.setStringList(["completion", "data", "goes", "here"])
    # model.setStringList(my_lst)

enter image description here

  • Will it show drop down on my textEdit in plugin ? – Ahsan Mukhtar Jul 4 '17 at 9:50
  • Do you want an autocomplete of your text? I showed how you would get a list to feed back to your widget – LaughU Jul 4 '17 at 9:53
  • i want to show list, just like google search engine, where you type the word and suggestions are shown in drop down. – Ahsan Mukhtar Jul 4 '17 at 9:56
1

Use On Change event and then get suggestions based on value in text box. For showing suggestions you can use a listbox just below your text box. When there are suggestions list box will be populated when not listbox will be disabled.

For setting is visible use

QComboBox.setVisible(true)

For setting disabled use

QComboBox.setDisable(true) 
  • what is event or function that i can use to disable the combobox in plugin? – Ahsan Mukhtar Jul 4 '17 at 9:41
  • QComboBox.setEnabled(False) # disable comboBox – user4906240 Jul 4 '17 at 9:43
  • Is there a way that i can do without using combo box ? – Ahsan Mukhtar Jul 4 '17 at 9:45
  • You can also use QComboBox.setVisible(False) – user4906240 Jul 4 '17 at 10:00

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.