Qt: Updating only changed rows in QTableView when Inserting/Removing rows in QAbstractTableModel












2















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.










share|improve this question

























  • 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











  • 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
















2















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.










share|improve this question

























  • 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











  • 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














2












2








2








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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











  • 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



















  • 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











  • 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

















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












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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter