Results 1 to 20 of 20

Thread: dynamic matrix of QStrings

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: dynamic matrix of QStrings

    Quote Originally Posted by QiT View Post
    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.
    J-P Nurmi

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    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.

  3. #3
    Join Date
    Jul 2006
    Posts
    36
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    exectution failed ,debugger indicte:

    Qt Code:
    1. this[r].resize(columns);
    To copy to clipboard, switch view to plain text mode 

    power of mind > mind of power

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    exectution failed ,debugger indicte:
    what do you mean?
    Segmentation fault, or compilation problem?
    show your code

  5. #5
    Join Date
    Jul 2006
    Posts
    36
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    Qt Code:
    1. //Q2DVector.h
    2. #include <QVector>
    3.  
    4. template <class type>
    5. class Q2DVector : public QVector< QVector<type> >
    6. {
    7.  
    8. public:
    9. Q2DVector() : QVector< QVector<type> >(){};
    10. Q2DVector(int rows, int columns) : QVector< QVector<type> >(rows) {
    11. for(int r=0; r<rows; r++) {
    12. this[r].resize(columns);
    13. }
    14. };
    15. virtual ~Q2DVector() {};
    16. };
    17. typedef Q2DVector<QString> Q2DQStringVector;
    18. typedef Q2DVector<int> Q2DQIntVector;
    To copy to clipboard, switch view to plain text mode 
    nothing in Q2Dvector.cpp
    Qt Code:
    1. //main.cpp
    2.  
    3. ...
    4. Q2DQStringVector myStringMatrix(P,Q);
    5. ...
    6. myStringMatrix[i][j]= someString;
    7. ...
    To copy to clipboard, switch view to plain text mode 


    I have got
    Unhandled exception at 0x004244e0 in APP.exe: 0x80000001: Not implemented
    Last edited by QiT; 3rd April 2007 at 15:12.
    power of mind > mind of power

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    Oh right, you can't have a templete class that subclasses QObject.

  7. #7
    Join Date
    Jul 2006
    Posts
    36
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    any suggestion
    power of mind > mind of power

  8. #8
    Join Date
    Jul 2006
    Posts
    36
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    Ok, this a "solution"

    Qt Code:
    1. //A Matrix (P,Q) sized Of QStrings
    2. QVector<QVector<QString> > myMatrixOfStrings;
    3. myMatrixOfStrings.resize(P);
    4. for(int r=0; r<P; r++)
    5. {
    6. myMatrixOfStrings[r].resize(Q);
    7. }
    To copy to clipboard, switch view to plain text mode 
    power of mind > mind of power

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    any suggestion
    Yes, don't make your matrix class a template class
    Or, make your template class without inheritinig QObject (no signal slots then).

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    Ok, this a the "solution"
    Which is what I suggested from the start

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: dynamic matrix of QStrings

    I think the problem is not QObject (where is it anyway? I don't see any QObjects here), but instead this:
    Qt Code:
    1. this[r].resize(columns);
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. (*this)[r].resize(columns);
    To copy to clipboard, switch view to plain text mode 

  12. The following user says thank you to wysota for this useful post:

    QiT (4th April 2007)

  13. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    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...
    Sorry about that. (wrong info).

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: dynamic matrix of QStrings

    Quote Originally Posted by high_flyer View Post
    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!" )

  15. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: dynamic matrix of QStrings

    QVector is a template class itself, so it couldn't be a QObject.
    You are right of course, didn't really think it through...

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.