pyQt QTableView select a noncontiguous set of rows - python

I have a QTableView, which is created this way:
self.preset_delegate = PresetDelegate() # used to provide a combobox for making a selection from a set of options, column 0
self.model_filelist = QtGui.QStandardItemModel()
self.model_filelist.setHorizontalHeaderLabels(HEADER_LABELS)
self.list_filelist = QtGui.QTableView()
self.list_filelist.horizontalHeader().setResizeMode( QtGui.QHeaderView.Interactive )
self.list_filelist.setItemDelegateForColumn(0, self.preset_delegate )
self.list_filelist.setModel( self.model_filelist )
self.list_filelist.setSelectionMode( QtGui.QAbstractItemView.ExtendedSelection )
When the user presses a button, I would like to algorithmically select rows from the table. The selection will not be contiguous. For the purpose of our discussion, it could be any random subset of rows in the model/table.
This is pseudo-code for what I'm using to make the selection:
files = [str(self.model_filelist.data( self.model_filelist.index(x,1)).toString()) for x in range(self.model_filelist.rowCount())]
self.list_filelist.clearSelection()
for x in match_set:
match_index = files.index( x )
model_index = self.model_filelist.index(match_index,1) # first column is okay
self.list_filelist.selectionModel().select( model_index, QtGui.QItemSelectionModel.Select | QtGui.QItemSelectionModel.Current )
My problem is the with selection model flag on the very last line. Whether I use SelectCurrent, ToggleCurrent or Select | Current, or Toggle | Current, I only get the last item in my match_set remaining selected at the end of the loop. As the loop executes, the selection is changed from one item to the other, rather than adding the new row to the set of the selection. I hope that makes sense.
I thought for sure that SelectCurrent flag was the way to do this, but it's not working for me. Any suggestions? (python 2.6.7, Fedora 14, Qt4.4??? I can't be sure)

As per comments to the question. I did not solve why the original code failed, but I found this to work:
Use the version of select() that takes a QItemSelection object, and load that object with QItemSelectRange objects that wrap ModelIndex to the rows I'm interested in.

Related

How to find and change DataViewListCtrl BackgroundColour of Selected text in WxPython?

My problem is I can't find the command to change the background color of DataViewListCtrl for selected text/row/item in DataViewListCtrl object.
I looked into the documentation but there's no apparent reference.
https://docs.wxpython.org/wx.dataview.DataViewCtrl.html#wx-dataview-dataviewctrl
I'm using the sample_one.py script from this reference:
https://wiki.wxpython.org/How%20to%20add%20a%20menu%20bar%20in%20the%20title%20bar%20%28Phoenix%29
The DataViewListCtrl has this example code:
self.dvlc = dv.DataViewListCtrl(
self,
style=dv.DV_MULTIPLE
| dv.DV_ROW_LINES
| dv.DV_HORIZ_RULES
| dv.DV_VERT_RULES,
)
self.dvlc.SetBackgroundColour("#ffffff")
self.dvlc.SetForegroundColour("black")
What I'm looking to do is similar to Tkinter example below (change the selected text background color to blue):
style.configure(
"Treeview", background="#FFFFDD", foreground="black", fieldbackground="#FFFFDD"
)
style.map("Treeview", background=[("selected", "#F0FFFF")])
I haven't been able to try a workaround yet as I'm not sure how to get the needed command/value, but on my end the selected DataViewListCtrl items have a #242424 color (I checked with an eyedropper from a screenshot).
I found the 2 parameters IsSelected and IsRowSelected in the doc:
https://docs.wxpython.org/wx.dataview.DataViewListCtrl.html#wx.dataview.DataViewListCtrl.IsRowSelected
https://docs.wxpython.org/wx.dataview.DataViewCtrl.html#wx.dataview.DataViewCtrl.IsSelected
and tested as
self.dvlc = dv.DataViewListCtrl(
self,
style=dv.DV_MULTIPLE
| dv.DV_ROW_LINES
| dv.DV_HORIZ_RULES
| dv.DV_VERT_RULES,
)
# Give it some columns.
self.dvlc.AppendTextColumn("Id", width=40)
self.dvlc.AppendTextColumn("Artist", width=170)
self.dvlc.AppendTextColumn("Title", width=260)
self.dvlc.AppendTextColumn("Genre", width=80)
# Load the data. Each item (row) is added as a sequence
# of values whose order matches the columns.
for itemvalues in musicdata:
self.dvlc.AppendItem(itemvalues)
# — 1st test with same result as 2nd below
# if self.dvlc.IsSelected == True:
# self.dvlc.SetBackgroundColour("#0066ff")
# else:
# self.dvlc.SetBackgroundColour("#F0FFFF")
if self.dvlc.IsRowSelected == True:
self.dvlc.SetBackgroundColour("#0066ff")
else:
self.dvlc.SetBackgroundColour("#F0FFFF")
self.dvlc.SetForegroundColour("black")
I tried also without the == True explicit as
# — 1st test with same result as 2nd below
# if self.dvlc.IsSelected:
# self.dvlc.SetBackgroundColour("#0066ff")
# else:
# self.dvlc.SetBackgroundColour("#F0FFFF")
if self.dvlc.IsRowSelected:
self.dvlc.SetBackgroundColour("#0066ff")
else:
self.dvlc.SetBackgroundColour("#F0FFFF")
and the unexpected blue background displays by default (when not selected).
Is there some documented "selected" parameter or otherwise known working method to be able to capture the selected background color and change it in WxPython?
You will have to use a custom renderer if you want to change the way the selected items background is rendered -- and you will have to overwrite the entire background entirely, as the default one will still be drawn, you will just be able to paint over it.
Alternatively, you can define a custom wxRendererNative-derived object and override its DrawItemSelectionRect() method and then set it as the renderer to use globally. This should be simpler, but it will change selection drawing in all controls using this function.

Clearing the background color of rows that are dropped into a QTableView

I have two QTableViews with different QStandardItemModels that are placed next to each other on my layout. One of the tableviews accepts drops from the other and that has been working well so far.
This is how the model is set up:
self.auto_queue_model = QStandardItemModel()
self.auto_queue.setModel(self.auto_queue_model)
The table rows that are dropped into the tableview come with two different background colors. I am having some trouble implementing a way for those background colors to be deleted or reset once the rows are dropped to the second tablewidget.
I have tried installing an event filter like so:
self.auto_queue.installEventFilter(self)
def eventFilter(self, source, event):
if source.objectName() == 'auto_queue':
if event.type() == QEvent.Drop:
for row in range(0, self.auto_queue_model.rowCount()):
item = self.auto_queue_model.item(row, 0)
if item:
item.setBackground(QtGui.QColor(70, 70, 70))
return super().eventFilter(source, event)
But I can not get PyQt to recognize the event.type() as QEvent.Drop even though it is in the documentation
https://doc.qt.io/qt-5/dnd.html
I have also tried connecting a signal to a function like so:
self.auto_queue_model.rowsInserted.connect(self.resetAutoQueueColors)
def resetAutoQueueColors(self, index, column, row):
print(column,row, self.auto_queue_model.rowCount())
for row in range(0, self.auto_queue_model.rowCount()):
item = self.auto_queue_model.item(row, 0)
if item:
item.setBackground(QtGui.QColor(70, 70, 70))
I have tried both changing the background of the dropped row(s) that are received by the resetAutoQueueColors function individually and looping over all rows like in the sample above. The problem seems to be that the signal is fired before the rows are actually added to the model. It then changes the background of all rows except for the ones I have dropped. The new rows are added to the rowCount(), but the item at that index location returns None at the time of the execution of the function.
i have also tried to subclass the QTableView and emitting a signal, but have been unsuccessful.
Also, I am wondering if there is a way to reset the background color instead of setting it explicitly.
Any help would be much appreciated.

pygtk cellrenderertoggle is not checked

I have the following code:
self.db='checks.db'
self.con = lite.connect(self.db)
self.cur = self.con.cursor()
self.q_oblig_initial='SELECT data_plirotees.rowid as rowid,recdate,bank,amount,dueto,gto,plirstatus FROM data_plirotees WHERE plirstatus=0 ORDER BY dueto ASC'
self.store_oblig = gtk.ListStore(int,str,str,str,str,str,bool)
self.cur.execute(q_oblig)
self.data_oblig=self.cur.fetchall()
for value in self.data_oblig:
if value[6]==0:
plir=False
elif value[6]==1:
plir=True
self.store_oblig.append([value[0],datetime.datetime.fromtimestamp(int(value[1])).strftime('%d/%m/%Y'),value[2],"%.2f" %(value[3]),datetime.datetime.fromtimestamp(int(value[4])).strftime('%d/%m/%Y'),value[5],plir])`
which gets data from a sqlite database and puts it in a liststore and,
rendererToggle.connect("toggled", self.on_cell_toggled)
column_toggle = gtk.TreeViewColumn("Καλύφθηκε", rendererToggle, active=1)
column_toggle.set_fixed_width(10)
treeView_oblig.append_column(column_toggle)
which has to show it in a column where true should show a checked toggle/checkbox and false should show un-checked.
Unfortunately this doesn't happen.
The checkbox needs not to be active (i don't want it to be able to toggle) but by clicking on the treeview row it opens a new window (where a checkbutton is checked or not accordingly). From that I understand that the true/false value is contained there somewhere but it is not presented visually.
Can someone show me where I'm wrong?
I didn't post the whole program 'cause it would be too big and perhaps misguiding...
self.store_oblig = gtk.ListStore(int,str,str,str,str,str,bool)
This line creates a GtkListStore where each column is of a different type. The columns are numbered from left to right, starting at 0:
self.store_oblig = gtk.ListStore(int,str,str,str,str,str,bool)
column number 0 1 2 3 4 5 6
You create your GtkTreeViewColumn with this:
column_toggle = gtk.TreeViewColumn("Καλύφθηκε", rendererToggle, active=1)
This says that the column's cell renderer should get the value of its active property from column 1 of the model (in this case, the list store). And the active property expects a bool.
But if you look back above, column 1 isn't a bool, but rather a string! So what you really wanted was active=6, not active=1. (Your code to add to the list store, on the other hand, seems correct.)
This is what the warning Warning: unable to set property 'active' of type 'gboolean' from value of type 'gchararray' gtk.main() is trying to tell you; gchararray is (one of) GLib's internal name(s) for a string.

PyQt - get list of all checked in QTreeWidget

I am building a simple application to open up a folder of data and plot that data. Importing the data updates a QTreeWidget that shows which signals are available to plot. Ex:
The QTreeWidget is populated after the data is imported using:
def update_treeWidget(self):
headers = self.df['voltage recording'].columns[1:]
sweeps = self.df['voltage recording'].index.levels[0]
for header in headers:
parent_item = QtWidgets.QTreeWidgetItem(self.treeWidget)
parent_item.setCheckState(0, QtCore.Qt.Unchecked)
parent_item.setText(0, header)
for sweep in sweeps:
child_item = QtWidgets.QTreeWidgetItem(parent_item)
child_item.setCheckState(0, QtCore.Qt.Unchecked)
child_item.setText(0, sweep)
It is unclear to me, though, how I would check the checked status for any of the items now in the tree.
So my two questions are:
How would I refer to any part of that tree to check it's checked status (either the parent, or the various children of a parent)
Is there a way to simply return a list of all of the boxes that are checked? If I can figure out the answer to Question 1 I can obviously create a function where, every time a box is checked it is added to a list (and removed from that list if it is unchecked). But since the goal here is simply to plot all of the checked signals, the most straightforward thing to me (logically anyway) is upon hitting the "Plot" button the interface first checks to see which signals boxes are checked and then plots those signals.
In all of the examples I have seem the tree items are explicitly declared (i.e. item_1, item_2), so referring to them is simple. Because of how I am populating the tree, though, I don't understand how to do that.
If this is an incorrect approach to populating the tree in the first place, please let me know and/or point me in the direction of a more correct approach.
Thanks
Edit:
This is very similar to:
PyQT QTreeWidget iterating
which is what I based my answer off of.
Figured it out. This at least achieves what I need it to achieve:
def find_checked(self):
checked = dict()
root = self.treeWidget.invisibleRootItem()
signal_count = root.childCount()
for i in range(signal_count):
signal = root.child(i)
checked_sweeps = list()
num_children = signal.childCount()
for n in range(num_children):
child = signal.child(n)
if child.checkState(0) == QtCore.Qt.Checked:
checked_sweeps.append(child.text(0))
checked[signal.text(0)] = checked_sweeps
return checked
Five years later I was searching for an answer to your second question. While dan_g's code was very helpful I believe it will not list all items if the tree depth is greater than 2. I created a recursive version that will list all selected items regardless of their level in the tree.
def get_selected_items(self):
checked_items = []
def recurse(parent_item):
for i in range(parent_item.childCount()):
child = parent_item.child(i)
grand_children = child.childCount()
if grand_children > 0:
recurse(child)
if child.checkState(0) == Qt.Checked:
checked_items.append(child.text(0))
recurse(self.ui.treeWidget.invisibleRootItem())
return checked_items
Or in my case if you only want to list selected items at the leaves of the tree just add an else statement.
def get_selected_leaves(self):
checked_items = []
def recurse(parent_item):
for i in range(parent_item.childCount()):
child = parent_item.child(i)
grand_children = child.childCount()
if grand_children > 0:
recurse(child)
else:
if child.checkState(0) == Qt.Checked:
checked_items.append(child.text(0))
recurse(self.ui.treeWidget.invisibleRootItem())
return checked_items
In either case the function calls itself whenever it finds an item that has children until it traverses the entire tree.
Next solution can be used:
for item in self.QTreeWidget.findItems("", Qt.MatchContains | Qt.MatchRecursive):
if (item.rowCount() == 0 and item.checkState()>0):
print (item.text(),item.checkState())

Qt and PyQt tablewidget growing row count

I am learning Python and trying to write a program to help my dad.
I want it to be excel-like, and it looks great for now, but I have no idea how to make the number of rows (not the columns) in a tableWidget to grow while someones crolls down...
Can I do it using QtDesigner or I have to write the code in the .py file?
Any help is appreciated...
Sorry if I am asking silly questions, I an a noob really...
Here is sort of a proof-of-concept example:
class Widget(QtGui.QWidget):
def __init__(self):
super(Widget, self).__init__()
self.resize(600,400)
layout = QtGui.QVBoxLayout(self)
self.table = QtGui.QTableWidget(20,10)
self.vBar = self.table.verticalScrollBar()
self._vBar_lastVal = self.vBar.value()
layout.addWidget(self.table)
self.vBar.valueChanged.connect(self.scrollbarChanged)
def scrollbarChanged(self, val):
bar = self.vBar
minVal, maxVal = bar.minimum(), bar.maximum()
avg = (minVal+maxVal)/2
rowCount = self.table.rowCount()
# scrolling down
if val > self._vBar_lastVal and val >= avg:
self.table.insertRow(rowCount)
# scrolling up
elif val < self._vBar_lastVal:
lastRow = rowCount-1
empty = True
for col in xrange(self.table.columnCount()):
item = self.table.item(lastRow, col)
if item and item.text():
empty=False
break
if empty:
self.table.removeRow(lastRow)
self._vBar_lastVal = val
You have to rely on the vertical scroll bar of the table widget to signal information about it changing value. So we connect its valueChanged(int) signal to our method.
The scrollbarChanged SLOT will receive the value of the scrollbar. What I am doing here is checking the min and max value of the scrollbar at that moment, and seeing if the current position is at least in the middle. We also check if the current scrollbar value is greater than the last time, because we only want to add rows on a down scroll. If these conditions are true, then we insert a new row.
The act of shrinking it back down is a bit more involved because I am sure you will need to check the last row to make sure it is empty and only remove it if so. My version gives you the rough idea but the math logic would probably need more work. But what it is doing is going through every item in the last row to see if its None or an empty value. If the whole row is empty, it removes it. Thus, you do get a reducing effect when scrolling back up.
Hope this gives you a starting point! Feel free to ask about the code if you need more detailed explanation of any parts.

Categories

Resources