I am using QStandardItemModel for my qtableview.
import ui_my_viewlogs
import os
from PyQt4 import QtCore, QtGui
class my_viewlogs(QtGui.QDialog, ui_my_viewlogs.Ui_viewlogs):
def __init__(self):
super(my_viewlogs, self).__init__()
self.setupUi(self)
self.model = QtGui.QStandardItemModel()
self.tableView.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
self.header_names = ['abc', 'def', 'ghi', 'kjl', 'mno', 'pqr']
self.model.setHorizontalHeaderLabels(self.header_names)
self.tableView.verticalHeader().setVisible(False)
self.tableView.setShowGrid(False)
self.selectionModel = self.tableView.selectionModel()
self.tableView.customContextMenuRequested.connect(self.open_menu)
self.tableView.setModel(self.model)
self.tableView.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
def open_menu(self, position):
menu = QtGui.QMenu()
remove_selected_item_icon = QtGui.QIcon()
remove_selected_item_icon.addPixmap(QtGui.QPixmap(":/images /Images/deleteSelected.png"), QtGui.QIcon.Normal, QtGui.QIcon.On)
remove_selected_item = menu.addAction(remove_selected_item_icon, "Remove selected item(s) ")
if action == remove_selected_item:
model = self.model
indices = self.tableView.selectionModel().selectedRows()
for index in sorted(indices):
model.removeRow(index.row(), QtCore.QModelIndex())
here when I am trying to delete the row selected(i.e. model.removeRow() ) I am getting a error" TypeError: argument 1 of QAbstractItemModel.removeRow() has an invalid type".
I have searched a lot for the correct way of deleting a selected row/rows in qtableview of pyqt. However, I am not able to delete the selected row/rows.
Can you please share a sample code for deleting selected row/rows in qtableview of pyqt?
the method model.removeRow(index.row()) removes the row selected.
model = self.model
indices = self.tableView.selectionModel().selectedRows()
for index in sorted(indices):
model.removeRow(index.row())
in the indices variable we get the selected row, then we delete the row.
For deleting multiple rows in our selection of tableview:
index_list = []
for model_index in self.tableView.selectionModel().selectedRows():
index = QtCore.QPersistentModelIndex(model_index)
index_list.append(index)
for index in index_list:
self.model.removeRow(index.row())
In C++:
QModelIndexList indices = myTable->selectionModel()->selectedRows();
for (int i=indices.count()-1; i>=0; --i)
{
QModelIndex index = indices.at(i);
myTable->removeRow(index.row());
}
You have to go from the bottom to the top of the list or your indexing will be screwed up.
This works just fine for me using the reversed().
indices = self.tag_table.selectionModel().selectedRows()
# Must delete in reverse order
for each_row in reversed(sorted(indices)):
self.tag_table.removeRow(each_row.row())
If someone is still looking for an answer after implementing Anuj Bhasin's answer because the above solution will not work in many cases as expected.
Reason:
When you delete row index (for example [0,1,2]), and you start to delete from 0 -> 1 -> 2, then only row 0 and row 2 will be deleted!
Deleting the First item will shift the remaining rows up, making row 1 and row 2 as row 0 and row 1 respectively, thus when you next delete row 1, row 2 (previously) will be deleted as it is now row 1.
I have a solution to this:
Pass rows you want to delete for example from 5 rows delete [0,3]
def setSel(selected: List[int], table_widget: QTableWidget):
"""
Select all rows for the given index range
"""
table_widget.setSelectionMode(QAbstractItemView.MultiSelection)
for i in selected:
table_widget.selectRow(i)
then call remove_row_all_table() with table_widget:QTableWidget as parameter
def remove_row_all_table(table_widget):
"""
Select and Delete rows from table widget
"""
table_widget: QTableWidget
selected_rows = table_widget.selectionModel().selectedRows()
if selected_rows:
row_indices = []
for row_index in selected_rows:
row_indices.append(row_index.row())
row_indices.sort(key=lambda x: -1 * x)
print(row_indices)
for row in row_indices: # sorted in descending order
print(f"row count:{table_widget.rowCount()}, deleting index:{row}")
table_widget.removeRow(row)
print()
Summary of this answer is: You have to turn on MultiSelection mode and Delete in reverse order, i.e. from higher index to lower index, so that the dependency that I mentioned at the beginning of the answer does not occur.
I know this is a bit of an older question, but I think this is more concise and does the trick.
def delete_record1(self, model, view):
"""Delete rows with currently selected cells and/or selected rows of the model"""
rows = [model_index.row() for model_index in view.selectedIndexes()]
rows.sort(reverse=True)
for i in rows:
model.removeRow(i)
model.submitAll()
model.select()
Can anybody advise what the pitfalls of this approach is?
Related
Basically I need to add a QPushButton to the end of every row of QTableView.
Here is how I insert a row:
for row in rows:
timestamp = QStandardItem("%s" % (row[6]))
tbModel.insertRow(0, (timestamp)))
I tried Things such as:
tbModel.insertRow(0, (timestamp, QStandardItemModel(QPushButton("Hi"))))
or at the end of for loop
tableWidget.setIndexWidget(tbModel.index(0, last_index), QPushButton("hi"))
But they both fail.
I Just want to add a button to the end of every row or add a button on the last column.
You have to use the setIndexWidget method passing the proper QModelIndex:
column = tableview.model().columnCount() - 1
for row in range(tableview.model().rowCount()):
index = tableview.model().index(row, column)
button = QPushButton("Hi")
tableview.setIndexWidget(index, button)
I am trying to compare two rows of data to one another which I have stored in a list.
for x in range(0, len_data_row):
if company_data[0][0][x] == company_data[1][0][x]:
print ('MATCH 1: {} - {}'.format(x, company_data[0][0][x]))
# do nothing
if company_data[0][0][x] == None and company_data[1][0][x] != None:
print ('MATCH 2: {} - {}'.format(x, company_data[1][0][x]))
# update first company_id with data from 2nd
if company_data[0][0][x] != None and company_data[1][0][x] == None:
print ('MATCH 3: {} - {}'.format(x, company_data[0][0][x]))
# update second company_id with data from 1st
Psuedocode of what I want to do:
If data at index[x] of a list is not None for row 2, but is blank for row 1, then write the value of row 2 at index[x] for row 1 data in my database.
The part I can't figure out is if in SQLAlchemy you can do specify which column is being updated by an "index" (I think in db-land index means something different than what I mean. What I mean is like a list index, e.g., list[1]). And also if you can dynamically specify which column is being updated by passing a variable to the update code? Here's what I'm looking to do (it doesn't work of course):
def some_name(column_by_index, column_value):
u = table_name.update().where(table_name.c.id==row_id).values(column_by_index=column_value)
db.execute(u)
Thank you!
I am trying to filter a QTableView based on text in QLineEdit by using a QSortFilterProxyModel. It is working, although not correctly - it is not displaying items that are a "complete match". An example will better illustrate what I mean.
Example:
Typing "22" into the QLineEdit will show rows that have "22" in the first column, but not the row that has a column that is equal to 22. "229" will be shown, "2224" will be shown, but not "22"
Below is the stripped down version of my code
self.model = QSqlTableModel()
self.model.setTable("products")
self.model.select()
self.proxy = QSortFilterProxyModel()
self.proxy.setSourceModel(self.model)
self.proxy.setFilterKeyColumn(0)
self.ui.table_products.setModel(self.proxy)
self.ui.line_input.textChanged.connect(self._filter_products)
def _filter_products(self, text):
search = QRegExp(
text,
Qt.CaseInsensitive,
QRegExp.RegExp
)
self.proxy.setFilterRegExp(search)
It looks to me like you are filtering on column 0...
self.proxy.setFilterKeyColumn(0)
...and there is only 1 row with 22 in that column.
def listedensecilensatirlar(self):
adada = self.ui.tableWidget.selectionModel().selectedRows()
print adada
I have chosen the line in each row I want to achieve but the model did not read the index. I choose what I want to get as text data contained in rows.
This is a picture of my problem: i.stack.imgur.com/APFPl.png
If you want to get the text from the items in the selected rows, you could try this:
indexes = tablewidget.selectionModel().selectedRows(column)
for index in sorted(indexes):
row = index.row()
rowtext = []
for column in range(tablewidget.columnCount()):
rowtext.append(tablewidget.item(row, column).text())
print(rowtext)
But note that selectedRows only get rows where all items are selected.
There is a good answer on the top, but try this one too.
indexRows = table.selectionModel().selectedRows()
for indexRow in sorted(indexRows):
row = indexRow.row()
rowText = table_model.item(row, column=number).text()
print(rowText)
Based on example found here but I guess I'm not understanding it. This works for single column primary keys but fails on multiple ones.
This is my code
#classmethod
def column_windows(cls, q, columns, windowsize, where = None):
"""Return a series of WHERE clauses against
a given column that break it into windows.
Result is an iterable of tuples, consisting of
((start, end), whereclause), where (start, end) are the ids.
Requires a database that supports window functions,
i.e. Postgresql, SQL Server, Oracle.
Enhance this yourself ! Add a "where" argument
so that windows of just a subset of rows can
be computed.
"""
#Here is the thing... how to compare...
def int_for_range(start_id, end_id):
if end_id:
return and_(
columns>=start_id,
columns<end_id
)
else:
return columns>=start_id
if isinstance(columns, Column):
columns_k=(columns,)
else:
columns_k=tuple(columns)
q2=None
cols=()
for c in columns:
cols = cols + (c,)
if not q2:
q2=q.session.query(c)
else:
q2=q2.add_column(c)
q2 = q2.add_column(func.row_number().over(order_by=columns_k).label('rownum'))
q2=q2.filter(q._criterion).from_self(cols)
if windowsize > 1:
q2 = q2.filter("rownum %% %d=1" % windowsize)
for res in q2:
print res
intervals = [id for id, in q2]
while intervals:
start = intervals.pop(0)
if intervals:
end = intervals[0]
else:
end = None
yield int_for_range(start, end)
#classmethod
def windowed_query(cls, q, columns, windowsize):
""""Break a Query into windows on a given column."""
for whereclause in cls.column_windows(q,columns, windowsize):
for row in q.filter(whereclause).order_by(columns):
yield row
Now I have the problem when comparing the set of columns of the primary key. Well I guess kind of recursive clause generating function should do it... Let's try it...
Well, result is not what expected but got it to work: Now it really windows any query keeping all in place, multi column unique ordering, and so on:
Here is my code, hope it may be usefull for someone else:
#classmethod
def window_query(cls, q, windowsize, windows=None):
"""
q=Query object we want to window results
windowsize=The number of elements each window has
windows=The window, or window list, numbers: 1-based to query
"""
windowselect=False
if windows:
if not isinstance(windows,list):
windows=list(windows)
windowselect=True
#Appending u_columns to ordered counting subquery will ensure unique ordering
u_columns=list([col for col in cls.getBestUniqueColumns()])
#o_columns is the list of order by columns for the query
o_columns=list([col for col in q._order_by])
#we append columns from u_columns not in o_columns to ensure unique ordering but keeping the desired one
sq_o_columns=list(o_columns)
for col in u_columns:
if not col in sq_o_columns:
sq_o_columns.append(col)
sub=None
#we select unique columns in subquery that we'll need to join in parent query
for col in u_columns:
if not sub:
sub=q.session.query(col)
else:
sub=sub.add_column(col)
#Generate a tuple from sq_o_columns list (I don't know why over() won't accept list itself TODO: more elegant
sq_o_col_tuple=()
for col in sq_o_columns:
sq_o_col_tuple=sq_o_col_tuple + (col,)
#we add row counting column, counting on generated combined ordering+unique columns tuple
sub = sub.add_column(func.row_number().over(order_by=sq_o_col_tuple).label('rownum')).filter(q._criterion)
#Prepare sub query to use as subquery (LOL)
sub=sub.subquery('lacrn')
#Prepare join ON clauses epxression comparing unique columns defined by u_columns
joinclause=expression.BooleanClauseList()
for col in u_columns:
joinclause=joinclause.__and__(col == sub.c[col.key])
#Make the joining
q=q.join(sub,joinclause
)
i=-1
while True:
#We try to query windows defined by windows list
if windowselect:
#We want selected-windows-results to returned
if windows:
i=windows.pop(0)-1
else:
break
else:
#We want all-windows-results to be returned
i=i+1
res=q.filter(and_(sub.c.rownum > (i*windowsize), sub.c.rownum <= ((i+1)*windowsize))).all()
if not (res or windowselect):
#We end an all-windows-results because of no more results, we must check if is selected-window-query
#because of selected-window-results may not exist and the are unordered
#EX: [1,2,9999999999999,3] : Assuming the third page required has no results it will return pages 1, 2, and 3
break
for row in res:
yield row