Results 1 to 2 of 2

Thread: QVariant can't save QWidget Pointer ?!!

  1. #1
    Join Date
    Jul 2010
    Posts
    41
    Thanks
    6
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QVariant can't save QWidget Pointer ?!!

    I get problem when converting a QWidget* to QVariant, please see the following code and its output:
    Qt Code:
    1. #include <QApplication>
    2. #include <QWidget>
    3. #include <QVariant>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication a(argc, argv);
    9. QWidget *widget= new QWidget();
    10. QObject *obj = widget;
    11.  
    12. //you can use all other methods like qVariantFromValue(widget)
    13. QVariant var1 = QVariant::fromValue(widget);
    14. QVariant var2 = QVariant::fromValue(obj);
    15.  
    16. qDebug() << "widget-addr=" << widget << "address saved to variant=" << var1;
    17. qDebug() << "object-addr=" << obj << "address saved to variant=" << var2;
    18. }
    To copy to clipboard, switch view to plain text mode 
    output:
    widget-addr= QWidget(0x347748) address saved to variant= QVariant(QWidget*, )
    object-addr= QWidget(0x347748) address saved to variant= QVariant(QObject*, QWidget(0x347748) )
    Why line 13 doesn't work? Why it doesn't convert QWidget* to QObject* automatically? [I'm using Qt-4.7.3 and minGW]

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

    Default Re: QVariant can't save QWidget Pointer ?!!

    Quote Originally Posted by srazi View Post
    Why it doesn't convert QWidget* to QObject* automatically?
    Your value is saved successfully, it's just the debug statement doesn't show it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    srazi (17th August 2011)

Similar Threads

  1. Passing QVariant a pointer
    By dohzer in forum Newbie
    Replies: 2
    Last Post: 9th August 2010, 11:11
  2. Save QWidget as PDF file
    By chapu in forum Newbie
    Replies: 2
    Last Post: 26th April 2010, 04:35
  3. Replies: 7
    Last Post: 5th April 2010, 21:19
  4. Convert QWidget to QVariant How to?
    By sergey_85 in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2009, 07:21
  5. [QWidget] How to save proporties?
    By Macok in forum Qt Programming
    Replies: 14
    Last Post: 29th March 2009, 14:26

Tags for this Thread

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.