How to iterate through a QStandardItemModel completely?
I have a QStandardItemModel, which I display in q QTreeView. Works fine.
To highlight relevant rows I want to highlight some of them: Therefore I have a QStringList with the names of the QStandItem* s to be highlighted.
QStringList namesToBeHighlighted = getNames();
QModelIndex in = myModel->index(0, 0);
if ( in.isValid() )
for (int curIndex = 0; curIndex < myModel->rowCount(in); ++curIndex)
QModelIndex si = myModel->index(curIndex, 0, in);
QStandardItem *curItem = myModel->itemFromIndex(si);
if (curItem)
QString curItemName = curItem->text();
if ( namesToBeHighlighted.contains(curItem->text()) )
curItem->setFont(highlightFont);
else curItem->setFont(unHighlightFont);
My Model has following structure:
Level_1
+--> Level_11
+--> Level_12
+--> Level_13
Level_2
+--> Level_21
+--> Level_22
+--> Level_23
...
Here, it iterates trough Levels 11, 12 and 13 then it stops.
c++ qt5 qstandarditemmodel qmodelindex qstandarditem
add a comment |
I have a QStandardItemModel, which I display in q QTreeView. Works fine.
To highlight relevant rows I want to highlight some of them: Therefore I have a QStringList with the names of the QStandItem* s to be highlighted.
QStringList namesToBeHighlighted = getNames();
QModelIndex in = myModel->index(0, 0);
if ( in.isValid() )
for (int curIndex = 0; curIndex < myModel->rowCount(in); ++curIndex)
QModelIndex si = myModel->index(curIndex, 0, in);
QStandardItem *curItem = myModel->itemFromIndex(si);
if (curItem)
QString curItemName = curItem->text();
if ( namesToBeHighlighted.contains(curItem->text()) )
curItem->setFont(highlightFont);
else curItem->setFont(unHighlightFont);
My Model has following structure:
Level_1
+--> Level_11
+--> Level_12
+--> Level_13
Level_2
+--> Level_21
+--> Level_22
+--> Level_23
...
Here, it iterates trough Levels 11, 12 and 13 then it stops.
c++ qt5 qstandarditemmodel qmodelindex qstandarditem
add a comment |
I have a QStandardItemModel, which I display in q QTreeView. Works fine.
To highlight relevant rows I want to highlight some of them: Therefore I have a QStringList with the names of the QStandItem* s to be highlighted.
QStringList namesToBeHighlighted = getNames();
QModelIndex in = myModel->index(0, 0);
if ( in.isValid() )
for (int curIndex = 0; curIndex < myModel->rowCount(in); ++curIndex)
QModelIndex si = myModel->index(curIndex, 0, in);
QStandardItem *curItem = myModel->itemFromIndex(si);
if (curItem)
QString curItemName = curItem->text();
if ( namesToBeHighlighted.contains(curItem->text()) )
curItem->setFont(highlightFont);
else curItem->setFont(unHighlightFont);
My Model has following structure:
Level_1
+--> Level_11
+--> Level_12
+--> Level_13
Level_2
+--> Level_21
+--> Level_22
+--> Level_23
...
Here, it iterates trough Levels 11, 12 and 13 then it stops.
c++ qt5 qstandarditemmodel qmodelindex qstandarditem
I have a QStandardItemModel, which I display in q QTreeView. Works fine.
To highlight relevant rows I want to highlight some of them: Therefore I have a QStringList with the names of the QStandItem* s to be highlighted.
QStringList namesToBeHighlighted = getNames();
QModelIndex in = myModel->index(0, 0);
if ( in.isValid() )
for (int curIndex = 0; curIndex < myModel->rowCount(in); ++curIndex)
QModelIndex si = myModel->index(curIndex, 0, in);
QStandardItem *curItem = myModel->itemFromIndex(si);
if (curItem)
QString curItemName = curItem->text();
if ( namesToBeHighlighted.contains(curItem->text()) )
curItem->setFont(highlightFont);
else curItem->setFont(unHighlightFont);
My Model has following structure:
Level_1
+--> Level_11
+--> Level_12
+--> Level_13
Level_2
+--> Level_21
+--> Level_22
+--> Level_23
...
Here, it iterates trough Levels 11, 12 and 13 then it stops.
c++ qt5 qstandarditemmodel qmodelindex qstandarditem
c++ qt5 qstandarditemmodel qmodelindex qstandarditem
edited Nov 12 '18 at 5:40
Cœur
17.5k9104145
17.5k9104145
asked Oct 14 '15 at 12:08
Ralf WickumRalf Wickum
40211547
40211547
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I hope it helps you:
void forEach(QAbstractItemModel* model, QModelIndex parent = QModelIndex())
for(int r = 0; r < model->rowCount(parent); ++r)
QModelIndex index = model->index(r, 0, parent);
QVariant name = model->data(index);
qDebug() << name;
// here is your applicable code
if( model->hasChildren(index) )
forEach(model, index);
QStandardItemModel model;
QStandardItem* parentItem = model.invisibleRootItem();
for (int i = 0; i < 4; ++i)
QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
for (int j = 0; j < 5; ++j)
item->appendRow(new QStandardItem(QString("item %0%1").arg(i).arg(j)));
parentItem->appendRow(item);
parentItem = item;
forEach(&model);
add a comment |
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%2f33124903%2fhow-to-iterate-through-a-qstandarditemmodel-completely%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I hope it helps you:
void forEach(QAbstractItemModel* model, QModelIndex parent = QModelIndex())
for(int r = 0; r < model->rowCount(parent); ++r)
QModelIndex index = model->index(r, 0, parent);
QVariant name = model->data(index);
qDebug() << name;
// here is your applicable code
if( model->hasChildren(index) )
forEach(model, index);
QStandardItemModel model;
QStandardItem* parentItem = model.invisibleRootItem();
for (int i = 0; i < 4; ++i)
QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
for (int j = 0; j < 5; ++j)
item->appendRow(new QStandardItem(QString("item %0%1").arg(i).arg(j)));
parentItem->appendRow(item);
parentItem = item;
forEach(&model);
add a comment |
I hope it helps you:
void forEach(QAbstractItemModel* model, QModelIndex parent = QModelIndex())
for(int r = 0; r < model->rowCount(parent); ++r)
QModelIndex index = model->index(r, 0, parent);
QVariant name = model->data(index);
qDebug() << name;
// here is your applicable code
if( model->hasChildren(index) )
forEach(model, index);
QStandardItemModel model;
QStandardItem* parentItem = model.invisibleRootItem();
for (int i = 0; i < 4; ++i)
QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
for (int j = 0; j < 5; ++j)
item->appendRow(new QStandardItem(QString("item %0%1").arg(i).arg(j)));
parentItem->appendRow(item);
parentItem = item;
forEach(&model);
add a comment |
I hope it helps you:
void forEach(QAbstractItemModel* model, QModelIndex parent = QModelIndex())
for(int r = 0; r < model->rowCount(parent); ++r)
QModelIndex index = model->index(r, 0, parent);
QVariant name = model->data(index);
qDebug() << name;
// here is your applicable code
if( model->hasChildren(index) )
forEach(model, index);
QStandardItemModel model;
QStandardItem* parentItem = model.invisibleRootItem();
for (int i = 0; i < 4; ++i)
QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
for (int j = 0; j < 5; ++j)
item->appendRow(new QStandardItem(QString("item %0%1").arg(i).arg(j)));
parentItem->appendRow(item);
parentItem = item;
forEach(&model);
I hope it helps you:
void forEach(QAbstractItemModel* model, QModelIndex parent = QModelIndex())
for(int r = 0; r < model->rowCount(parent); ++r)
QModelIndex index = model->index(r, 0, parent);
QVariant name = model->data(index);
qDebug() << name;
// here is your applicable code
if( model->hasChildren(index) )
forEach(model, index);
QStandardItemModel model;
QStandardItem* parentItem = model.invisibleRootItem();
for (int i = 0; i < 4; ++i)
QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
for (int j = 0; j < 5; ++j)
item->appendRow(new QStandardItem(QString("item %0%1").arg(i).arg(j)));
parentItem->appendRow(item);
parentItem = item;
forEach(&model);
answered Oct 14 '15 at 13:28
AnatolySAnatolyS
3,6081023
3,6081023
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f33124903%2fhow-to-iterate-through-a-qstandarditemmodel-completely%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