Having problems with removeRow() on a QTableView object.
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())
On 25 Oct 2022, at 18:03, DFS <nospam@dfs.com> wrote:
Having problems with removeRow() on a QTableView object.
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())
--
https://mail.python.org/mailman/listinfo/python-list
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)
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())
There is an active PyQt mailing list that has lots of helpful and knowledgeable people on it.
https://www.riverbankcomputing.com/mailman/listinfo/pyqt
Barry
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)
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)
Since you helped me on the last one, maybe you could try to answer a
couple more [probably simple] roadblocks I'm hitting.
I just wanna set the font to bold/not-bold when clicking on a row in QTableView.
With a QTableWidget I do it like this:
font = QFont()
font.setBold(True) or False
QTableWidget.item(row,col).setFont(font)
But the QTableView has data/view 'models' attached to it and that syntax doesn't work:
Tried:
font = QFont()
font.setBold(True) or False
model = QTableView.model()
model.setFont(model.index(row,col), font)
Throws AttributeError: 'QSqlTableModel' object has no attribute 'setFont'
This doesn't throw an error, but doesn't show bold: model.setData(model.index(tblRow, col), font, Qt.FontRole)
Any ideas?
Thanks
On 10/27/2022 11:15 AM, DFS wrote:
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)
Since you helped me on the last one, maybe you could try to answer a
couple more [probably simple] roadblocks I'm hitting.
I just wanna set the font to bold/not-bold when clicking on a row in
QTableView.
With a QTableWidget I do it like this:
font = QFont()
font.setBold(True) or False
QTableWidget.item(row,col).setFont(font)
But the QTableView has data/view 'models' attached to it and that
syntax doesn't work:
Tried:
font = QFont()
font.setBold(True) or False
model = QTableView.model()
model.setFont(model.index(row,col), font)
Throws AttributeError: 'QSqlTableModel' object has no attribute 'setFont'
This doesn't throw an error, but doesn't show bold:
model.setData(model.index(tblRow, col), font, Qt.FontRole)
Any ideas?
You definitely need to be setting the font in an item. I'm not sure but
I think that your QFont() doesn't have any properties, so it doesn't do anything. I found this bit in a page - it's in C++ instead of Python
but that doesn't really make a difference except for the exact syntax to
use -
https://forum.qt.io/topic/70016/qlistview-item-font-stylesheet-not-working/4
QVariant v = ModelBaseClass::data(index,role);
if( condition && role == Qt::FontRole )
{
QFont font = v.value<QFont>();
font.setBold( true );
v = QVariant::fromValue<QFont>( font );
}
IOW, you have to get the font from the item, then set it to bold, which
you would do with setFont(). Then you set that new font on the item. Of course you would have to unset bold on it later. See
https://doc.qt.io/qt-6/qtablewidgetitem.html#font
Instead of "item", you might need to operate on "row". I didn't look
into that. Since a row probably doesn't have just one font (since it
can have more than one item), you'd still have to get the font from some
item in the row.
You might also be able to make the item bold using CSS, but I'm not sure.
Thanks
Internet searches are your friend for questions like this. I've never worked with a QTableView, so I had to start with some knowledge about
some other parts of QT. I found the first page searching for "qt set qtableview row font", and the second searching for "qtablewidgetitem".
On 10/27/2022 11:15 AM, DFS wrote:
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)
Since you helped me on the last one, maybe you could try to answer a
couple more [probably simple] roadblocks I'm hitting.
I just wanna set the font to bold/not-bold when clicking on a row in
QTableView.
With a QTableWidget I do it like this:
font = QFont()
font.setBold(True) or False
QTableWidget.item(row,col).setFont(font)
But the QTableView has data/view 'models' attached to it and that
syntax doesn't work:
Tried:
font = QFont()
font.setBold(True) or False
model = QTableView.model()
model.setFont(model.index(row,col), font)
Throws AttributeError: 'QSqlTableModel' object has no attribute 'setFont'
This doesn't throw an error, but doesn't show bold:
model.setData(model.index(tblRow, col), font, Qt.FontRole)
Any ideas?
You definitely need to be setting the font in an item. I'm not sure but
I think that your QFont() doesn't have any properties, so it doesn't do anything. I found this bit in a page - it's in C++ instead of Python
but that doesn't really make a difference except for the exact syntax to
use -
https://forum.qt.io/topic/70016/qlistview-item-font-stylesheet-not-working/4
QVariant v = ModelBaseClass::data(index,role);
if( condition && role == Qt::FontRole )
{
QFont font = v.value<QFont>();
font.setBold( true );
v = QVariant::fromValue<QFont>( font );
}
IOW, you have to get the font from the item, then set it to bold, which
you would do with setFont(). Then you set that new font on the item. Of course you would have to unset bold on it later. See
https://doc.qt.io/qt-6/qtablewidgetitem.html#font
Instead of "item", you might need to operate on "row". I didn't look
into that. Since a row probably doesn't have just one font (since it
can have more than one item), you'd still have to get the font from some
item in the row.
You might also be able to make the item bold using CSS, but I'm not sure.
Thanks
Internet searches are your friend for questions like this.
I've never
worked with a QTableView, so I had to start with some knowledge about
some other parts of QT. I found the first page searching for "qt set qtableview row font", and the second searching for "qtablewidgetitem".
You might also be able to make the item bold using CSS, but I'm not sure.
I found one person that said they did it but their syntax didn't work.
But it doesn't throw an error either.
model.setData(model.index(tblRow, col), font, Qt.FontRole)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 300 |
Nodes: | 16 (2 / 14) |
Uptime: | 58:20:16 |
Calls: | 6,712 |
Files: | 12,243 |
Messages: | 5,355,631 |