Show completion list when qlineEdit textChanged - python

I can use the following command to show an auto completion list view if I have a predefined list
wordList =["alpha", "omega", "omicron", "zeta"]
completer = QCompleter(wordList)
ineEdit.setCompleter(completer)
I need a different case:, Type something in lineEdit, when textChanged it connect to a function def searchAction: via lineEdit.textChanged.connect(searchAction). Inside def searchAction: i have an sql query which updates a list (say data_list) for every string I type in the lineEdit . If set wordList as data_list it does not do anything. My question how can I show the data_list as a autocompletion something like in the image.

It is not necessary to create lists since QCompleter supports models so you can filter using QSqlQueryModel as shown below:
import os
import sys
from pathlib import Path
from PyQt5.QtWidgets import QApplication, QCompleter, QLineEdit
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlQueryModel
CURRENT_DIRECTORY = Path(__file__).resolve().parent
def create_connection(path):
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName(path)
if not db.open():
print(db.lastError().text())
return False
return True
class SqlQueryCompleter(QCompleter):
def __init__(self, sql_model=None, parent=None):
super().__init__(sql_model, parent)
if sql_model is None:
sql_model = QSqlQueryModel(self)
elif not isinstance(sql_model, QSqlQueryModel):
raise ValueError(f"The {sql_model} must be an instance of QSqlQueryModel")
self.setModel(sql_model)
self.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
def splitPath(self, path):
if isinstance(self.model(), QSqlQueryModel):
self.model().setQuery(self.query(path))
return super().splitPath(path)
def query(self, path):
query = QSqlQuery("SELECT bar FROM 'foo' WHERE bar LIKE ?")
query.addBindValue(f"%{path}%")
if not query.exec_():
print(query.lastError().text())
return query
def main():
app = QApplication(sys.argv)
filename = os.fspath(CURRENT_DIRECTORY / "database.db")
if not create_connection(filename):
sys.exit(-1)
line_edit = QLineEdit()
line_edit.show()
completer = SqlQueryCompleter()
line_edit.setCompleter(completer)
sys.exit(app.exec_())
if __name__ == "__main__":
main()

Related

How to reimplement QTextDocument createObject?

How to reimplement QTextDocument.createObject?
this method plays a role in making QTextFrame, QTextList, QTextTable or other QTextObject.
According to woboq, I think my reimplementation is the same.
But kernel stops.
Why? What is my code short of?
from PySide2 import QtWidgets
from PySide2 import QtGui
from PySide2 import QtCore
import PySide2
import sys
import os
dirname = os.path.dirname(PySide2.__file__)
plugin_path = os.path.join(dirname, 'plugins', 'platforms')
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
class TextEdit(QtWidgets.QTextEdit):
def __init__(self, parent=None):
super(TextEdit, self).__init__(parent=None)
document = TextDocument(self)
self.setDocument(document)
class TextDocument(QtGui.QTextDocument):
def __init__(self, parent=None):
super(TextDocument, self).__init__(parent=None)
self.setParent(parent)
def createObject(self, f):
obj = QtGui.QTextObject(self)
if f.isListFormat():
obj = QtGui.QTextList(self)
elif f.isTableFormat():
obj = QtGui.QTextTable(self)
elif f.isFrameFormat():
obj = QtGui.QTextFrame(self)
return obj
def main():
if QtWidgets.QApplication.instance() is not None:
app = QtWidgets.QApplication.instance()
else:
app = QtWidgets.QApplication([])
mainwindow = TextEdit()
mainwindow.show()
sys.exit(QtWidgets.QApplication.exec_())
if __name__ == "__main__":
main()
It seems to me that it is a bug (I have tested it with PyQt5 and it works correctly), the problem to be the life cycle of the QTextObject since as in C++ the life cycle is undefined since it is a pointer but being a child of QTextDocument So its life cycle is that of the QTextDocument, but in python it seems that it considers it an object of limited scope(local variable) not respecting the ownership that the QTextDocument has over this since it is its parent. A workaround seems to be making obj an member of the class:
def createObject(self, f):
self.obj = QtGui.QTextObject(self)
if f.isListFormat():
self.obj = QtGui.QTextList(self)
elif f.isTableFormat():
self.obj = QtGui.QTextTable(self)
elif f.isFrameFormat():
self.obj = QtGui.QTextFrame(self)
return self.obj
Or using a container that is a member of the class.
class TextDocument(QtGui.QTextDocument):
def __init__(self, parent=None):
super(TextDocument, self).__init__(parent)
self.objs = []
def createObject(self, f):
obj = QtGui.QTextObject(self)
if f.isListFormat():
obj = QtGui.QTextList(self)
elif f.isTableFormat():
obj = QtGui.QTextTable(self)
elif f.isFrameFormat():
obj = QtGui.QTextFrame(self)
self.objs.append(obj)
return obj
I prefer the second workaround since in the case of the first one you could generate problems if you create several QTextObject since the previous one would be deleted.
Finally I recommend reporting the bug.
The handling of the life cycle of some objects seems a persistent bug in PySide2.

Program quit with error when using QCompleter

I'm writing a program with pyqt5 and want to make the QlineEdit show history input by using sqlite to store the inputs. I use a signal to catch the cursor when focusInEvent happens and select the history records at that time, then I put the results into QCompleter so it can pop up in QlineEdit. Now I can make the history inputs show in QlineEdit object but when I click on any value, 1s later, the whole program quit automatically with error, which says "Python has stopped".
class FocusLineEdit(QLineEdit):
ac = pyqtSignal(list)
def __init__(self, parent=None):
super(FocusLineEdit, self).__init__(parent)
self.ac.connect(self.addCompleter)
def focusInEvent(self, event):
rtl = call_history(self.objectName())
self.ac.emit(rtl)
def addCompleter(self, rtl):
self.autoCompleter = QCompleter(rtl)
self.autoCompleter.setCompletionMode(1)
self.setCompleter(self.autoCompleter)
def focusOutEvent(self, event):
pass
It is difficult to analyze where the problem is without you provide an MCVE, so my response implements what you require without taking into account your current code for it must meet the following requirements:
You must have 2 tables: objects and history that are related.
The table objects saves the name of the filtering history, it is similar to the use of the objectName that you use, but in general 2 widgets can access the same history if the connection establishes the same name
In the history table the information of the words associated with the id of the objects table is saved.
In my example I use the following instructions to create them:
CREATE TABLE IF NOT EXISTS objects (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE);
CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY AUTOINCREMENT, id_object INTEGER REFERENCES objects (id), word TEXT NOT NULL, UNIQUE (id_object, word));
Also to test it I created a data, if you already have data then you must eliminate the if test: and everything inside.
Finally for a better understanding of my solution I show a QTableView that is changing according to the selection.
from PyQt5 import QtCore, QtGui, QtWidgets, QtSql
def createConnection():
db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
db_path = 'test.db'
db.setDatabaseName(db_path)
if not db.open():
QMessageBox.critical(None, qApp.tr("Cannot open database"),
qApp.tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information "
"how to build it.\n\n"
"Click Cancel to exit."),
QMessageBox.Cancel)
return False
test = True
if test:
query = QtSql.QSqlQuery()
if not query.exec_('CREATE TABLE IF NOT EXISTS objects (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL UNIQUE);'):
return False
if not query.exec_('CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY AUTOINCREMENT, id_object INTEGER REFERENCES objects (id), word TEXT NOT NULL, UNIQUE (id_object, word));'):
return False
for i in range(3):
query.prepare('INSERT INTO objects (name) VALUES (?)')
query.addBindValue("obj{}".format(i))
if not query.exec_():
print(query.lastError().text())
import requests
import random
word_site = "http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
response = requests.get(word_site)
WORDS = response.content.decode().splitlines()
print(WORDS)
for i in range(3):
for text in random.sample(WORDS, 50):
query.prepare('INSERT INTO history (id_object, word) VALUES (?, ?)')
query.addBindValue(i+1)
query.addBindValue(text)
if not query.exec_():
print(query.lastError().text())
return True
class Completer(QtWidgets.QCompleter):
def __init__(self, parent=None):
super(Completer, self).__init__(parent)
self._last_words = []
def splitPath(self, path):
if path[-1] != ' ':
words = path.split()
self._last_words = words[:-1] if len(words) > 1 else []
return [words[-1]]
else:
QtCore.QTimer.singleShot(0, self.popup().hide)
return []
def pathFromIndex(self, index):
val = super(Completer, self).pathFromIndex(index)
return ' '.join(self._last_words + [val])
class HistoryManager(QtCore.QObject):
nameChanged = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super(HistoryManager, self).__init__(parent)
model = QtSql.QSqlRelationalTableModel(self)
model.setTable("history")
model.setRelation(1, QtSql.QSqlRelation("objects", "id", "name"))
model.select()
self._proxy = QtCore.QSortFilterProxyModel(self)
self._proxy.setSourceModel(model)
self._proxy.setFilterKeyColumn(1)
# proxy.setFilterFixedString("obj1")
self._widgets = {}
self._completer = Completer(self)
self._completer.setModel(self._proxy)
self._completer.setCompletionColumn(2)
def register_widget(self, widget, objectname):
# TODO
if callable(getattr(widget, "setCompleter")):
widget.installEventFilter(self)
self._widgets[widget] = objectname
return True
return False
def eventFilter(self, obj, event):
if obj in self._widgets:
if event.type() == QtCore.QEvent.FocusIn:
name = self._widgets[obj]
self._proxy.setFilterFixedString(name)
obj.setCompleter(self._completer)
self.nameChanged.emit(name)
elif event.type() == QtCore.QEvent.FocusOut:
obj.setCompleter(None)
self._proxy.setFilterFixedString("")
self.nameChanged.emit("")
return super(HistoryManager, self).eventFilter(obj, event)
class Widget(QtWidgets.QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self._manager = HistoryManager()
model = QtSql.QSqlRelationalTableModel(self)
model.setTable("history")
model.setRelation(1, QtSql.QSqlRelation("objects", "id", "name"))
model.select()
self._proxy = QtCore.QSortFilterProxyModel(self)
self._proxy.setSourceModel(model)
self._proxy.setFilterKeyColumn(1)
tv = QtWidgets.QTableView()
tv.setModel(self._proxy)
vlay = QtWidgets.QVBoxLayout()
for i in range(3):
le = QtWidgets.QLineEdit()
vlay.addWidget(le)
self._manager.register_widget(le, "obj{}".format(i))
vlay.addStretch()
lay = QtWidgets.QHBoxLayout(self)
lay.addWidget(tv, stretch=1)
lay.addLayout(vlay)
self._manager.nameChanged.connect(self._proxy.setFilterFixedString)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
if not createConnection():
sys.exit(-1)
manager = HistoryManager()
w = Widget()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

Get selected object from ListBox (binded to ListStore) in Python GTK

I have made a simple GTK music player with ListBox (as playlist).
This is GObject class, which i use to bind to ListBox (using bind_model() method) and ListStore.
import eyed3
from gi.repository import Gio, GObject
class Song(GObject.GObject):
path = GObject.Property(type=str)
name = GObject.Property(type=str)
def __init__(self, path):
GObject.GObject.__init__(self)
self.path = path
self.file = eyed3.load(path)
self.name = self
def __str__(self):
return str(self.file.tag.artist) + ' - ' + str(self.file.tag.title)
playlist = Gio.ListStore().new(Song)
And this is how I bind ListStore to ListBox:
play_listbox.connect('row-selected', self.on_row_selected)
playlist.append(Song('/home/user/Downloads/Some album/01 - Song1.mp3'))
playlist.append(Song('/home/user/Downloads/Some album/02 - Song2.mp3'))
play_listbox.bind_model(playlist, self.create_song_label)
def create_song_label(self, song):
return Gtk.Label(label=song.name)
And so far, everything works like it should.
The question is: Is it possible to retrieve Song object (stored in playlist) based on selection? To retrieve path property stored in that object?
If not, is it possible to at least retrieve selection text? Trying this with
def on_row_selected(self, container, row):
print(row.widget.label)
Gives a traceback:
Traceback (most recent call last):
File "/home/user/Documents/App/player.py", line 45, in on_row_selected
print(row.widget.label) # or data, value, text - nothing works
RuntimeError: unable to get the value
row variable is of type
<Gtk.ListBoxRow object at 0x7f9fe7604a68 (GtkListBoxRow at 0x5581a51ef7d0)>
So above code, I think, should work like a charm... but it doesn't.
Thank you very much for any help provided!
So you need to assign the selection with:
treeview_selection = treeview.get_selection()
And connect it with the 'changed' signal:
treeview_selection.connect('changed', on_tree_selection_changed)
Then you can fetch the required data with:
def on_tree_selection_changed(self, treeview):
model, treeiter = treeview.get_selected()
if treeiter is not None:
print(model[treeiter][0]) # you should a list index to get the data you require for each row - first column = [0], second column = [1] etc.
I would suggest you read the pgi docs and also the python Gtk docs
Is it possible to retrieve Song object (stored in playlist) based on selection? To retrieve path property stored in that object?
def on_row_selected(self, container, row):
song = playlist.get_item(row.get_index())
If not, is it possible to at least retrieve selection text?
def on_row_selected(self, container, row):
name = row.get_child().get_text()
Here is an example. You could also use the selected-rows-changed signal if you have to deal with multiple selections:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio, GObject
import sys
class Song(GObject.GObject):
path = GObject.Property(type=str)
name = GObject.Property(type=str)
def __init__(self, path, name):
GObject.GObject.__init__(self)
self.path = path
self.name = name
class GUI:
def __init__(self):
self.playlist = Gio.ListStore()
self.playlist.append(Song("path1", "name1"))
self.playlist.append(Song("path2", "name2"))
self.playlist.append(Song("path3", "name3"))
play_listbox = Gtk.ListBox()
play_listbox.set_selection_mode(Gtk.SelectionMode.SINGLE)
play_listbox.bind_model(self.playlist, self.create_widget_func)
play_listbox.connect('row-selected', self.on_row_selected_1)
play_listbox.connect('row-selected', self.on_row_selected_2)
play_listbox.connect('selected-rows-changed', self.on_selected_rows_changed)
window = Gtk.Window()
window.add(play_listbox)
window.connect("destroy", self.on_window_destroy)
window.show_all()
def create_widget_func(self, song):
return Gtk.Label(label = song.name)
def on_row_selected_1(self ,container, row):
print("on_row_selected_1")
song = self.playlist.get_item(row.get_index())
print(song.path)
def on_row_selected_2(self ,container, row):
print("on_row_selected_2")
print(row.get_child().get_text())
def on_selected_rows_changed(self, container):
print("on_selected_rows_changed", len(container.get_selected_rows()), "item(s) selected")
for row in container.get_selected_rows():
song = self.playlist.get_item(row.get_index())
print(song.name, song.path)
print("---")
def on_window_destroy(self, window):
Gtk.main_quit()
def main():
app = GUI()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())

When I use "currentIndex()" on

I've been stuck on a school assignment for hours because of this one problem. I need to check the index number (AS AN INTEGER) for the currently selected item in a ListWidget.
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QListView
from PyQt5.QtGui import QPixmap
import Ui_countries
class MyForm(QMainWindow, Ui_countries.Ui_mainWindow):
# DON'T TOUCH!
def __init__(self, parent=None):
super(MyForm, self).__init__(parent)
self.setupUi(self)
# END DON'T TOUCH
# EVENT HOOKS HERE
self.countryList.itemClicked.connect(self.CountrySelected)
self.actionLoad_Countries.triggered.connect(self.LoadCountries)
self.sqUnits.currentIndexChanged.connect(self.SqUnits)
#self.updatePopulation.itemClicked.connect(self.updateMemory)
# RESPONSES HERE
# def updateMemory(self):
def LoadCountries(self):
global namelist
global populationlist
global arealist
namelist = []
populationlist = []
arealist = []
objFile = open("GUI/countries.txt")
for line in objFile:
line = line.replace("\n","")
lineList = line.split(",")
self.countryList.addItem(lineList[0])
namelist.append(lineList[0])
populationlist.append(lineList[1])
arealist.append(lineList[2])
objFile.close()
def CountrySelected(self,selectedCountryIndex):
QMessageBox.information(self,"Country changed!",selectedCountryIndex.text())
strCountryName = selectedCountryIndex.text()
strCountryName = strCountryName.replace(" ", "_")
imagePixmap = QPixmap(f"GUI/Flags/{strCountryName}")
strCountryName = strCountryName.replace("_", " ")
self.lblCountryName.setText(strCountryName)
self.flag.setPixmap(imagePixmap)
self.flag.resize(imagePixmap.width(),imagePixmap.height())
idx = self.countryList.currentIndex()
# self.populationbox.setText(populationlist[idx])
# selectedCountryIndex.index()
#^^^^^^^^^^^ useful code
print(int(strCountryName))
def SqUnits(self):
QMessageBox.information(self,"Event Received","Please convert between different units.")
if self.sqUnits.currentText() == "Sq. Miles":
self.totalareabox.setText("YAAAAA")
else:
self.totalareabox.setText("YEEEE")
# DON'T TOUCH
if __name__ == "__main__":
app = QApplication(sys.argv)
the_form = MyForm()
the_form.show()
sys.exit(app.exec_())
The area to focus on would be the CountrySelected function. Whenever I try to run idx = self.countryList.currentIndex(), instead of an integer, I get 'PyQt5.QtCore.QModelIndex object at 0x051A6470' if I try to print idx. My instructor wants us to use pyqt, and I have no experience with it, so I'm kinda freaking out!
Figured it out, classmate sent me this: index = self.listWidget.currentRow()
No idea why index didn't work, but alas, tis the nature of doing your assignments one hour before they're due.

PyQt4 How to send push button signal to another class in Python?

I need your advice and help for my code.
I am making a GUI program. Basically the program does the followings:
Gets input from the user
When a certain button is pushed, the program retrieves all the input and saved them in the database
The program does computations.
Shows the output
Here is the simple version of the program, calculator program:
As you can see in the figure, it takes 4 input from user, (1) any integer for var1, (2) any integer for var2, (3) operator (addition or subtraction) and (4) click button.
The GUI was designed using QtDesigner.
And here is my code:
import sys
import sqlite3
from PyQt4 import QtCore, QtGui
from testgui import Ui_MainWindow
class ShowAndInput(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#connect to the database
self.connect_to_db()
#create table in the database
self.create_table()
#add items to the comboBox
self.add_items()
#calculate button to start calculating, gives signal to the next class Calculate
self.ui.pushButton_3.clicked.connect(self.calculate)
#close button to close the window
QtCore.QObject.connect(self.ui.pushButton,QtCore.SIGNAL("clicked()"), quit)
self.conn.close()
def connect_to_db(self):
self.conn = sqlite3.connect('testguidb.sqlite3')
self.cur = self.conn.cursor()
def create_table(self):
self.cur.execute('''CREATE TABLE IF NOT EXISTS Input (var1 INTEGER, var2 INTEGER)''')
self.conn.commit()
def add_items(self):
lst = ['addition', 'subtraction']
self.ui.comboBox.clear()
self.ui.comboBox.addItems(lst)
def calculate(self):
calculate = Calculate(self)
calculate.exec_()
So the purpose of the first class was to show the GUI and take input from the user. If user clicks the pushbutton named 'Calculate', it should connect to the new class named 'Calculate'.
Here is my code for the Calculate class:
class Calculate(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#validator to validate that the input is integer or digit
#connect to database
self.connect_to_db()
#get the input values and store them in the database
self.get_the_values()
#print the output
PrintOutcome()
def connect_to_db(self):
self.conn = sqlite3.connect('testguidb.sqlite3')
self.cur = self.conn.cursor()
def get_the_values(self):
dicti = {}
dicti['var1'] = self.ui.lineEdit.text()
dicti['var2'] = self.ui.lineEdit_2.text()
for key, val in sorted(dicti.items()):
key = str(key)
val = int(val)
self.cur.execute('INSERT OR IGNORE INTO Input (?) VALUES (?)',(key,val))
self.conn.commit()
And at the end of this class, it calls another class named 'PrintOutcome' to print out the result in the QTextBrowser. Here is the last piece of the code:
class PrintOutcome(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
#connect to database
self.connect_to_db()
#get the value of the combobox
self.get_combox()
#print the output
if self.operator == 'addition': self.print_add()
elif self.operator == 'subtraction': self.print_sub()
def connect_to_db(self):
self.conn = sqlite3.connect('testguidb.sqlite3')
self.cur = self.conn.cursor()
def get_combox(self):
self.operator = str(self.ui.comboBox.currentText())
def print_add(self):
dicti={}
for i in range(2):
i = i + 1
dicti['var{0}'.format(i)] = self.cur.execute('SELECT var{0} FROM Input'.format(i))
dicti['var{0}'.format(i)] = int(self.cur.fetchone()[0])
text = str(dicti['var{0}'.format(i)])
self.ui.textBrowser.append(text)
result = dicti['var1'] + dicti['var2']
textsep = '-'*25+'(+)'
text = str(result)
self.ui.textBrowser.append(text)
self.ui.textBrowser.append(text)
def print_sub(self):
dicti={}
for i in range(2):
i = i + 1
dicti['var{0}'.format(i)] = self.cur.execute('SELECT var{0} FROM Input'.format(i))
dicti['var{0}'.format(i)] = int(self.cur.fetchone()[0])
text = str(dicti['var{0}'.format(i)])
self.ui.textBrowser.append(text)
result = dicti['var1'] + dicti['var2']
textsep = '-'*25+'(-)'
text = str(result)
self.ui.textBrowser.append(text)
self.ui.textBrowser.append(text)
if __name__=='__main__':
app = QtGui.QApplication(sys.argv)
myapp = ShowAndInput()
myapp.show()
sys.exit(app.exec_())
When I ran it, everything was fine. But when I pressed the calculate button, it returned an error message.
Here is the error message: RuntimeError: super-class init() of type Calculate was never called.
Any respond will be much appreciated. Thanks a lot.
Regards,
Arnold

Categories