Results 1 to 5 of 5

Thread: setData()

  1. #1
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default setData()

    I have a QAction that I wish to use setData() on so I can reference a QWidget at a later date rather then using a QHash for a lookup table.
    Qt Code:
    1. QAction *act = new QAction(this);
    2.  
    3. QWidget *randomWidget = new QWidget(this);
    4.  
    5. act->setData(randomWidget);
    To copy to clipboard, switch view to plain text mode 

    Problem is when I want to get that QWidget back and use it. If I try to cast it
    Qt Code:
    1. QWidget *returnWidget = qobject_cast<QWidget *>(act->data());
    To copy to clipboard, switch view to plain text mode 
    I get "error: no matching function for call to ‘qobject_cast(QVariant)’"

    If I try to set it directly
    Qt Code:
    1. QWidget *widget = act->data();
    To copy to clipboard, switch view to plain text mode 
    I get "error: cannot convert ‘QVariant’ to ‘QWidget*’ in initialization".

    In QVariant docs I am allowed to set my own user type data; so am I just misunderstanding the use of a QVariant?

    Bob

  2. #2
    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: setData()

    Last edited by jpn; 28th February 2008 at 12:13. Reason: updated contents
    J-P Nurmi

  3. #3
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: setData()

    After my failed attempts I cannot seem to cast the QVariant back to a QWidget.

    Using
    Qt Code:
    1. qVariantValue<QWidget>(act->data());
    To copy to clipboard, switch view to plain text mode 

    Gives me multiple errors of
    1. error: ‘QWidget::QWidget(const QWidget&)’ is private
    2. error: ‘qt_metatype_id’ is not a member of ‘QMetaTypeId<QWidget>’

    If I use my own custom structure then there is no problem, and if I use QColor and the like from the document examples there is not a problem.

    I need a QWidget * back and I cannot seem to get it to cast it correctly. It must be getting really late because after going over the docs and examples I cannot for the life of me figure out how to get a QWidget * cast correctly.

    Q_DECLARE_METATYPE(QWidget); also results in an epic fail.

    Bob

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: setData()

    It's a pointer, not an object!

  5. #5
    Join Date
    Oct 2007
    Posts
    78
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: setData()

    @jpn QAction::associatedWidgets() will not work as the QWidgets are not associated in that manner with the QAction.

    @ChristianEhrlicher I tried qVariantValue<QWidget *>(act->data()); to no avail but through the magic of sleep deprivation after your post on another attempt it worked.

Similar Threads

  1. model/view call setdata() function help?
    By dolphins in forum Qt Programming
    Replies: 1
    Last Post: 19th October 2007, 01:58
  2. model/view setData help
    By nategoofs in forum Qt Programming
    Replies: 16
    Last Post: 19th August 2007, 18:00
  3. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53

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.