Results 1 to 5 of 5

Thread: QVariant with Custom Type

  1. #1

    Default Re: QVariant with Custom Type

    Hello Everyone,

    I Have a custom type, let say MyClassA

    Qt Code:
    1. class MyClassA
    2. {
    3. function a();
    4. function b();
    5. ....
    6. };
    7. Q_DECLARE_METATYPE(MyClassA);
    To copy to clipboard, switch view to plain text mode 

    In my program I use a QCombobox, so i have the need of a QVariant, so I use

    Qt Code:
    1. MyClassA a
    2. QVariant var = QVariant::fromValue(a);
    To copy to clipboard, switch view to plain text mode 

    My Question is, once I get back the QVariant, with the signal "currentindexchange" for example, how can I use the function of the class MyClassA, on the QVariant?

    ThanksYou


    Added after 23 minutes:


    Ok So I have Found a solution, and for now, before another solution I'm doing:


    Qt Code:
    1. QVariant var2 = ui->comboBox_3->itemData(index);
    2. MyClassA classA = var2.value<MyClassA>();
    To copy to clipboard, switch view to plain text mode 

    Hope this will help someone
    Last edited by kasmanit; 1st September 2012 at 22:00.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QVariant with Custom Type

    You should read up on type casting. I suggest a good C++ book.

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

    Default Re: QVariant with Custom Type

    Quote Originally Posted by tbscope View Post
    You should read up on type casting. I suggest a good C++ book.
    eh?
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QVariant with Custom Type

    Quote Originally Posted by amleto View Post
    eh?
    What are you confused about? Did I say something wrong?

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

    Default Re: QVariant with Custom Type

    I dont see how casting is relevant since you cannot use simple casts to recover 'proper' types from a QVariant.
    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.

Similar Threads

  1. Custom type registration on QVariant & toString()
    By zickedi in forum Qt Programming
    Replies: 10
    Last Post: 4th March 2012, 00:34
  2. QVariant custom type.
    By hickscorp in forum Qt Programming
    Replies: 0
    Last Post: 3rd May 2010, 14:23
  3. Using QVariant with custom data type
    By QAlex in forum Qt Programming
    Replies: 3
    Last Post: 4th December 2009, 12:04
  4. Convert between a custom data type wrapped in a QVariant
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 09:07
  5. QVariant custom/user type comparison
    By gri in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2008, 14:36

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.