PDA

View Full Version : dynamic matrix of QStrings



QiT
3rd April 2007, 12:55
Hi all;

I am tring to create a dynamic matrix of QString but I have not succed.

Are there any possibility to do it?

thanks

high_flyer
3rd April 2007, 13:10
Show us what you tried.
Would that do?

QVector<QVecotr<QString> > stringMatrix;
What it is you need it for?
May be what you need is a map?

QiT
3rd April 2007, 13:22
QVector<QVecotr<QString> > stringMatrix;
:rolleyes:


QVector<QVector<QString> > stringMatrix;

high_flyer
3rd April 2007, 13:31
And.... what is the problem?

QiT
3rd April 2007, 13:43
template <class type>
class Q2DVector : public QVector< QVector<type> >
{
public:
Q2DVector() : QVector< QVector<type> >(){};
Q2DVector(int rows, int columns) : QVector< QVector<type> >(rows) {
for(int r=0; r<rows; r++) {
this[r].resize(columns);
}
};
virtual ~Q2DVector() {};
};
typedef Q2DVector<QString> Q2DQStringVector;

I am now even able to do:


Q2DQStringVector my2DArray(4,10);
my2DArray[1][5] = "thanks Robin Ericsson schrieb from qt-interest";


:rolleyes:

QiT
3rd April 2007, 13:59
And.... what is the problem?

How can I (Read/Write) (from/to) element (i,j) according to your solution?

jpn
3rd April 2007, 14:06
How can I acced to element (i,j) according to your solution?
In exactly the same way. The spoon-fed Q2DQStringVector is just a template wrapper around the same underlying data structure suggested by high_flyer with the only difference being the one additional convenience method for resizing the vectors. :p

high_flyer
3rd April 2007, 14:14
How can I (Read/Write) (from/to) element (i,j) according to your solution?
I gave no 'solution', I marely made a suggestion, in trying to understand the problem.
As Jpn hinted, what I suggested was a principal - which is why I asked what you need it for, since sometimes insted of string 'matrxes' string maps might be better, depending on needs.
If the principal of a 2D vector is good foryou , then making the wrapper class you got from qt-interest is trivial.
Oh, and reading/wiring to the bare QVector< QVector<QString> > is tright forword.
objs[i][j] = someString.
Resizing is offered by QVector. as you can see your self in the class you posted.

QiT
3rd April 2007, 14:44
exectution failed ,debugger indicte:


this[r].resize(columns);

:crying:

high_flyer
3rd April 2007, 15:01
exectution failed ,debugger indicte:
what do you mean?
Segmentation fault, or compilation problem?
show your code

QiT
3rd April 2007, 15:08
//Q2DVector.h
#include <QVector>

template <class type>
class Q2DVector : public QVector< QVector<type> >
{

public:
Q2DVector() : QVector< QVector<type> >(){};
Q2DVector(int rows, int columns) : QVector< QVector<type> >(rows) {
for(int r=0; r<rows; r++) {
this[r].resize(columns);
}
};
virtual ~Q2DVector() {};
};
typedef Q2DVector<QString> Q2DQStringVector;
typedef Q2DVector<int> Q2DQIntVector;

nothing in Q2Dvector.cpp


//main.cpp

...
Q2DQStringVector myStringMatrix(P,Q);
...
myStringMatrix[i][j]= someString;
...

:o

I have got
Unhandled exception at 0x004244e0 in APP.exe: 0x80000001: Not implemented

high_flyer
3rd April 2007, 15:26
Oh right, you can't have a templete class that subclasses QObject.

QiT
3rd April 2007, 16:38
any suggestion
http://http://www.qtcentre.org/forum/images/icons/icon5.gif

QiT
3rd April 2007, 17:00
Ok, this a "solution"



//A Matrix (P,Q) sized Of QStrings
QVector<QVector<QString> > myMatrixOfStrings;
myMatrixOfStrings.resize(P);
for(int r=0; r<P; r++)
{
myMatrixOfStrings[r].resize(Q);
}


:)

high_flyer
3rd April 2007, 17:01
any suggestion
Yes, don't make your matrix class a template class :)
Or, make your template class without inheritinig QObject (no signal slots then).

high_flyer
3rd April 2007, 17:02
Ok, this a the "solution"
Which is what I suggested from the start ;)

wysota
3rd April 2007, 20:02
I think the problem is not QObject (where is it anyway? I don't see any QObjects here), but instead this:

this[r].resize(columns);
As "this" is a pointer, you're trying to use an index operator on a pointer, which doesn't make any sense. Either use at() or dereference "this" first:

(*this)[r].resize(columns);

high_flyer
4th April 2007, 08:56
I think the problem is not QObject (where is it anyway? I don't see any QObjects here),
Oops... you are right...
Now I see that QVector is not a QObjet...
But I thought (obviously was wrong) that in Qt4 all cllases derived QObject.
Hmm, I better read the docs again...:rolleyes:
Sorry about that. (wrong info).

wysota
4th April 2007, 09:24
Oops... you are right...
Now I see that QVector is not a QObjet...
QVector is a template class itself, so it couldn't be a QObject.


But I thought (obviously was wrong) that in Qt4 all cllases derived QObject.

Naaa... QObject is quite heavy, so it's better to avoid it if you don't need it. Besides, you could do multiple inheritance then ("Remember, multiple inheritance is a beautiful thing!" :cool: )

high_flyer
4th April 2007, 09:26
QVector is a template class itself, so it couldn't be a QObject.
You are right of course, didn't really think it through... :rolleyes: