selected item of comboBox in custom Delegate from QTableView - combobox

I use a custom delegate to display a column of comboBoxes in my QTableView.
The values are the same for all the comboBoxes so it's not really the population part that gives me trouble.
I want them to show as the selected item, some value that I can retrieve from a database. I have access to the database from the delegate, but in order to send my request, I need the row of the comboBox.
So I guess my question is : how can you iterate over all the rows of the table and do some action from inside the custom delegate ?
If it can help here is my custom delegate class :
class ComboBoxDelegate(QtGui.QItemDelegate):
def __init__(self, parent, itemslist):
QtGui.QItemDelegate.__init__(self, parent)
self.itemslist = itemslist
self.parent = parent
def paint(self, painter, option, index):
# Get Item Data
value = index.data(QtCore.Qt.DisplayRole).toInt()[0]
# value = self.itemslist[index.data(QtCore.Qt.DisplayRole).toInt()[0]]
# fill style options with item data
style = QtGui.QApplication.style()
opt = QtGui.QStyleOptionComboBox()
opt.currentText = str(self.itemslist[value])
opt.rect = option.rect
# draw item data as ComboBox
style.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt, painter)
self.parent.openPersistentEditor(index)
def createEditor(self, parent, option, index):
##get the "check" value of the row
# for row in range(self.parent.model.rowCount(self.parent)):
# print row
self.editor = QtGui.QComboBox(parent)
self.editor.addItems(self.itemslist)
self.editor.setCurrentIndex(0)
self.editor.installEventFilter(self)
self.connect(self.editor, QtCore.SIGNAL("currentIndexChanged(int)"), self.editorChanged)
return self.editor
# def setEditorData(self, editor, index):
# value = index.data(QtCore.Qt.DisplayRole).toInt()[0]
# editor.setCurrentIndex(value)
def setEditorData(self, editor, index):
text = self.itemslist[index.data(QtCore.Qt.DisplayRole).toInt()[0]]
pos = self.editor.findText(text)
if pos == -1:
pos = 0
self.editor.setCurrentIndex(pos)
def setModelData(self,editor,model,index):
value = self.editor.currentIndex()
model.setData(index, QtCore.QVariant(value))
def updateEditorGeometry(self, editor, option, index):
self.editor.setGeometry(option.rect)
def editorChanged(self, index):
check = self.editor.itemText(index)
id_seq = self.parent.selectedIndexes[0][0]
update.updateCheckSeq(self.parent.db, id_seq, check)
And I call it fromthe QTableView like this :
self.setEditTriggers(QtGui.QAbstractItemView.CurrentChanged)
self.viewport().installEventFilter(self)
self.setItemDelegateForColumn(13,ComboBoxDelegate(self, self.checkValues))
Hope I was clear enough, thanks for your attention

Not sure if accessing the database from the delegate is a right thing to do. Your delegate can contain reference to the instance of QAbstractTableModel which the QTableView refers to. You can then use methods in the model to iterate over rows of the table.

Related

PySide6 QTreeWidget Checkbox SingleSelection

have a PySide6 QTreeWidget with some Elements and Checkboxes, very simple. What i cannot getting to work is how can i make s Single Selection with the Checkboxes? What works is SingleSelection without the Checkboxes, but not when i only use the Checkboxes itself. I wann use only Checkboxes and not Mouseclicks on the row, i did that with
tv.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
tv.setFocusPolicy(QtCore.Qt.NoFocus)
My Idea was set a itemChanged handler function for it and when a checkbox is clicked i run through all items, set all item checkboxes to uncheck with
child.setCheckState(0, QtCore.Qt.Unchecked)
and after that set the item that is selected from the handler to checked.
def handle_statechange(item):
selected_item = item.text(0)
for i in range(tv.invisibleRootItem().childCount()):
child = tv.invisibleRootItem().child(i)
child.setCheckState(0, QtCore.Qt.Unchecked)
item.setCheckState(0, QtCore.Qt.Checked)
But that doesnt work. How can i make this behavior? Thanks!
Here is small sample Code
import sys
from PySide6 import QtGui, QtCore, QtWidgets
testdict = {'TEST1': 'Testname1',
'TEST2': 'Testname2',
'TEST3': 'Testname3',
'TEST4': 'Testname4',
}
app = QtWidgets.QApplication(sys.argv)
widget = QtWidgets.QWidget()
widget.setWindowTitle("test")
widget.grid = QtWidgets.QGridLayout(widget)
widget.grid.setContentsMargins(5, 5, 5, 5)
widget.hide()
tv = QtWidgets.QTreeWidget()
tv.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
tv.setIndentation(0)
tv.setColumnCount(4)
tv.setFocusPolicy(QtCore.Qt.NoFocus)
tv.hideColumn(2)
tv.hideColumn(3)
tv.setHeaderLabels(['NORM', 'NAME'])
tv.header().setDefaultAlignment(QtCore.Qt.AlignLeft|QtCore.Qt.AlignLeft)
strlen_list = []
for k, v in testdict.items():
strlen_list.append(len(v))
TreeNodeItem = QtWidgets.QTreeWidgetItem
treeNode = TreeNodeItem(tv, k)
treeNode.setText( 0, k )
treeNode.setText( 1, v )
treeNode.setText( 2, '512' )
treeNode.setText( 3, '513' )
treeNode.setCheckState(0, QtCore.Qt.Unchecked)
widget.grid.addWidget(tv, 0, 0, QtCore.Qt.AlignTop)
label = QtWidgets.QLabel()
label.setText("Testlabel")
widget.grid.addWidget(label, 1, 0, QtCore.Qt.AlignTop)
button = QtWidgets.QPushButton('Testbutton', widget)
button.clicked.connect(test)
widget.grid.addWidget(button, 2, 0, QtCore.Qt.AlignLeft)
widget.show()
The check state change of items has absolutely nothing to do with the selection and is not directly correlated to mouse clicks, since the user could click on an item but not on its checkbox, or could press the space-bar to toggle the check state of the current item.
Assuming that you toggle the check state of items ONLY using mouse/keyboard and you will always have only one checked item at most, you can connect to the itemChanged signal and verify if any of its columns has a checked state.
Then you have to store the checked item and column so that you can later verify if any other item has changed and it's different from the previously set checked item.
class ToggleTree(QtWidgets.QTreeWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.lastToggled = None
self.itemChanged.connect(self.checkToggled)
def checkToggled(self, item):
for column in range(item.columnCount()):
if item.checkState(column):
break
else:
if self.lastToggled and self.lastToggled[0] == item:
self.lastToggled = None
return
if self.lastToggled:
oldItem, oldColumn = self.lastToggled
if oldItem != item:
# prevent recursion
self.itemChanged.disconnect(self.checkToggled)
oldItem.setCheckState(oldColumn, 0)
self.itemChanged.connect(self.checkToggled)
self.lastToggled = item, column
# ...
tv = ToggleTree()
Note: if you only need a 2-dimensional model like in your example, don't use QTreeWidget, but QTableWidget.

combo box using different display text and return value [duplicate]

This question already has an answer here:
Trying to take a value from a dictionary of a ComboBox and instert the key in a textEdit
(1 answer)
Closed 2 years ago.
I have a drop down which the displayed text is populated from a csv:
fish_events_terms = gpd.read_file("domains/FISH/events/thesaurus_terms.csv")
self.comboActivityType.addItems(list(map(lambda x: x.upper(), fish_events_terms['TERM'])))
I want to display the above but record the uid of that value in this case CLA_GR_UID
So the user sees some text from the TERM column and the value of CLA_GR_UID is passed.
I'm not sure if I understand the question correctly, but if you want to store extra data in an item in addition to the displayed text, you could add the items on-by-one by using QComboBox.addItem(text, user_data), i.e.
from PyQt5 import QtWidgets, QtCore
import pandas as pd
class Widget(QtWidgets.QWidget):
def __init__(self, parent = None):
super().__init__(parent)
self.combo = QtWidgets.QComboBox(self)
# some data
self.df = pd.DataFrame({'TERM': ['apple', 'banana', 'cherry', 'date', 'grape'],
'UID': [1, 2, 3, 4, 5]})
# for each row in dataframe, add item with value in 'TERM' column as text and value in 'UID' column as data
for row in self.df.itertuples():
self.combo.addItem(row.TERM, row.UID)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.combo)
self.combo.currentIndexChanged.connect(self.combo_index_changed)
def combo_index_changed(self, index):
# retrieve user data of an item in combo box via QComboBox.itemData()
print(f'index {index}, text {self.combo.itemText(index)}, uid {self.combo.itemData(index)}')
if __name__ == "__main__":
app = QtWidgets.QApplication([])
w = Widget()
w.show()
app.exec()

How to create a completely (uniformly) random dataset on PyTorch

I need to run some experiments on custom datasets using pytorch. The question is, how can I create a dataset using torch.Dataloader?
I have two lists, one is called Values and has a datapoint tensor at every entry, and the other one is called Labels, that has the corresponding label. What I did is the following:
for i in range(samples):
dataset[i] = [values[i],labels[I]]
So I have a list with datapoint and respective label, and then tried the following:
dataset = torch.tensor(dataset).float()
dataset = torch.utils.data.TensorDataset(dataset)
data_loader = torch.utils.data.DataLoader(dataset=dataset, batch_size=100, shuffle=True, num_workers=4, pin_memory=True)
But, first of all, I get the error "Not a sequence" in the torch.tensor command, and second, I'm not sure this is the right way of creating one. Any suggestion?
Thank you very much!
You do not need to overload DataLoader, but rather create a Dataset for your data.
For instance,
class MyDataset(Dataset):
def __init__(self):
super(MyDataset, self).__init__()
# do stuff here?
self.values = values
self.labels = labels
def __len__(self):
return len(self.values) # number of samples in the dataset
def __getitem__(self, index):
return self.values[index], self.labels[index]
Just to enrich the answer by #shai
class MyDataset(Dataset):
def __init__(self, values):
super(MyDataset, self).__init__()
self.values = values
def __len__(self):
return len(self.values)
def __getitem__(self, index):
return self.values[index]
values = np.random.rand(51000, 3)
dataset = MyDataset(values)

Is there a way to use/get the value from a current entry window

I'm trying to get the variable that's entered in an entry widget on the Return key pressed event, but struggling a bit. What I have tried has always produced a blank result.
This code may look messy and hap-hazard, but it's only going to be a template that I'll be using on a current project!
I've tried that many things to get it to work, I can't remember what I have tried!
from collections import OrderedDict
try:
import tkinter as tk
except:
import Tkinter as tk
root = tk.Tk()
labelLIST = OrderedDict([
('Temp ID', 'tempID'),
('PO Number', "poNumber"),
('Reference', "reference"),
('Cut/Sample Date', "csDate"),
('Cut Number', "cut")
])
i = 0
e_loops = len(labelLIST)
print (e_loops)
def bval1(event=None):
for i in range(e_loops):
print (entries[i].get())
entries[0].delete(0, tk.END)
entries[0].insert(0, 'DISABLED')
entries[0].configure(state='disabled')
def bval2():
entries[0].configure(state='normal')
for i in range(e_loops):
entries[i].delete(0, tk.END)
entries[0].focus()
def onClick(event):
ent = event.widget # event.widget is the widget that called the event
print(ent.cget("text")) # Print the text for the selected button
event.widget.tk_focusNext().focus()
def enterEV(event):
# print(entries[].get())
event.widget.tk_focusNext().focus()
entries = []
for key, value in labelLIST.items():
label = tk.Label(root, text=key)
label.grid(row=i, column=0, sticky="ew", padx=1, pady=1)
entry = tk.Entry(root, width=10)
entry.grid(row=i, column=1, sticky="ew", padx=5, pady=5)
if value == "cut":
entry.bind('<Return>', bval1)
else:
# entry.bind('<Return>', enterEV)
entry.bind('<Return>', onClick)
entries.append(entry)
i = i+1
button = tk.Button(root, text="Submit", command=bval1)
button.grid(row=0, column=2, columnspan=9, sticky="ew")
button = tk.Button(root, text="Clear", command=bval2)
button.grid(row=1, column=2, columnspan=9, sticky="ew")
entries[0].focus()
tk.mainloop()
When enter/return is pressed, I want the value that is the entry box to be printed to terminal via the onClick event. But the output is always empty.
def onClick(event):
ent = event.widget # event.widget is the widget that called the event
print(ent.cget("text")) # Print the text for the selected button
event.widget.tk_focusNext().focus()
You don't use the text attribute to get the value in an Entry widget. Using cget("text") returns the value for the textvariable attribute. Since you haven't set that attribute, it will always be the empty string.
Instead, you need to call the get method:
print(ent.get())

overwrite existing entity via bulkloader.Loader

I was going to CSV based export/import for large data with app engine. My idea was just simple.
First column of CSV would be key of entity.
If it's not empty, that row means existing entity and should overwrite old one.
Else, that row is new entity and should create new one.
I could export key of entity by adding key property.
class FrontExporter(bulkloader.Exporter):
def __init__(self):
bulkloader.Exporter.__init__(self, 'Front', [
('__key__', str, None),
('name', str, None),
])
But when I was trying to upload CSV, it had failed because bulkloader.Loader.generate_key() was just for "key_name" not "key" itself. That means all exported entities in CSV should have unique 'key_name' if I want to modify-and-reupload them.
class FrontLoader(bulkloader.Loader):
def __init__(self):
bulkloader.Loader.__init__(self, 'Front', [
('_UNUSED', lambda x: None),
('name', lambda x: x.decode('utf-8')),
])
def generate_key(self,i,values):
# first column is key
keystr = values[0]
if len(keystr)==0:
return None
return keystr
I also tried to load key directly without using generate_key(), but both failed.
class FrontLoader(bulkloader.Loader):
def __init__(self):
bulkloader.Loader.__init__(self, 'Front', [
('Key', db.Key), # not working. just create new one.
('__key__', db.Key), # same...
So, how can I overwrite existing entity which has no 'key_name'? It would be horrible if I should give unique name to all entities.....
From the first answer, I could handle this problem. :)
def create_entity(self, values, key_name=None, parent=None):
# if key_name is None:
# print 'key_name is None'
# else:
# print 'key_name=<',key_name,'> : length=',len(key_name)
Validate(values, (list, tuple))
assert len(values) == len(self._Loader__properties), (
'Expected %d columns, found %d.' %
(len(self._Loader__properties), len(values)))
model_class = GetImplementationClass(self.kind)
properties = {
'key_name': key_name,
'parent': parent,
}
for (name, converter), val in zip(self._Loader__properties, values):
if converter is bool and val.lower() in ('0', 'false', 'no'):
val = False
properties[name] = converter(val)
if key_name is None:
entity = model_class(**properties)
#print 'create new one'
else:
entity = model_class.get(key_name)
for key, value in properties.items():
setattr(entity, key, value)
#print 'overwrite old one'
entities = self.handle_entity(entity)
if entities:
if not isinstance(entities, (list, tuple)):
entities = [entities]
for entity in entities:
if not isinstance(entity, db.Model):
raise TypeError('Expected a db.Model, received %s (a %s).' %
(entity, entity.__class__))
return entities
def generate_key(self,i,values):
# first column is key
if values[0] is None or values[0] in ('',' ','-','.'):
return None
return values[0]
Your best option is probably to override create_entity. You'll need to copy most of the existing code there, but modify the constructor to supply a key argument instead of a key_name argument.

Resources