Solved Loop not working with model
-
@Leopold below virtual function of the model class when you are overriding provides the data for the index with different roles
QVariant MyModel::data(const QModelIndex &index, int role)const
In current implementation you are using for loop in data member function which is not correct..
(in spite of for loop which is having return statement )
-
Thank you both,
i will have a look to it this evening. -
@Leopold said in Loop not working with model:
for (row=0; row<6 ; row++)
What is this for?
tm_contact_name.length()
That doesn't even make any sense. What is
.length()
on aQList<>
? -
@kshegunov
the amount of entries in the list, in my case its 54.
I want 9 columns with 6 rows like the example.
When I uncommand the for loops and if loops I get the column with all the entries from QlistQString.
When I set columnCount to 9 I get 9 columns all with the same entries.![alt text]( image url)```
case Qt::DisplayRole:
{ if (!index.isValid() || role != Qt::DisplayRole) {
return QVariant();
}//for (row=0; row<6 ; row++)
// if (col=1 && col<9)
// {
// if (row=0 && row<6 )
// for (col=1;col<9; col++)// { return tm_contact_name[row];//[index.row()]; //row++: // } // col++;
//}
}
break; -
@Leopold said in Loop not working with model:
When I uncommand the for loops and if loops I get the column with all the entries from QlistQString.
When I set columnCount to 9 I get 9 columns all with the same entries.Of course you do, because you don't handle both the row and the column that was passed to you. You take the row'th element from the list (why the row I don't even know). How are you supposed to have 9 columns from a list to begin with?
-
Excuse me, I do not understand what you want to say.
If I have a construct like this for loops I can usually fill a table with. Item 1 to 6 in column 1, if item> 6 take column 2, now Item 7to 12, now increase columns and so on. -
@Leopold
From your commented lines you say you uncomment:// if (col=1 && col<9)
The above will always be true, and assigns
col
to1
.// if (row=0 && row<6 )
The above will always be true, and assigns
row
to0
.If you actually want help with this: remove all your commented-out lines, they are just confusing. Show the exact lines which you are actually using, and what incorrect behaviour is observed.
-
@JonB
this is my actual code:// mymodel.cpp #include <QMainwindow> #include "mymodel.h" #include <QAbstractTableModel> #include <QStringList> #include <QString> #include <QList> MyModel::MyModel(QObject *parent) : QAbstractTableModel (parent) { } // Create a method to populate the model with data: void MyModel::populateData(const QList<QString> &contactNames) { tm_contact_name.clear(); tm_contact_name = contactNames; return; } int MyModel::rowCount(const QModelIndex &parent)const { Q_UNUSED(parent); return tm_contact_name.length(); //return 6; } int MyModel::columnCount(const QModelIndex &parent)const { Q_UNUSED(parent); return 1; //return 3; } QVariant MyModel::data(const QModelIndex &index, int role)const { int row = index.row(); int col = index.column(); switch(role) { case Qt::DisplayRole: { if (!index.isValid() || role != Qt::DisplayRole) { return QVariant(); } for (int row =0; row <6; row++) { for (int col= 1; col < 9; col++) return tm_contact_name[index.row()]; } } break; { case Qt::CheckStateRole: if (row == 0 && col == 0) //add a checkbox to cell(0,0) { return Qt::Checked; } else { return Qt::Unchecked; } } return QVariant(); } }
This is what I get:![alt text]( image url)
-
Sorry, I couldn`t continue writing. You see I have drawn the slider downwards.
The complete QListQString is in one column, I want it divided into 9 columns and 6 rows like the example in my first thread. -
@Leopold
I think you are saying:- You have a
QStringList
- You want to treat it as representing rows and columns, with each row containing 9 columns
- You want to know what element is at
(index.row(), index.column())
[That's what you say. But your code also has
columnCount() return 1;
. Why do you put that in code if you say you want 9 columns?]So simple math is
int offset = index.row() * 9 + index.column(); return tm_contact_name[offset]; // or tm_contact_name.at(offset) if it is a `QStringList`
?
No loops. Just basic arithmetic. - You have a
-
Hi
Alsoint MyModel::columnCount(const QModelIndex &parent)const { Q_UNUSED(parent); return 1; //return 3; }
tells it to use one col so that should be fixed.
and this too
int MyModel::rowCount(const QModelIndex &parent)const { Q_UNUSED(parent); return tm_contact_name.length(); //return 6; }
should also return the wanted number of rows. tm_contact_name.length(); will return ALL strings you have
but that is not the number of rows.Then the view will ask you
give me data for
0,0
0,1
0,2
etc.
for all cols, for all rows.
So data function must return one "data" for each combination of row and col. ( as nagesh explained)
So we cant use a for loop as we give the data one by one so to speak.
Since you have all the names in one big list, you must calculate an offset as JonB shows
to return the right col data since your internal list is not ordered as you want it to be displayed. -
@Leopold I was specifically confused because
for (int row =0; row <6; row++) { for (int col= 1; col < 9; col++)
has no place in
data()
. There you return one item - the one that corresponds to the row and column - you don't fill anything. And I continue not to understand what the loops have to do with it. -
JonB' s mathematics and his solution with offset was not the right way.But I guess offset is the way.
This is his solution:
I tried the two loops again and I tried a switch case but not corect too:
![alt text]( image url)
now it starts in every new column with the next row!
Here is the code:```
// mymodel.cpp
#include <QMainwindow>#include "mymodel.h"
#include <QAbstractTableModel>
#include <QStringList>
#include <QString>
#include <QList>MyModel::MyModel(QObject *parent) : QAbstractTableModel (parent)
{}
// Create a method to populate the model with data:
void MyModel::populateData(const QList<QString> &contactNames)
{
tm_contact_name.clear();
tm_contact_name = contactNames;return;
}
int MyModel::rowCount(const QModelIndex &parent)const
{Q_UNUSED(parent);
// return tm_contact_name.length();
return 6;
}int MyModel::columnCount(const QModelIndex &parent)const
{Q_UNUSED(parent); return 9; //return 9;
}
QVariant MyModel::data(const QModelIndex &index, int role)const
{
int row = index.row();
int col = index.column();
int offset;switch(role)
{
case Qt::DisplayRole:
{ if (!index.isValid() || role != Qt::DisplayRole)
{
return QVariant();
}
{ for ( col=0; col < 9; col++)
{switch(col)
case 0:
index.row()<6;
offset = index.row() + index.column();
break;
case 1:
index.row()>6<12;
offset = index.row() + index.column();
break;
case 2:
index.row()>12<19;
offset = index.row() + index.column();
break;
}}
}
return tm_contact_name.at(offset); break; { case Qt::CheckStateRole: if (row == 0 && col == 0) //add a checkbox to cell(0,0) { return Qt::Checked; } else { return Qt::Unchecked; } } return QVariant();
}
}
-
@Leopold said in Loop not working with model:
JonB' s mathematics and his solution with offset was not the right way.But I guess offset is the way.
Fascinating, because I believe it is from what you said:
I want 9 columns with 6 rows like the example.
Mind you, the example you show does not have 9 columns, nor does it have 6 rows (try counting them), and you have given different values for the number of columns in your various posts & code.
This is his solution:
My solution does not produce one column. It has nothing to do with how many columns your table has, that is down to you.
At no point do I see where you have actually stated how your items in one
QStringList
are supposed to map to your desired rows/columns. Maybe if you did that people would understand.I try again on your:
Item 1 to 6 in column 1, if item> 6 take column 2, now Item 7to 12, now increase columns and so on.
So if that means the items are laid out in the list such that you get all rows in column #0 first, then all rows in column #1, you obviously need to use your own math knowledge to alter my suggested
int offset = index.row() * 9 + index.column();
to
int offset = index.column() * 6 + index.row();
Did you try that, instead of saying mine "was not the right way"?
I tried the two loops again
As people have said many times, there is no loop to be put inside your
data()
method. You ignore these comments and persist with loops, without understanding.Your new
switch
statement is pointless, as in everycase
you setoffset = index.row() + index.column();
.index.row()<6; index.row()>6<12; index.row()>12<19;
All these statements do nothing. If there is meant to be an
if
there, it is not present. Your apparent conception of how to check for a range does not correspond to how C++ works. The result your code comes up with for settingoffset
is quite wrong in all cases.Sorry, but to get anywhere with Qt or any other toolkit you need to concentrate on the basics of programming in general and C++ in particular.
-
@JonB
Sorry if I offended you .@JonB said in Loop not working with model:
Sorry, but to get anywhere with Qt or any other toolkit you need to concentrate on the basics of programming in general and C++ in particular.
I am working on that. :-)
But good news : this was the solution!
Thank you. -
@Leopold
If I understand your string list layout correctly, try theint offset = index.column() * 6 + index.row();
Even better, to allow for a different number of rows from the
6
yourrowCount()
currently returns you could write:int offset = index.column() * rowCount() + index.row();
You showed my solution as only "producing" one column in the table, but the number of rows/columns in the table is determined by the return values of
rowCount()
/columnCount()
, not whatever you do in thedata()
method.It is still the case that
data(const QModelIndex &index, int role=Qt::DisplayRole)
's job is to return the item in the list corresponding to(index.row(), index.column())
. This should involve some multiplication and addition of the row/column to reach the right offset element in the list. There should be no loop involved. -
@JonB
like I said already, it is working perfect now.
Now I have to fight how I can attach a hint to the different items. The callenge is that I will have different QListStrings with different hints.
If other people have the same problem with model, here is the code:```
// mymodel.cpp
#include <QMainwindow>#include "mymodel.h"
#include <QAbstractTableModel>
#include <QStringList>
#include <QString>
#include <QList>MyModel::MyModel(QObject *parent) : QAbstractTableModel (parent)
{}
// Create a method to populate the model with data:
void MyModel::populateData(const QList<QString> &contactNames)
{
tm_contact_name.clear();
tm_contact_name = contactNames;return;
}
int MyModel::rowCount(const QModelIndex &parent)const
{Q_UNUSED(parent);
// return tm_contact_name.length();
return 6;
}int MyModel::columnCount(const QModelIndex &parent)const
{Q_UNUSED(parent); return 9; //return 9;
}
QVariant MyModel::data(const QModelIndex &index, int role)const
{
int row = index.row();
int col = index.column();
int offset;switch(role)
{
case Qt::DisplayRole:
{ if (!index.isValid() || role != Qt::DisplayRole)
{
return QVariant();
}int offset = index.column() * 6 + index.row(); return tm_contact_name.at(offset); break; } case Qt::CheckStateRole: { if (row == 0 && col == 0) //add a checkbox to cell(0,0) { return Qt::Checked; } else { return Qt::Unchecked; } } return QVariant();
}
}
and here the output: ![alt text](![tableviewworking.JPG](https://ddgobkiprc33d.cloudfront.net/317aa69f-226f-4271-badc-88bc206eceee.JPG) image url)
-
@Leopold
This looks much better! No loops, just a multiply & divide :) I trust you understand how that calculation maps correctly to the desired elements in the list :)Now I have to fight how I can attach a hint to the different items. The challenge is that I will have different QListStrings with different hints.
Maybe (I'm picking simplest for you, not necessarily what I would do): for each individual
QStringList
--- which you effectively set via yourpopulateData(const QList<QString> &contactNames)
--- have anotherQStringList
of the "hints" (saytm_contact_tooltip
) with the same number of elements in the same order. Therefore for a given calculatedoffset
(from colum & row) the same offset applies to the hint list as to the data list.Then in your
data()
method you will see from https://doc.qt.io/qt-5/qabstractitemmodel.html#data and https://doc.qt.io/qt-5/qt.html#ItemDataRole-enum that if theint role
parameter todata()
equalsQt::ToolTipRole
(instead ofQt::DisplayRole
) --- which Qt will call yourdata()
method when it wants to return "The data displayed in the item's tooltip." --- you shouldreturn tm_contact_tooltip.at(offset)
.