I have another question with Python GUI widgets in Qt Designer.
I am using Python 3.7 w/ PyQt5.
I have a list of values being generated to a combo box from an SQL table.
The combo box displays all of the values correctly but there are around 100 values total, I want to be able to type and it start to autocomplete so I can quickly find and select any value I may need.
I've done some research which has me baffled.
The list I created in Python is named listofCustOrders as I am building a "business gui" to help me learn more about Python coding. Is there a way to autocomplete this list?
from PyQt5 import QtWidgets, uic
from Classes import CustOrders as CO
import DBConnection as DB
import time
class MyWindow(QtWidgets.QMainWindow):
listofCustOrders = []
def __init__(self):
super(MyWindow, self).__init__()
uic.loadUi('PyGUI.ui',self)
self.init()
def init(self):
global listofCustOrders
listofCustOrders = CO.CustOrders.getCustOrders()
for x in listofCustOrders:
self.cbCONum.addItem(x.getCustOrderNO())
self.cbCONum.currentIndexChanged.connect(self.coSelected)
self.CObutton.clicked.connect(self.Submitted1)
self.SLabel2.hide()
def coSelected(self, text):
cbCOIndex = self.cbCONum.currentIndex()
selectedCO = listofCustOrders[cbCOIndex]
self.RLbl2.setText(selectedCO.getPart())
self.QtyLbl3.setText(str(selectedCO.getQTY()))
def Submitted1(self):
self.SLabel1.hide()
self.SLabel2.show()
CBW = str(self.cbCONum.currentText())
PN = self.RLbl2.text()
QY = self.QLINE.text()
EP = self.EMPLINE.text()
TIMER = time.strftime('%m-%d-%Y %H:%M:%S')
conn1 = DB.DBConnection.getConnection()
cursor = conn1.cursor()
cursor.execute('''
INSERT INTO database.dbo (CustOrderNo, PartNo, Qty, Employee, Color)
VALUES (?, ?, ?, ?, ?)''',
(CBW, PN, QY, EP,TIMER,))
conn1.commit()
conn1.close()
self.QLINE.clear()
self.EMPLINE.clear()
self.RLbl2.clear()
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Qt has QCompleter class for such tasks, here is an example of how you can use it.
completer = QCompleter(wordList, self)
completer.setCaseSensitivity(Qt.CaseInsensitive)
comboBox.setCompleter(completer)
For reference: https://doc.qt.io/qt-5/qcompleter.html, also checkout simple examples that show how to change your completer if your model (set of words) changed - https://doc.qt.io/qt-5/qtwidgets-tools-completer-example.html . The examples are in C++, unfortunately.
Related
I'm building a GUI that allows users to search information in a ms access database
(yup. It has to be the ms access)
The user has a textfield where he can type his search
and the Tableview should update instantly.
At the moment the DB disappears whenever you type a letter in the field.
Took me a while to figure out the problem: my SQL statement is simply not right.
(Thanks to model.lastError)
The whole function looks like this:
self.ui.Kistenschild_suchen.textChanged.connect(self.update_filter)
def update_filter(self, s):
s = re.sub("[\W_]+", "", s)
filter_str = 'Produkt LIKE %{}%"'.format(s)
self.ui.model.setFilter(filter_str)
print(self.ui.model.lastError())
In this case I typed k
The errormessage is:
PySide6.QtSql.QSqlError("-3100", "QODBC: Unable to execute statement", "[Microsoft][ODBC-Treiber für Microsoft Access] Syntaxfehler in Abfrageausdruck 'Produkt LIKE (%h%\")'.") at 0x000001CA4FB33B88>
Point of interest should be the
'%h%")'."'
Since it shows more characters than typed
I tried to change in several ways, like changing the % to * and?
Still nothing
EDIT:
Here is the minimal reproducible example:
import re
import sys
from PySide6.QtCore import QSize, Qt
from PySide6.QtSql import QSqlDatabase, QSqlTableModel
from PySide6.QtWidgets import (
QApplication,
QLineEdit,
QMainWindow,
QTableView,
QVBoxLayout,
QWidget,
)
Driver= r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\scripts\python\pyside_Tutorials\databases\chinook.accdb'
db = QSqlDatabase("QODBC")
db.setDatabaseName(Driver)
db.open()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
container = QWidget()
layout = QVBoxLayout()
self.search = QLineEdit()
self.search.textChanged.connect(self.update_filter)
self.table = QTableView()
layout.addWidget(self.search)
layout.addWidget(self.table)
container.setLayout(layout)
self.model = QSqlTableModel(db=db)
self.table.setModel(self.model)
self.model.setTable("Track")
self.model.select()
self.setMinimumSize(QSize(1024, 600))
self.setCentralWidget(container)
# tag::filter[]
def update_filter(self, s):
s = re.sub("[\W_]+", "", s)
filter_str = 'Name LIKE "%{}%"'.format(s)
self.model.setFilter(filter_str)
print(self.model.lastError())
print(s,type(s))
# end::filter[]
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
This code gives me the errormessage:
<PySide6.QtSql.QSqlError("-3010", "QODBC: Unable to execute statement", "[Microsoft][ODBC-Treiber für Microsoft Access] 1 Parameter wurden erwartet, aber es wurden zu wenig Parameter übergeben.") at 0x0000016FC7535108>
Which means something like: "1 parameter was expected but too few parameters were passed"
MS-Access needs a double apostrophe like:
def update_filter(self, s):
s = re.sub(r"[\W_]+", "", s)
filter_str = f"Produkt '%{s}%'"
self.model.setFilter(filter_str)```
I have the following issue.
I have a very simple code which is connecting to my local sql database, and from there I am exporting a data set with a query. Then I display this data in a PyQT table widget and that is all.
The problem is that I cannot align my data to center of columns. Currently, everything is aligned to the left.
I tried to use different combinations with Qt.AlignCenter but I cannot make it working.
here is my code
import os
from PyQt5 import QtCore, QtGui, QtWidgets, QtSql
class BlobDelegate(QtWidgets.QStyledItemDelegate):
def displayText(self, value, locale):
if isinstance(value, QtCore.QByteArray):
value = value.data().decode()
return super(BlobDelegate, self).displayText(value, locale)
def createConnection():
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "stats.db")
db.setDatabaseName(file)
if not db.open():
QtWidgets.QMessageBox.critical(
None,
QtWidgets.qApp.tr("Cannot open database"),
QtWidgets.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."
),
QtWidgets.QMessageBox.Cancel)
return False
return True
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
if not createConnection():
sys.exit(-1)
w = QtWidgets.QTableView()
w.setFont(QtGui.QFont('Arial', 18))
w.horizontalHeader().setStretchLastSection(False)
w.horizontalHeader().setFixedHeight(40)
w.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
# w.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
w.setWordWrap(True)
delegate = BlobDelegate(w)
w.setItemDelegateForColumn(4, delegate)
model = QtSql.QSqlQueryModel()
model.setQuery("SELECT * FROM table_tennis_statistics")
w.setModel(model)
w.resize(1024, 600)
w.show()
sys.exit(app.exec_())
screenshot
You have to modify the displayAlignment property of QStyleOptionViewItem and apply the delegate to all columns:
class BlobDelegate(QtWidgets.QStyledItemDelegate):
def initStyleOption(self, option, index):
super(BlobDelegate, self).initStyleOption(option, index)
option.displayAlignment = QtCore.Qt.AlignCenter
def displayText(self, value, locale):
if isinstance(value, QtCore.QByteArray):
value = value.data().decode()
return super(BlobDelegate, self).displayText(value, locale)
delegate = BlobDelegate(w)
w.setItemDelegate(delegate)
I have a PostgreSQL parent table called 'rooms' and a related table called 'configurations'. I have a form which would allow the user to add edit and add new configurations to individual rooms in a QTableView. In the configuration table, room is a foreign key to the rooms table. The form looks like this:
As far as editing the configurations, there seems to be no issue. The problem arises when I add a new record in the configurations model. If I insert a row and do not set the room cell, the user can edit any of the fields and it will save to the database. Although, when finished editing, the row clears and ! shows up in the row header. If I reload the form, the record edited correctly.
What I want to do, however, is to set the room number for the user and keep the room number field in the configuration view hidden from the user. When I add a row and set the room number cell's value, I cannot edit the row. Any ideas?
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtSql import *
from rooms import *
from config import *
class Setup_rooms(QDialog):
def __init__(self):
QDialog.__init__(self)
self.ui = Ui_rooms_setup()
self.ui.setupUi(self)
self.rooms_model = QSqlRelationalTableModel(self)
self.rooms_model.setTable('rooms')
self.rooms_model.setRelation(4, QSqlRelation('room_types1', 'room_type', 'room_type'))
self.rooms_model.setSort(int(self.rooms_model.fieldIndex("room")), Qt.AscendingOrder)
self.rooms_model.select()
self.rooms_mapper = QDataWidgetMapper(self)
self.rooms_mapper.setSubmitPolicy(QDataWidgetMapper.AutoSubmit)
self.rooms_mapper.setModel(self.rooms_model)
self.rooms_mapper.setItemDelegate(QSqlRelationalDelegate(self))
self.rooms_mapper.addMapping(self.ui.room_num, self.rooms_model.fieldIndex("room"))
self.rooms_mapper.addMapping(self.ui.room_description, self.rooms_model.fieldIndex("description"))
self.rooms_mapper.addMapping(self.ui.deep_clean_date, self.rooms_model.fieldIndex("deepcleandate"))
self.rooms_mapper.addMapping(self.ui.deep_clean_cycle, self.rooms_model.fieldIndex("deepcleancycle"))
room_types_model = self.rooms_model.relationModel(4)
self.ui.room_type.setModel(room_types_model)
self.ui.room_type.setModelColumn(room_types_model.fieldIndex('room_type'))
self.rooms_mapper.addMapping(self.ui.room_type, self.rooms_model.fieldIndex('room_type'))
self.rooms_mapper.toFirst()
self.ui.current_index.setText(str(self.rooms_mapper.currentIndex()))
self.config_model = QSqlRelationalTableModel(self)
self.config_model.setTable('room_configurations')
self.config_model.setRelation(self.config_model.fieldIndex("configuration"), QSqlRelation('configurations', 'configuration', 'configuration'))
self.room_model = self.config_model.relationModel(self.config_model.fieldIndex('room'))
self.config_view = self.ui.configurations
self.config_view.setModel(self.config_model)
self.config_view.horizontalHeader().setStretchLastSection(True)
self.config_model.select()
self.config_model.setHeaderData(self.config_model.fieldIndex("sort_order"), Qt.Horizontal, "sort order")
self.config_view.setItemDelegate(View_room_config(self))
self.config_view.setItemDelegate(QSqlRelationalDelegate(self.config_view))
self.config_view.resizeColumnsToContents()
room = self.rooms_model.record(self.rooms_mapper.currentIndex()).value('room')
self.config_model.setFilter("room = '{0}'".format(room))
#self.config_view.setColumnHidden(0, True)
#self.config_view.setColumnHidden(2, True)
self.ui.add_config.clicked.connect(self.add_config)
def add_config(self):
row=int(self.config_model.rowCount())
self.config_model.insertRow(row)
self.index = QModelIndex(self.config_model.index(row, 2))
self.config_model.setData(self.index, self.ui.room_num.text(), Qt.EditRole)
class View_room_config(QSqlRelationalDelegate):
def __init__(self, parent=None):
QItemDelegate.__init__(self)
def createEditor(self, parent, option, index):
if index.column() == 3:
combo = super().createEditor(parent, option, index)
if __name__=="__main__":
app=QApplication(sys.argv)
db = QSqlDatabase.addDatabase("QPSQL")
db.setHostName(host)
db.setDatabaseName(database)
db.setUserName(user)
db.setPassword(password)
if (db.open()==False):
QMessageBox.critical(None, "Database Error", db.lastError().text())
myapp = Setup_rooms()
myapp.show()
sys.exit(app.exec_())
I kind of sorted my problem. I restored a back up of the database into the development area. I am not aware of any underlying tables which would have caused an issue but my theory is that a database error was occurring. I am now testing for errors on self.config_model using self.config_model.lastError().text() but I haven't found any.
Nonetheless, without changing my code the program is kind of working although the user interface when editing is rather clunky. I will now look into that.
I'm trying to create a GUI with QT Designer. I've converted my .ui designer file to a .py.
Here is my code:
from PyQt4.QtCore import QSettings, QTranslator, qVersion, QCoreApplication
from PyQt4.QtGui import QAction, QIcon
from qgis.core import *
import resources
from delete_feature_dialog import DeleteFeatureDialog
import os.path
class DeleteFeature:
def __init__(self, iface):
# Save reference to the QGIS interface
self.iface = iface
# Declare instance attributes
self.actions = []
self.menu = self.tr(u'&DeleteFeature')
self.toolbar = self.iface.addToolBar(u'DeleteFeature')
self.toolbar.setObjectName(u'DeleteFeature')
def add_action(
self,
icon_path,
text,
callback,
enabled_flag=True,
add_to_menu=True,
add_to_toolbar=True,
status_tip=None,
whats_this=None,
parent=None):
# Create the dialog (after translation) and keep reference
self.dlg = DeleteFeatureDialog()
....
return action
def initGui(self):
icon_path = ':/plugins/DeleteFeature/icon.png'
self.add_action(
icon_path,
text=self.tr(u''),
callback=self.run,
parent=self.iface.mainWindow())
def run(self):
#this code will populate the combo box with all vector layer
self.dlg.layerListCombo.clear()
layers = self.iface.legendInterface().layers()
layer_list = []
for layer in layers:
layerType = layer.type()
if layerType == QgsMapLayer.VectorLayer:
layer_list.append(layer.name())
self.dlg.layerListCombo.addItems(layer_list)
# show the dialog
self.dlg.show()
# Run the dialog event loop
result = self.dlg.exec_()
# See if OK was pressed
if result:
# Do something useful here - delete the line containing pass and
# substitute with your code.
selectedLayerIndex = self.dlg.layerlistcombo.currentIndex()
selectedLayer = layers [selectedLayerIndex]
.....
Then when I open the plugin, I get the following error:
'DeleteFeatureDialog' objectObject has no attribute
'layerlistcombo'in QGIS Plugin
Any suggestion for this.
Seems that you wrote:
selectedLayerIndex = self.dlg.layerlistcombo.currentIndex()
but you should have written:
selectedLayerIndex = self.dlg.layerListCombo.currentIndex()
Like you did previously in your code (notice the Camel Notation when writing, not just lower-case letters), which is probably causing the No Attribute error you get.
Combobox dbSelection does not update to apply in code when selecting table names from sqlite to display in combobox tabSelection.
It also takes a significant number of seconds to load the directory dialog window once the button is clicked.
I would also like to ensure that all tables within a database are listed in the tabSelection combobox.
The code is as follows and is associated with a Qt Designer file:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
import qcDbWidget2
import os
import sqlite3
class MainDialog(QWidget, qcDbWidget2.Ui_qcQueryWidget):
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.setupUi(self)
self.connect(self.dbDirSelect, SIGNAL("clicked()"), self.getDirName)
def getDirName(self):
existDir = QFileDialog.getExistingDirectory(self)
dbDir = str(existDir)
self.dbDirDisplay.setText(dbDir)
dbFileList = []
for root, dirs, files in os.walk(dbDir):
for file in files:
if file.endswith('.db'):
dbFileList.append(file)
self.dbSelection.addItems(dbFileList)
tableList = []
self.dbSelection.update()
dbName = str(self.dbSelection.currentText())
dbPath = str(dbDir + '\\' + dbName)
conn = sqlite3.connect(dbPath)
c = conn.cursor()
res = c.execute("SELECT name FROM sqlite_master WHERE type='table';")
self.tabSelection.clear()
for name in res:
tableList.append(name[0])
print(name[0])
for name in tableList:
self.tabSelection.addItems(tableList)
app = QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()
Have you considered to use QTableView instead of QTableList?
Qt uses MVC which methods are way faster than doing it by hand.
In this case, it's pretty simple.
You create a model:
my_model = QStringListModel()
You can then save data to my_model:
my_list = ["toto", "tutu", "tata"]
my_model.setStringList(my_list)
Once you have a QTableView, you use it's method setModel() to provide the model.
my_table_view.setModel(my_model)