Any PyQt developers here?
DFS
nospam at dfs.com
Tue Oct 25 15:19:48 EDT 2022
On 10/25/2022 1:45 PM, Thomas Passin wrote:
> On 10/25/2022 1:03 PM, DFS wrote:
>> Having problems with removeRow() on a QTableView object.
>
> removeRow() isn't listed as being a method of a QTableView, not even an
> inherited method, so how are you calling removeRow() on it? (See
> https://doc.qt.io/qt-6/qtableview-members.html)
* I thought I was calling it the same way it's called with
QTableWidgets: tbl.removeRow()
But looking at my code again I was using tbl.model().removeRow()
* Plus I found several others online with similar removeRow() issues
with QTableViews.
* Plus the code didn't throw an error:
selected = tbl.selectionModel().selectedRows()
#reverse sort the selected items to delete from bottom up
selected = sorted(selected,reverse=True)
for i,val in enumerate(selected):
tbl.model().removeRow(selected[i].row())
But... as you say, when looking at the docs, removeRow() isn't even one
of the slots for QTableViews. So duh!
I see the QTableView.hideRow(row) method, which does exactly what I need.
Thanks man!
>> After calling removeRow(), the screen isn't updating. It's as if the
>> model is read-only, but it's a QSqlTableModel() model, which is not
>> read-only.
>>
>> The underlying SQL is straightforward (one table) and all columns are
>> editable.
>>
>> None of the editStrategies are working either.
>>
>> I tried everything I can think of, including changes to the
>> EditTriggers, but no luck. HELP!
>>
>> FWIW, the same removeRow() code works fine with a QTableWidget.
>>
>> -------------------------------------------------------------------
>> object creation and data loading all works fine
>> -------------------------------------------------------------------
>> #open db connection
>> qdb = QSqlDatabase.addDatabase("QSQLITE")
>> qdb.setDatabaseName(dbname)
>> qdb.open()
>>
>> #prepare query and execute to return data
>> query = QSqlQuery()
>> query.prepare(cSQL)
>> query.exec_()
>>
>> #set model type and query
>> model = QSqlTableModel()
>> model.setQuery(query)
>>
>> #assign model to QTableView object
>> view = frm.tblPostsView
>> view.setModel(model)
>>
>> #get all data
>> while(model.canFetchMore()): model.fetchMore()
>> datarows = model.rowCount()
>>
>>
>>
>> -------------------------------------------------------------------
>> iterate selected rows also works fine
>> SelectionMode is Extended.
>> identical code works for a QTableWidget
>> -------------------------------------------------------------------
>> selected = tbl.selectionModel().selectedRows()
>> #reverse sort the selected items to delete from bottom up
>> selected = sorted(selected,reverse=True)
>> for i,val in enumerate(selected):
>> tbl.model().removeRow(selected[i].row())
>>
>
More information about the Python-list
mailing list