Results 1 to 4 of 4

Thread: Using QMetaObject::invokeMethod on a SLOT that has a QString Reference argument?

  1. #1
    Join Date
    Jan 2010
    Posts
    18
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Using QMetaObject::invokeMethod on a SLOT that has a QString Reference argument?

    I've looked all over the place and tried many different ways to call a slot with QMetaObject::invokeMethod whose only argument is a QString&. Here is my code:

    QString myString = QString(somePoundDefine);
    QMetaObject::invokeMethod(this, "mySlot", Qt::QueuedConnection, Q_ARG(QString&, myString)); //Note that there is NO space between the "QString" and the "&" in the Q_ARG call.

    When I run the above code, I get the following printout:
    QMetaMethod::invoke: Unable to handle unregistered datatype 'QString&'

    Which makes me go to:
    http://doc.qt.nokia.com/4.7-snapshot/qmetamethod.html
    to read the documentation on that error which says to use qRegisterMetaType().

    I've tried a couple different ways of trying to register a QString& data type with no luck.
    This may be a case of not seeing the forest for the trees.
    Help.

    Note that this was originally posted on the "Qt-based Software" part of the forum. Re-posted here under a slightly different title because it seemed more relevant under "Qt Programming".

  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: Using QMetaObject::invokeMethod on a SLOT that has a QString Reference argument?

    You can't queue references. The argument gets copied when queued so all references would be broken. What is it that you are trying to achieve?
    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. #3
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Using QMetaObject::invokeMethod on a SLOT that has a QString Reference argument?

    If you have a slot that accepts a reference like that:
    Qt Code:
    1. void someSlot( const QString& ref );
    To copy to clipboard, switch view to plain text mode 
    then you don't use reference when specifying type in the Q_ARG():
    Qt Code:
    1. QMetaObject::invokeMethod( this, "someSlot", Qt::QueuedConnection, Q_ARG(QString, someString ) );
    To copy to clipboard, switch view to plain text mode 
    The important bit is the const in slot definition.
    As wysota mentioned it's a copy of a string so there's no point in changing it.
    Also when you register new type you don't register "SomeType&", just "SomeType".

  4. #4
    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: Using QMetaObject::invokeMethod on a SLOT that has a QString Reference argument?

    I think the point is he has a non-const reference.
    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.


Similar Threads

  1. Problems with QMetaObject::invokeMethod
    By AlphaWolf in forum Qt Programming
    Replies: 3
    Last Post: 13th September 2012, 12:57
  2. Replies: 0
    Last Post: 19th January 2012, 16:52
  3. How to use QMetaObject::invokeMethod?
    By niko in forum Qt Programming
    Replies: 3
    Last Post: 26th January 2008, 17:02
  4. Using QMetaObject::invokeMethod() doesn't work
    By bruccutler in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2007, 23:40
  5. Replies: 17
    Last Post: 8th February 2007, 17:30

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.