Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: QGenericMatrix with custom Template Type

  1. #1
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGenericMatrix with custom Template Type

    Hi,
    I want to know if it is possible to create a QGenericMatrix of my own Object Type.

    Like this:
    Qt Code:
    1. QGenericMatrix<5,5,MyClass> matrix;
    To copy to clipboard, switch view to plain text mode 

    thanks in advance

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGenericMatrix with custom Template Type

    isn't that the whole meaning of 'generic'?

    Isn't it faster to try than to ask here?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGenericMatrix with custom Template Type

    I tried it but the compiler doesn't like it....
    Qt Code:
    1. class MyClass
    2. {
    3. private:
    4. QString value;
    5.  
    6. public:
    7. MyClass();
    8. MyClass(QString value);
    9.  
    10. inline QString getValue() const { return value; }
    11.  
    12. bool operator== ( const MyClass & other ) const;
    13. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "myclass.h"
    2.  
    3. MyClass::MyClass()
    4. {
    5.  
    6. }
    7.  
    8. MyClass::MyClass(QString value) :
    9. value(value)
    10. {
    11. }
    12.  
    13. bool MyClass::operator ==(const MyClass & other) const
    14. {
    15. return (value == other.getValue());
    16. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QCoreApplication a(argc, argv);
    4.  
    5. QGenericMatrix<4,4,MyClass>m;
    6. return a.exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGenericMatrix with custom Template Type

    You should implement assignment operator for your class.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. The following user says thank you to spirit for this useful post:

    Qtonimo (8th August 2012)

  6. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGenericMatrix with custom Template Type

    I would imagine assignment is implicit.

    More likely there are problems with +, *, / etc.

    If op would show the compiler error then we would know for sure...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGenericMatrix with custom Template Type

    Nope, the problem is in QGenericMatrix::setToIdentity uses assignment operator which looks like ... operator = (qreal other).
    But I agree with you that other common operators have to be reloaded too.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGenericMatrix with custom Template Type

    he just needs another ctor then for that issue. The assignment will be implicit

    Qt Code:
    1. class MyClass
    2. {
    3. private:
    4. QString value;
    5.  
    6. public:
    7. MyClass();
    8. MyClass(float f); // <<<<<<<
    9. MyClass(QString value);
    10.  
    11. inline QString getValue() const { return value; }
    12.  
    13. bool operator== ( const MyClass & other ) const;
    14. };
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  9. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGenericMatrix with custom Template Type

    Well, yes it's another way to solve the issue, but in this case a new temporary object will be created. In the case of using assignment operation that will not happen.
    Implicit anything it's evil.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. #9
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGenericMatrix with custom Template Type

    I tried it with MyClass(float f) and it works...thanks

    @spirit maybe you can show me the assignment operator in this example....

    is this correct?
    Qt Code:
    1. MyClass & MyClass::operator= ( const MyClass & other )
    2. {
    3. this->value = other.getValue();
    4. return *this;
    5. }
    To copy to clipboard, switch view to plain text mode 

  11. #10
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGenericMatrix with custom Template Type

    Quote Originally Posted by Qtonimo View Post
    I tried it with MyClass(float f) and it works...thanks

    @spirit maybe you can show me the assignment operator in this example....

    is this correct?
    Qt Code:
    1. MyClass & MyClass::operator= ( const MyClass & other )
    2. {
    3. this->value = other.getValue();
    4. return *this;
    5. }
    To copy to clipboard, switch view to plain text mode 
    This code is correct but not very useful: it is probably equivalent to what the compiler generates automatically.

    What spirit and amleto pointed out is this:
    MyClass needs a way to be assigned a qreal. One way to do that is to add a constructor MyClass(qreal), which you did. However this may be suboptimal, as assigning a qreal to a MyClass first instanciates a temporary MyClass with MyClass(qreal), then assigns it with operator(const MyClass &), then destroys the temporary. You could add an assignment operator operator=(qreal) to avoid this unnecessary construction and destruction. However since MyClass is essentially a wrapper around QString which itself uses implicit sharing, I am not convinced it will make a huge difference.

    Also, it is possible that some other operators (such as binary arithmetic one) are missing from MyClass which are needed for QGenericMatrix to function properly. Errors will probably show up as soon as you try arithmetic operations on your matrices.

  12. #11
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGenericMatrix with custom Template Type

    Quote Originally Posted by spirit View Post
    Well, yes it's another way to solve the issue, but in this case a new temporary object will be created. In the case of using assignment operation that will not happen.
    Implicit anything it's evil.
    only in debug build and/or poor compiler implementations, and definitely not in c++11 with move semantics
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  13. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGenericMatrix with custom Template Type

    Here we go:
    Qt Code:
    1. ...
    2. MyClass &operator=(qreal other)
    3. {
    4. value = QString("%1").arg(other);
    5. return *this;
    6. }
    7. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  14. The following user says thank you to spirit for this useful post:

    Qtonimo (9th August 2012)

  15. #13
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGenericMatrix with custom Template Type

    Quote Originally Posted by amleto View Post
    only in debug build and/or poor compiler implementations, and definitely not in c++11 with move semantics
    Good point.

    Quote Originally Posted by spirit View Post
    Here we go:
    Qt Code:
    1. ...
    2. MyClass &operator=(qreal other)
    3. {
    4. value = QString("%1").arg(other);
    5. return *this;
    6. }
    7. ...
    To copy to clipboard, switch view to plain text mode 
    An alternative solution would be value.setNum(other).

  16. The following user says thank you to yeye_olive for this useful post:

    Qtonimo (9th August 2012)

  17. #14
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGenericMatrix with custom Template Type

    Quote Originally Posted by amleto View Post
    only in debug build and/or poor compiler implementations, and definitely not in c++11 with move semantics
    686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00) -- still shows this.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  18. #15
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGenericMatrix with custom Template Type

    hmmm, ok, I may have 'jumped the gun' with that comment, suggesting rvo when it does not apply.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  19. #16
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGenericMatrix with custom Template Type

    Thank you guys

    You brought light into the darkness.

  20. #17
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGenericMatrix with custom Template Type

    What I have to change, if I want to store pointers in my QGenericMatrix?

    Qt Code:
    1. QGenericMatrix<4,4,MyClass*> m;
    To copy to clipboard, switch view to plain text mode 

    The compiler cannot convert float to a pointer in assignment at setToIdentity()... The message is clear, but I'm not able to work arround this.
    Pursue your targets and don't give up....

  21. #18
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGenericMatrix with custom Template Type

    Well, it's gonna be challenging...
    So, I think, you have chosen wrong type of container -- What are you using it for?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  22. #19
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGenericMatrix with custom Template Type

    That wont work. You will need to create a wrapper.

    If you want QGenericMatrix<4,4,T>, then it must support

    T t;
    t = 1.0f

    since T is MyClass*, there is no opportunity to overload operators.

    Therefore you are left with using a thin wrapper or keeping with value semantics.
    Qt Code:
    1. template<class T>
    2. class Wrapper
    3. {
    4. public:
    5. Wrapper(T* ptr) : ptr_(ptr) {}
    6.  
    7. Wrapper& operator=(float f)
    8. {
    9. (*ptr) = f; // operator= (or suitable ctor) must be supported by T!
    10. }
    11.  
    12. private:
    13. T* ptr_;
    14. };
    To copy to clipboard, switch view to plain text mode 
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  23. #20
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGenericMatrix with custom Template Type

    Pointers where my intention, because I receive lots of pointers (MyClass* pointer1, MyClass* pointer2, etc.) and want to insert them in the matrix.
    If I could store pointers I would save insertion time (matrix(0,0) = pointer1...).

    So I have to create every incoming myclass object on the stack and delete the pointer object on the heap : (
    Pursue your targets and don't give up....

Similar Threads

  1. Qt Creator Custom file template for QtCreator
    By ComServant in forum Qt Tools
    Replies: 0
    Last Post: 8th March 2012, 04:45
  2. QTreeView with custom type
    By jajdoo in forum Newbie
    Replies: 1
    Last Post: 25th September 2011, 20:27
  3. Replies: 1
    Last Post: 28th August 2011, 06:36
  4. QVariant custom type.
    By hickscorp in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2010, 14:23
  5. Template classes and abstraction of type
    By Raistlin in forum General Programming
    Replies: 15
    Last Post: 1st April 2008, 10:18

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.