Qt: Updating only changed rows in QTableView when Inserting/Removing rows in QAbstractTableModel
I am trying to perform only a partial update on updated rows in my QTableView
when I insert and remove rows of my TableModel
(QAbstractTableModel
inheriting).
When I perform my insertions I call my overridden insertRows(row, count, QModelIndex())
which appends to my data structure within beginInsertRows(parent, row, row + count - 1)
and endInsertRows()
.
The table stays up to date during adds and removes but it looks like the signals emitted by the beginInsertRows(..)
and endInsertRows()
telling the view to update the entire table. A print statement inside my TableModel::data()
reveals that the all cells of my table are being refreshed.
bool TableModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (row >= 0 && row <= file_records.size())
{
beginInsertRows(parent, row, row + count - 1);
file_records.append(0);
endInsertRows();
return true;
}
return false;
}
// when adding record
int row = file_records.size();
if (insertRow(row))
{
file_records[row] = file_record;
}
inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent)
{
return insertRows(arow, 1, aparent);
}
Is there a way to tell the view to only update the new rows?
My update function emits dataChanged()
on the affected indexes, and it successfully only updates relevant view data. But there doesn't seem to be a way around using the begin*
and end*
functions when I make structural changes.
c++ qt
add a comment |
I am trying to perform only a partial update on updated rows in my QTableView
when I insert and remove rows of my TableModel
(QAbstractTableModel
inheriting).
When I perform my insertions I call my overridden insertRows(row, count, QModelIndex())
which appends to my data structure within beginInsertRows(parent, row, row + count - 1)
and endInsertRows()
.
The table stays up to date during adds and removes but it looks like the signals emitted by the beginInsertRows(..)
and endInsertRows()
telling the view to update the entire table. A print statement inside my TableModel::data()
reveals that the all cells of my table are being refreshed.
bool TableModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (row >= 0 && row <= file_records.size())
{
beginInsertRows(parent, row, row + count - 1);
file_records.append(0);
endInsertRows();
return true;
}
return false;
}
// when adding record
int row = file_records.size();
if (insertRow(row))
{
file_records[row] = file_record;
}
inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent)
{
return insertRows(arow, 1, aparent);
}
Is there a way to tell the view to only update the new rows?
My update function emits dataChanged()
on the affected indexes, and it successfully only updates relevant view data. But there doesn't seem to be a way around using the begin*
and end*
functions when I make structural changes.
c++ qt
You are right, you have to usebeginInsertRows()
andendInsertRows()
in your case. Have you checked the roles for whichdata()
gets called (e.g.Qt::DisplayRole
,Qt::SizeHintRole
, ...)?
– Mike
Jan 2 at 9:47
Yes, I am filtering out all but display role.
– EggDol
Jan 7 at 3:27
what do you mean by "filtering out"? I was asking about roles for which your overriddendata()
method gets called. Maybe try putting aqDebug()
statement to print out the role insidedata()
to identify them...
– Mike
Jan 7 at 16:11
Inside my data method I have a conditional that looks for a role that is Display Role. If and only if the role is Display Role then I return the appropriate cell value. If role is something else I return an empty QVariant(). Hence, I am "filtering out" all roles except the display role. The reason I know which cells are being accessed is because I used qDebug() inside the conditional to print the row and column currently being accessed, so I am certain they are DisplayRole.
– EggDol
Jan 14 at 3:39
add a comment |
I am trying to perform only a partial update on updated rows in my QTableView
when I insert and remove rows of my TableModel
(QAbstractTableModel
inheriting).
When I perform my insertions I call my overridden insertRows(row, count, QModelIndex())
which appends to my data structure within beginInsertRows(parent, row, row + count - 1)
and endInsertRows()
.
The table stays up to date during adds and removes but it looks like the signals emitted by the beginInsertRows(..)
and endInsertRows()
telling the view to update the entire table. A print statement inside my TableModel::data()
reveals that the all cells of my table are being refreshed.
bool TableModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (row >= 0 && row <= file_records.size())
{
beginInsertRows(parent, row, row + count - 1);
file_records.append(0);
endInsertRows();
return true;
}
return false;
}
// when adding record
int row = file_records.size();
if (insertRow(row))
{
file_records[row] = file_record;
}
inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent)
{
return insertRows(arow, 1, aparent);
}
Is there a way to tell the view to only update the new rows?
My update function emits dataChanged()
on the affected indexes, and it successfully only updates relevant view data. But there doesn't seem to be a way around using the begin*
and end*
functions when I make structural changes.
c++ qt
I am trying to perform only a partial update on updated rows in my QTableView
when I insert and remove rows of my TableModel
(QAbstractTableModel
inheriting).
When I perform my insertions I call my overridden insertRows(row, count, QModelIndex())
which appends to my data structure within beginInsertRows(parent, row, row + count - 1)
and endInsertRows()
.
The table stays up to date during adds and removes but it looks like the signals emitted by the beginInsertRows(..)
and endInsertRows()
telling the view to update the entire table. A print statement inside my TableModel::data()
reveals that the all cells of my table are being refreshed.
bool TableModel::insertRows(int row, int count, const QModelIndex &parent)
{
if (row >= 0 && row <= file_records.size())
{
beginInsertRows(parent, row, row + count - 1);
file_records.append(0);
endInsertRows();
return true;
}
return false;
}
// when adding record
int row = file_records.size();
if (insertRow(row))
{
file_records[row] = file_record;
}
inline bool QAbstractItemModel::insertRow(int arow, const QModelIndex &aparent)
{
return insertRows(arow, 1, aparent);
}
Is there a way to tell the view to only update the new rows?
My update function emits dataChanged()
on the affected indexes, and it successfully only updates relevant view data. But there doesn't seem to be a way around using the begin*
and end*
functions when I make structural changes.
c++ qt
c++ qt
edited Jan 1 at 23:59


William Miller
1,407317
1,407317
asked Jan 1 at 22:08
EggDolEggDol
113
113
You are right, you have to usebeginInsertRows()
andendInsertRows()
in your case. Have you checked the roles for whichdata()
gets called (e.g.Qt::DisplayRole
,Qt::SizeHintRole
, ...)?
– Mike
Jan 2 at 9:47
Yes, I am filtering out all but display role.
– EggDol
Jan 7 at 3:27
what do you mean by "filtering out"? I was asking about roles for which your overriddendata()
method gets called. Maybe try putting aqDebug()
statement to print out the role insidedata()
to identify them...
– Mike
Jan 7 at 16:11
Inside my data method I have a conditional that looks for a role that is Display Role. If and only if the role is Display Role then I return the appropriate cell value. If role is something else I return an empty QVariant(). Hence, I am "filtering out" all roles except the display role. The reason I know which cells are being accessed is because I used qDebug() inside the conditional to print the row and column currently being accessed, so I am certain they are DisplayRole.
– EggDol
Jan 14 at 3:39
add a comment |
You are right, you have to usebeginInsertRows()
andendInsertRows()
in your case. Have you checked the roles for whichdata()
gets called (e.g.Qt::DisplayRole
,Qt::SizeHintRole
, ...)?
– Mike
Jan 2 at 9:47
Yes, I am filtering out all but display role.
– EggDol
Jan 7 at 3:27
what do you mean by "filtering out"? I was asking about roles for which your overriddendata()
method gets called. Maybe try putting aqDebug()
statement to print out the role insidedata()
to identify them...
– Mike
Jan 7 at 16:11
Inside my data method I have a conditional that looks for a role that is Display Role. If and only if the role is Display Role then I return the appropriate cell value. If role is something else I return an empty QVariant(). Hence, I am "filtering out" all roles except the display role. The reason I know which cells are being accessed is because I used qDebug() inside the conditional to print the row and column currently being accessed, so I am certain they are DisplayRole.
– EggDol
Jan 14 at 3:39
You are right, you have to use
beginInsertRows()
and endInsertRows()
in your case. Have you checked the roles for which data()
gets called (e.g. Qt::DisplayRole
, Qt::SizeHintRole
, ...)?– Mike
Jan 2 at 9:47
You are right, you have to use
beginInsertRows()
and endInsertRows()
in your case. Have you checked the roles for which data()
gets called (e.g. Qt::DisplayRole
, Qt::SizeHintRole
, ...)?– Mike
Jan 2 at 9:47
Yes, I am filtering out all but display role.
– EggDol
Jan 7 at 3:27
Yes, I am filtering out all but display role.
– EggDol
Jan 7 at 3:27
what do you mean by "filtering out"? I was asking about roles for which your overridden
data()
method gets called. Maybe try putting a qDebug()
statement to print out the role inside data()
to identify them...– Mike
Jan 7 at 16:11
what do you mean by "filtering out"? I was asking about roles for which your overridden
data()
method gets called. Maybe try putting a qDebug()
statement to print out the role inside data()
to identify them...– Mike
Jan 7 at 16:11
Inside my data method I have a conditional that looks for a role that is Display Role. If and only if the role is Display Role then I return the appropriate cell value. If role is something else I return an empty QVariant(). Hence, I am "filtering out" all roles except the display role. The reason I know which cells are being accessed is because I used qDebug() inside the conditional to print the row and column currently being accessed, so I am certain they are DisplayRole.
– EggDol
Jan 14 at 3:39
Inside my data method I have a conditional that looks for a role that is Display Role. If and only if the role is Display Role then I return the appropriate cell value. If role is something else I return an empty QVariant(). Hence, I am "filtering out" all roles except the display role. The reason I know which cells are being accessed is because I used qDebug() inside the conditional to print the row and column currently being accessed, so I am certain they are DisplayRole.
– EggDol
Jan 14 at 3:39
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999326%2fqt-updating-only-changed-rows-in-qtableview-when-inserting-removing-rows-in-qab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999326%2fqt-updating-only-changed-rows-in-qtableview-when-inserting-removing-rows-in-qab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You are right, you have to use
beginInsertRows()
andendInsertRows()
in your case. Have you checked the roles for whichdata()
gets called (e.g.Qt::DisplayRole
,Qt::SizeHintRole
, ...)?– Mike
Jan 2 at 9:47
Yes, I am filtering out all but display role.
– EggDol
Jan 7 at 3:27
what do you mean by "filtering out"? I was asking about roles for which your overridden
data()
method gets called. Maybe try putting aqDebug()
statement to print out the role insidedata()
to identify them...– Mike
Jan 7 at 16:11
Inside my data method I have a conditional that looks for a role that is Display Role. If and only if the role is Display Role then I return the appropriate cell value. If role is something else I return an empty QVariant(). Hence, I am "filtering out" all roles except the display role. The reason I know which cells are being accessed is because I used qDebug() inside the conditional to print the row and column currently being accessed, so I am certain they are DisplayRole.
– EggDol
Jan 14 at 3:39