PDA

View Full Version : Errors inserting elements in a QList



sogico
15th August 2012, 19:02
Hi all,

I am trying to fill a QList with structs of 2 elements. At first, I am doing it as in the Address Book Example in Qt Examples and Demostrations, like this:


QList<QPair<int,int>> listOfPairs;
QPair<int,int> pair(1,1);
listOfPairs.insert(0,pair);
The code compiles right but when I check the list listOfPairs it has wrong values and not the two ones I have inserted. I have also tried with the method append, and I get the same result. And I've tried also changing the list of QPair's to a list of structs defined as:

typedef struct mystruct
{
int m_a;
int m_b;
mystruct(int a, int b)
{
m_a = a;
m_b = b;
}

}mystruct;

QList<mystruct> listOfMyStructs;
mystruct str(1,1);
listOfMyStructs.insert(0,str);

But it does not work again.

What am I doing wrong?

Thank you!

spirit
15th August 2012, 19:24
What's the error?

ChrisW67
15th August 2012, 23:59
First, your Qt code will not compile as posted. When the error is corrected the code compiles and runs just fine:


#include <QCoreApplication>
#include <QDebug>

int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);

// Your snippet v correction
QList<QPair<int,int> > listOfPairs;
QPair<int,int> pair(1,1);
listOfPairs.insert(0,pair);

// Put more in the list
listOfPairs.append(QPair<int, int>(2, 3));
listOfPairs.append(QPair<int, int>(4, 5));

qDebug() << listOfPairs;

return 0;
}

Output:


(QPair(1,1) , QPair(2,3) , QPair(4,5) )


So, the thing that does not work and then "does not work again" is probably the method you are using to inspect the list, but we cannot see that.

sogico
16th August 2012, 08:30
Thank you.

The code I posted is only a few lines from a more complex code, I know that those lines alone don't compile ;) And you are right and the content of the list is correct, I was checking the content of the variables with the Visual Studio tools and they show me wrong values, but when using QDebug() as you propose it works fine. Do you know why Visual Studio doesn't work for Qt variables or classes?

Thank you very much.

ChrisW67
16th August 2012, 23:22
Do you know why Visual Studio doesn't work for Qt variables or classes?
No, and I cannot make an educated guess because "doesn't work" and "wrong values" is not a good description of what you are seeing.

The first thing to check is that your project is built with debugging symbols.

Many (most) Qt classes are built with a private implementation so often the only member variable of a class is a pointer to a private class and that's all you see by default. Debuggers use helper code to present user friendly versions of these objects (like GDB in the Qt SDK does). As a non-Visual Studio user I don't know what helpers are available.