Inserting row in QTableView - python

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.

Related

Python retrieve QComboBox from QTablewidget

I am now a month into Python and half a month into QDesigner/Qt.
Within QDesigner, I have a QTableWidget, which I promote to my own InputTable(QTableWidget) class.
class InputTable(QTableWidget):
def __init__(self, parent=None):
self.parent = parent
super(QTableWidget, self).__init__(parent)
...
I inserted some QComboboxes, which allow the user to select the units of the values in the table. This is at the initialization within the InputTable class.
pressureunits = [ "barg","bara", "psig", "psia"]
pressurecombobox = QComboBox()
for i in range(len(pressureunits)):
pressurecombobox.addItem(pressureunits[i])
self.setCellWidget(2, 0, pressurecombobox)
So far, so good.
Now I want to convert all the inputdata to a parallel table (master table) with values in SI units for further analysis.
units = self.tableInputs.cellWidget(2,0)
if units.currentIndex()==0: # this means the combobox is at the first entry
mastertable[0,2] = float(self.tableInputs.item(2,1).text())
This fails, because units is of type QWidget, which has no attribute currentIndex. On some forums I read that the QComboBox is a child of the QWidget and I have played with the children() functions, but helas no solution here. Some people recommend switching to QTableView structure. I spend my day searching how to do this, nothing found yet. Any smart ideas?
Update: After reducing all complexity from my question, I was able to find the solution below.
from PyQt5.QtWidgets import QComboBox, QTableWidget, QTableWidgetItem
Mytable = QTableWidget(3,3)
Mytable.setItem(0, 0, QTableWidgetItem("1"))
print("Now retrieve the data :", Mytable.item(0, 0).text())
pressureunits = [ "barg","bara", "psig", "psia"]
pressurecombobox = QComboBox()
for i in range(len(pressureunits)):
pressurecombobox.addItem(pressureunits[i])
Mytable.setCellWidget(2, 0, pressurecombobox)
print("Now retrieve the active (first) entry :", Mytable.cellWidget(2,0).currentText())

QComboBox Auto Complete (QCompleter?)

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.

PyQt4 Combobox Not Updating After Initial Selection

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)

gtk.TreeView does not change after changing its gtk.ListStore:

Used: python 2.79, gtk 2.0
Dear python and PyGtk-users
In my program I want to give the opportunity to show searchresults of a great datafile in a gtk.TreeView using the method in the code. In the function that creates the ListStore I also create the csv-file the store is using. Now I can see, that the csv-file has changed after a new search with different keywords, but the TreeView does not, it still keeps the result of the first search, though I clear the store everytime self.search is running. It's really enerving if you need to restart the program for every new search...
I already tried some things, like creating a proper function for the datacreatin for the store, but nothing serves and the truth is that I don't have a lot of ideas...
And everything I found in the internet about this was about autorefreshing of the store with a timer or other topics more profound, seems like everyone instead of me knows how to do this...
Can anyone tell me, what mistake I am making? How do I refresh a TreeView?(as you can see self.search is called by a button...) here the relevant part of the code:
import gtk, csv
class BASE:
#...
#some other funtions, irrelevant for the problem
#...
def search(self, widget, event, data=None):
#...
#all the searchingstuff and the creation of 'inwrite.csv'
#...
with open('/home/emil/Documents/inwrite.csv', 'r') as storefile:
lines = storefile.readlines()
store = gtk.ListStore(str, str, str, str, str, str)
store.clear()
i=0
while i < len(lines):
line = [lines[i]]
csvfile = csv.reader(line, delimiter=',')
for row in csvfile:
temp = (row[0], row[1], row[2], row[3], row[4], row[5])
store.append(temp)
i = i + 1
self.tabview = gtk.TreeView(store)
self.tabview.set_rules_hint(True)
self.tabview.set_reorderable(True)
self.sw.add(self.tabview)
self.tabview.show()
self.create_columns(self.tabview)
def __init__(self):
#...
#creating the Window, entries,...
#...
self.button1 = gtk.Button('Buscar')
self.button1.connect('clicked', self.search, 'button 1')
#...
#packing and showing the whole GUI
#...
def create_columns(self, tabview):
rendererText = gtk.CellRendererText()
rendererText.props.wrap_width = 80
rendererText.props.wrap_mode = gtk.WRAP_WORD
column = gtk.TreeViewColumn('Codigo', rendererText, text=0)
column.set_sort_column_id(0)
self.tabview.append_column(column)
#...
#creating 5 more columns by the same principle
#...
def main(self):
gtk.main()
if __name__=='__main__':
base = BASE()
run = base
run.main()
thanks for your help, tell me if you need more information!
I do not know what is exactly wrong with your program (if you want to know that, you should provide a working minimal example).
Nevertheless, you do not need to manually update the TreeView because the TreeView always shows the content of the ListStore or TreeStore. You also do not have to create the ListStore everytime. You can create it one time and clear and insert the new entries when the event is emitted.
Here is a small example that shows how it works:
#from gi.repository import Gtk # uncomment to use PyGObject
import gtk as Gtk
class TestCase:
def __init__(self):
win = Gtk.Window()
button = Gtk.Button('Show')
button.connect('clicked', self.on_button_clicked)
self.clicked = 0
self.liststore = Gtk.ListStore(int)
self.liststore.append((self.clicked,))
view = Gtk.TreeView(self.liststore)
renderer = Gtk.CellRendererText()
column = Gtk.TreeViewColumn('demo', renderer, text=0)
view.append_column(column)
box = Gtk.HBox()
box.pack_start(button, True, True, 0)
box.pack_start(view, True, True, 0)
win.add(box)
win.connect('delete-event', Gtk.main_quit)
win.show_all()
def on_button_clicked(self, button):
self.clicked += 1
self.liststore.clear()
self.liststore.append((self.clicked,))
if __name__ == "__main__":
TestCase()
Gtk.main()
It also seems that you first create your csv file and read it afterwards. I don't know about the rest of your program but it is probably better to use the data directly and insert it into the ListStore and not read it from the csv file. It might be even possible then to just remove and insert the new entries and not clear the whole ListStore and insert everything again. That would be more efficient with bigger amounts of data.

How to sort items in Qt QListview using Qt.UserRole

I'm having some problem sorting the items in my QListView using values in a field I specified.
Basically what I'm trying to do is this:
Detect faces in a collection of photos and display them in a QListView
Cluster the faces (images)
Update the view by placing items in the list (which are face images) belonging to the same cluster in together. Concretely, if item 1, 3, 5 are in one cluster and items 2, 4, 6 are in another, then items 1, 3, 5 should be displayed (in whatever permutations) before any of items 2, 4, 6 are displayed or vice versa.
The way I went about doing this is to set one of the UserRole field for each QStandardItem in my list to the cluster label and then try to get the QStandardModel to sort according to this UserRole. This would then display items in the same cluster (i.e. with the same cluster label in the UserRole) next to each other.
I'm able to set the UserRole successfully for the items but calling the sort function on the QStandardModel did not sort the items even though when I set the sort role to be the default DisplayRole (i.e. sort according to the text label of each face) it worked as intended.
Can anyone tell me what is wrong with my code or offer an alternative method? I've googled sorting list and I found the following link on QSortFilterProxyModel but as I'm quite new to Qt, I'm not able to adapt it to my situation.
Thanks in advance to any replies.
Here is the relevant code:
import os
from PySide.QtGui import QListView, QStandardItemModel, QStandardItem, QIcon
from PySide.QtCore import Qt
class FacesView(QListView):
"""
View to display detected faces for user to see and label.
"""
UNCLUSTERED_LABEL = -1
CLUSTER_ROLE = Qt.UserRole + 1
def __init__(self, *args):
super(FacesView, self).__init__(*args)
self._dataModel = QStandardItemModel()
self.setModel(self._dataModel)
# Layout items in batches instead of waiting for all items to be
# loaded before user is allowed to interact with them.
self.setLayoutMode(QListView.Batched)
def updateFaceClusters(self, labels):
"""Update the cluster label for each face.
#param labels: [1 x N] array where each element is an integer
for the cluster the face belongs to."""
assert(len(labels) == self._dataModel.rowCount())
# Put the cluster label each item/face belong to in the
# CLUSTER_ROLE field.
for i in xrange(self._dataModel.rowCount()):
index = self._dataModel.index(i, 0)
self._dataModel.setData(index, labels[i], self.CLUSTER_ROLE)
# Use cluster label as sort role
self._dataModel.setSortRole(self.CLUSTER_ROLE)
# This does NOT seem to sort the items even though it works fine
# when sort role is the default Qt.DisplayRole.
self._dataModel.sort(0)
print("Finished updating face clusters")
def itemsInList(self):
"""Returns the label for a face and the path to its image.
#return: (label, path)"""
items = []
for i in xrange(self._dataModel.rowCount()):
label = self._dataModel.index(i, 0).data(Qt.DisplayRole)
imagePath = self._dataModel.index(i, 0).data(Qt.UserRole)
clusterLabel = self._dataModel.index(i, 0).data(self.CLUSTER_ROLE)
items.append((imagePath, label, clusterLabel))
return items
def addItem(self, label, imagePath):
"""Add an item to list view
#param label: The label associated with the item.
#param imagePath: Path to image for the icon."""
if os.path.exists(imagePath):
icon = QIcon(imagePath)
else:
icon = QIcon(':/res/Unknown-person.gif')
item = QStandardItem(icon, label)
item.setEditable(True)
# Add image path to the UserRole field.
item.setData(imagePath, Qt.UserRole)
# Add cluster label to image. CLUSTER_ROLE is where I intend
# to put the item's cluster label.
item.setData(self.UNCLUSTERED_LABEL, self.CLUSTER_ROLE)
# Prevent an item from dropping into another item.
item.setDropEnabled(False)
# Add item to list indirectly by adding it to the model.
self._dataModel.appendRow(item)
def clear(self):
self._dataModel.clear()
There's nothing wrong with the code you posted. So there must be something wrong with how you are using it. How are you generating the cluster labels?
Here's a test script using your FacesView class that sorts as you intended:
from random import randint
from PySide.QtGui import QWidget, QPushButton, QVBoxLayout, QApplication
from facesview import FacesView
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.list = FacesView(self)
self.button = QPushButton('Test', self)
self.button.clicked.connect(self.handleButton)
layout = QVBoxLayout(self)
layout.addWidget(self.list)
layout.addWidget(self.button)
def handleButton(self):
labels = []
self.list.model().setRowCount(0)
for row in range(10):
labels.append(randint(0, 3))
text = 'Item(%d) - Cluster(%d)' % (row, labels[-1])
self.list.addItem(text, 'icon.png')
self.list.updateFaceClusters(labels)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())

Categories