I've created a table view where I'm trying to drop rows by clicking the checkbox embedded in the table view. I get to the point where it indexes the correct row when the checkbox is deselected, but the model refuse to remove the row and instead returns 'False'. Any thoughts? Still new to tableviews after switching from tablewidgets
class pandasModel(QAbstractTableModel):
def __init__(self, data, check_col=[0, 0]):
QAbstractTableModel.__init__(self)
self._data = data
self._check = check_col
cs = 0
if check_col[1]: cs = 2
self._checked = [[cs for i in range(self.columnCount())] for j in range(self.rowCount())]
def rowCount(self, parent=QModelIndex):
return self._data.shape[0]
def columnCount(self, parent=QModelIndex):
return self._data.shape[1]
def data(self, index, role):
if index.isValid():
data = self._data.iloc[index.row(), index.column()]
if role == Qt.DisplayRole:
if isinstance(data, float):
return f'{data:,.2f}'
elif isinstance(data, (int, np.int64)):
return f'{data:,}'
else:
return str(data)
elif role == Qt.TextAlignmentRole:
if isinstance(data, (float, int, np.int64)):
return Qt.AlignVCenter + Qt.AlignRight
elif role == Qt.CheckStateRole:
if self._check[0] > 0:
if self._check[0] == index.column():
return self._checked[index.row()][index.column()]
return None
def flags(self, index):
if not index.isValid(): return
return Qt.ItemIsSelectable | Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsEditable
def setData(self, index, value, role):
if not index.isValid() or role != Qt.CheckStateRole: return False
self._checked[index.row()][index.column()] = value
if value == 0:
print('checked')
self.removeRow(index.row()) #THIS IS WHERE IT RETURNS FALSE
self.dataChanged.emit(index, index)
return True
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self._data.columns[col]
return None
Related
i want to set a comboBox and show my table's column1 data as items and associated value column2 is for selected item id and want to setText in a qLabel.
i am using a model to view the items in comboBox and working fine. i can get the currentText value but how to get the associated value for the items.
in my case :
my sql table like:
type id
------------
DIV 2
TRANS 33
FEE 41
EXP 89
now , i can set the column1(type) values into comboBox successfully. now, if user selected value 'FEE' , than qlable should be updated as its associate id : 41. how to do that!
df=loadData()
model=PandasModel(df)
self.comboBox.setModel(model)
self.comboBox.setModelColumn(0)
content=self.comboBox.currentText()
self.label.setText(content) # content should be ID instead of currentText
pandasMode:
class PandasModel(QtCore.QAbstractTableModel):
def __init__(self, df = pd.DataFrame(), parent=None):
QtCore.QAbstractTableModel.__init__(self, parent=parent)
self._df = df
def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
if role != QtCore.Qt.DisplayRole:
return QtCore.QVariant()
if orientation == QtCore.Qt.Horizontal:
try:
return self._df.columns.tolist()[section]
except (IndexError, ):
return QtCore.QVariant()
elif orientation == QtCore.Qt.Vertical:
try:
return self._df.index.tolist()[section]+1
except (IndexError, ):
return QtCore.QVariant()
def data(self, index, role=QtCore.Qt.DisplayRole):
if index.isValid():
if role == QtCore.Qt.DisplayRole:
return str(self._df.iloc[index.row(), index.column()])
return None
def setData(self, index, value, role):
if not index.isValid():
return False
if role != QtCore.Qt.EditRole:
return False
row = index.row()
if row < 0 or row >= len(self._df.values):
return False
column = index.column()
if column < 0 or column >= self._df.columns.size:
return False
self._df.values[row][column] = value
self.dataChanged.emit(index, index)
return True
# def rowCount(self, parent=QtCore.QModelIndex()):
# return len(self._df.index)
def rowCount(self, parent=None):
return len(self._df.index)
def columnCount(self, parent=QtCore.QModelIndex()):
return len(self._df.columns)
def sort(self, column, order):
colname = self._df.columns.tolist()[column]
self.layoutAboutToBeChanged.emit()
self._df.sort_values(colname, ascending= order == QtCore.Qt.AscendingOrder, inplace=True)
self._df.reset_index(inplace=True, drop=True)
self.layoutChanged.emit()
Assuming that the model stores the id in the second column then it has to obtain the associated index:
ID_COLUMN = 1
index = self.comboBox.model().index(
self.comboBox.currentIndex(), ID_COLUMN, self.comboBox.rootModelIndex()
)
id_ = index.data()
print(id_)
self.label.setText(id_)
I'm relatively new to Python and especially PyQt and model-view programming. I want to have it so that someone can only enter integers in my table and not letters/symbols etc. Here's the model I've made so far for my tableView widget:
class PixelTableModel(QtCore.QAbstractTableModel):
def __init__(self):
super(PixelTableModel, self).__init__()
self.pixel_coordinate = [[None, None, None, None]]
def rowCount(self, parent):
return 1
def columnCount(self, parent):
return 4
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
def setData(self, index, value, role = QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
self.pixel_coordinate[row][column] = value
print(self.pixel_coordinate) #testing
return True
return False
def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
value = self.pixel_coordinate[row][column]
return value
def headerData(self, section, orientation, role): # section = row column, orientation = vertical/horizontal
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
dict = {0: "Xstart", 1: "Ystart", 2: "Xmax", 3: "Ymax"}
for key in dict:
if section == key:
return dict[key]
else:
return "Pixel coordinate"
It seems to work except obviously for the part where it still can take letters/symbols as input in the tableView. I've tried a couple of things in the setData() method but can't seem to get it work, always get some type of error or it won't even change the box at all. Thanks to anyone that can help me with this. Also sorry for bad English.
For anyone still interested, after going through it again fixed it with simple try except block:
def setData(self, index, value, role = QtCore.Qt.EditRole):
if role == QtCore.Qt.EditRole:
try:
row = index.row()
column = index.column()
string_to_int = int(value)
self.pixel_coordinate[row][column] = value
print(self.pixel_coordinate) #testing
return True
except ValueError:
print("Not a number")
return False
return False
I have this code:
class ManifestModel(QtSql.QSqlTableModel):
def __init__(self, parent=None, db=QtSql.QSqlDatabase()):
super(ManifestModel, self).__init__(parent, db)
def flags(self, index):
if (index.column() == 4):
return QtCore.Qt.ItemIsEnabled
elif (index.column() == 6):
return QtCore.Qt.ItemIsEnabled
elif (index.column() == 3):
return QtCore.Qt.AlignHCenter
else:
return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable
When I run it I get an error:
builtins.TypeError: invalid result from ManifestModel.flags(), AlignmentFlag cannot be converted to PyQt5.QtCore.ItemFlags in this context
In the same routine that uses ManifestModel, I have the code:
ui.label = QtWidgets.QLabel(ui.page)
ui.label.setGeometry(QtCore.QRect(308, 0, 131, 20))
ui.label.setAlignment(QtCore.Qt.AlignCenter)
So what do I have to do to change the alignment in a QTableView column?
The aligment is handled in the data() method of the model. So try something like:
class ManifestModel(QtSql.QSqlTableModel):
def data(self, index, role=QtCore.Qt.DisplayRole):
if role == QtCore.Qt.TextAlignmentRole and index.column() == 3:
return QtCore.Qt.AlignHCenter
return super(ManifestModel, self).data(index, role)
I have an abstract item model. which is displayed as a tree.
The purpose is to store a set of preferences.
The code for the qt object is:
class QPreferenceModel(QAbstractItemModel):
'Convention states only items with column index 0 can have children'
#report_thread_error
def __init__(self, pref_struct, parent=None):
super(QPreferenceModel, self).__init__(parent)
self.rootPref = pref_struct
#report_thread_error
def index2Pref(self, index=QModelIndex()):
'''Internal helper method'''
if index.isValid():
item = index.internalPointer()
if item:
return item
return self.rootPref
#-----------
# Overloaded ItemModel Read Functions
#report_thread_error
def rowCount(self, parent=QModelIndex()):
parentPref = self.index2Pref(parent)
return parentPref.qt_row_count()
#report_thread_error
def columnCount(self, parent=QModelIndex()):
parentPref = self.index2Pref(parent)
return parentPref.qt_col_count()
#report_thread_error
def data(self, index, role=Qt.DisplayRule):
'''Returns the data stored under the given role
for the item referred to by the index.'''
if not index.isValid():
return QVariant()
if role != Qt.DisplayRole and role != Qt.EditRole:
return QVariant()
nodePref = self.index2Pref(index)
data = nodePref.qt_get_data(index.column())
var = QVariant(data)
print('---')
print('data = %r' % data)
print('type(data) = %r' % type(data))
#if isinstance(data, float):
#var = var.toDouble()[0]
print('var= %r' % var)
return var
#report_thread_error
def index(self, row, col, parent=QModelIndex()):
'''Returns the index of the item in the model specified
by the given row, column and parent index.'''
if parent.isValid() and parent.column() != 0:
return QModelIndex()
parentPref = self.index2Pref(parent)
childPref = parentPref.qt_get_child(row)
if childPref:
return self.createIndex(row, col, childPref)
else:
return QModelIndex()
#report_thread_error
def parent(self, index=None):
'''Returns the parent of the model item with the given index.
If the item has no parent, an invalid QModelIndex is returned.'''
if index is None: # Overload with QObject.parent()
return QObject.parent(self)
if not index.isValid():
return QModelIndex()
nodePref = self.index2Pref(index)
parentPref = nodePref.qt_get_parent()
if parentPref == self.rootPref:
return QModelIndex()
return self.createIndex(parentPref.qt_parents_index_of_me(), 0, parentPref)
#-----------
# Overloaded ItemModel Write Functions
#report_thread_error
def flags(self, index):
'Returns the item flags for the given index.'
if index.column() == 0:
# The First Column is just a label and unchangable
return Qt.ItemIsEnabled | Qt.ItemIsSelectable
if not index.isValid():
return Qt.ItemFlag(0)
childPref = self.index2Pref(index)
if childPref:
if childPref.qt_is_editable():
return Qt.ItemIsEditable | Qt.ItemIsEnabled | Qt.ItemIsSelectable
return Qt.ItemFlag(0)
#report_thread_error
def setData(self, index, data, role=Qt.EditRole):
'Sets the role data for the item at index to value.'
if role != Qt.EditRole:
return False
leafPref = self.index2Pref(index)
result = leafPref.qt_set_leaf_data(data)
if result is True:
self.dataChanged.emit(index, index)
return result
#report_thread_error
def headerData(self, section, orientation, role=Qt.DisplayRole):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
if section == 0:
return QVariant('Config Key')
if section == 1:
return QVariant('Config Value')
return QVariant()
I assume the problem is probably in setData or data
def data(self, index, role=Qt.DisplayRule):
'''Returns the data stored under the given role
for the item referred to by the index.'''
if not index.isValid():
return QVariant()
if role != Qt.DisplayRole and role != Qt.EditRole:
return QVariant()
nodePref = self.index2Pref(index)
data = nodePref.qt_get_data(index.column())
var = QVariant(data)
print('---')
print('data = %r' % data)
print('type(data) = %r' % type(data))
#if isinstance(data, float):
#var = var.toDouble()[0]
print('var= %r' % var)
return var
def setData(self, index, data, role=Qt.EditRole):
'Sets the role data for the item at index to value.'
if role != Qt.EditRole:
return False
leafPref = self.index2Pref(index)
result = leafPref.qt_set_leaf_data(data)
if result is True:
self.dataChanged.emit(index, index)
return result
I was messing around with data to see if turning the QVariant into a double would do something. I also tried replacing Qt.DisplayRole and Qt.EditRole with Qt.UserRole.
I'm not actually sure what is controlling the display and edit precision of the float. The type is correct, but whenever I want to enter something like .0002 Qt prevents me from typing after .00
You could use a custom item delegate to adjust the precision of floats, but it might be overkill for this particular use-case.
A simpler solution would be to just return the values as strings - then you can format the displayed values in whatever way you like.
I'm using PYQT for developing an application. my requirement is to insert a tree view with checkbox inside a combobox items. I would like to know how to achieve this?
I have the following code but this does not work.
class CheckboxInsideListbox(QWidget):
def __init__(self, parent = None):
super(CheckboxInsideListbox, self).__init__(parent)
self.setGeometry(250,250,300,300)
self.MainUI()
def MainUI(self):
#stb_label = QLabel("Select STB\'s")
stb_combobox = QComboBox()
length = 10
cb_layout = QVBoxLayout(stb_combobox)
for i in range(length):
c = QCheckBox("STB %i" % i)
cb_layout.addWidget(c)
main_layout = QVBoxLayout()
main_layout.addWidget(stb_combobox)
main_layout.addLayout(cb_layout)
self.setLayout(main_layout)
Please let me know if I'm missing anything here.
You should create a model that support Qt.CheckStateRole in data and SetData methods and the flag Qt.ItemIsUserCheckable in the flags method.
I Paste you here an example i am using in a project, this is a QSortFilterProxyModel generic implementation to use in any model but you can use the same ideas in your model implementation, obviously i am using internal structures in this subclass you have not directly in PyQt and are attached to my internal implementation (self.booleanSet and self.readOnlySet).
def flags(self, index):
if not index.isValid():
return Qt.ItemIsEnabled
if index.column() in self.booleanSet:
return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled
elif index.column() in self.readOnlySet:
return Qt.ItemIsSelectable | Qt.ItemIsEnabled
else:
return QSortFilterProxyModel.flags(self, index)
def data(self, index, role):
if not index.isValid():
return QVariant()
if index.column() in self.booleanSet and role in (Qt.CheckStateRole, Qt.DisplayRole):
if role == Qt.CheckStateRole:
value = QVariant(Qt.Checked) if index.data(Qt.EditRole).toBool() else QVariant(Qt.Unchecked)
return value
else: #if role == Qt.DisplayRole:
return QVariant()
else:
return QSortFilterProxyModel.data(self, index, role)
def setData(self, index, data, role):
if not index.isValid():
return False
if index.column() in self.booleanSet and role == Qt.CheckStateRole:
value = QVariant(True) if data.toInt()[0] == Qt.Checked else QVariant(False)
return QSortFilterProxyModel.setData(self, index, value, Qt.EditRole)
else:
return QSortFilterProxyModel.setData(self, index, data, role)
If you really mean to apply a layout to a layout, try adding your widget to your cb_layout. Otherwise get rid of your sublayout.