Results 1 to 6 of 6

Thread: QPushButton invoking setText fails

  1. #1
    Join Date
    May 2012
    Posts
    2
    Thanks
    1

    Default QPushButton invoking setText fails

    I'm trying to set text of a push button by invoking its method via QMetaObject::invokeMethod but failing to achieve any successful results. I have tried it with some other widgets and they seem to work fine. Would appropriate any suggestions, kinda stuck here and googling is not helping. Here is the code:

    Qt Code:
    1. // w is QMainWindow
    2.  
    3. QWidget* label = w.findChild<QLabel*>("label");
    4. ((QLabel*)label)->setText("text old"); // works fine
    5. bool res_label = QMetaObject::invokeMethod(label, "setText", Q_ARG(QString, "text new")); // works fine, returns true
    6.  
    7. QWidget* lineEdit = w.findChild<QLineEdit*>("lineEdit");
    8. ((QLineEdit*)lineEdit)->setText("text old"); // works fine
    9. bool res_line_edit = QMetaObject::invokeMethod(lineEdit, "setText", Q_ARG(QString, "text new")); // works fine, returns true
    10.  
    11. QWidget* textBrowser = w.findChild<QTextBrowser*>("textBrowser");
    12. ((QTextBrowser*)textBrowser)->setText("text old"); // works fine
    13. bool res_text_browser = QMetaObject::invokeMethod(textBrowser, "setText", Q_ARG(QString, "text new")); // works fine, returns true
    14.  
    15. QWidget* pushButton = w.findChild<QPushButton*>("pushButton");
    16. ((QPushButton*)pushButton)->setText("text old"); // works fine
    17. bool res_push_button = QMetaObject::invokeMethod(pushButton, "setText", Q_ARG(QString, "text new")); // NOT WORKING, RETURNS FALSE
    To copy to clipboard, switch view to plain text mode 

    I tried all connection types (Qt::DirectConnection, Qt::QueuedConnection, Qt::BlockingQueuedConnection, Qt::AutoConnection). Also setText is defined as a public slot in the first three widgets but is defined as a public function in the QPushButton, is it possible that it might be the cause of the problem?

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

    Default Re: QPushButton invoking setText fails

    That is right, QMetaObject::invokeMethod() works on signals, slots and invokable methods, not on properties (such as text for QPushButton). For the latter you can use a combination of QMetaObject::indexOfProperty(), QMetaObject::property(), and QMetaProperty::write().

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

    bokadam (25th June 2012)

  4. #3
    Join Date
    May 2012
    Posts
    2
    Thanks
    1

    Default Re: QPushButton invoking setText fails

    Thanks yeye, it worked like a charm. Here is the working code:

    Qt Code:
    1. QWidget* pushButton = w.findChild<QPushButton*>("pushButton");
    2. int propertyIndex = pushButton->metaObject()->indexOfProperty("text");
    3. QMetaProperty property = pushButton->metaObject()->property(propertyIndex);
    4. bool res = property.write(pushButton, "text new 2"); // works fine, returns true
    To copy to clipboard, switch view to plain text mode 

  5. #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: QPushButton invoking setText fails

    Quote Originally Posted by yeye_olive View Post
    That is right, QMetaObject::invokeMethod() works on signals, slots and invokable methods, not on properties (such as text for QPushButton). For the latter you can use a combination of QMetaObject::indexOfProperty(), QMetaObject:roperty(), and QMetaProperty::write().
    Or just QObject::setProperty()
    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.


  6. #5

    Default Re: QPushButton invoking setText fails

    HI ,
    Facing an issue for passing a pointer as QARG in invokeMethod :

    I am calling the api void CAppManager::launchUrl(const int f_iXcoord,const int f_iYcoord,const int f_iHeight, const int f_iWidth,const QString f_cobjUrl,unsigned long *f_ulRenHandle,bool bSetupURL) as

    QMetaObject::invokeMethod(this,"launchUrl",Qt::Que uedConnection,
    Q_ARG(int,0),Q_ARG(int,0),Q_ARG(int,480),Q_ARG(int ,800),Q_ARG(QString,sSetUpURL),Q_ARG(unsigned long*,handle),Q_ARG(bool,true));

    The issue seems to be with ARG f_ulRenHandle . As when the function had this argument only as pass by value and in InvokeMethod I had passed just Q_ARG(unsigned long,handle) instead of Q_ARG(unsigned long*,handle).It worked.
    handle here has to be pointer or refrence as its getting modified in launchURL API .Here I am using pointer as we can't use Refrences with Q_ARG.Here invoke method is always returning 0.
    I am initializing/declaring handle as follows :
    unsigned long dummyHandle=0;
    unsigned long * handle = &dummyHandle;
    Please let me know what measure should i take to solve this issue ?

  7. #6
    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: QPushButton invoking setText fails

    You should not be passing pointers or references. It is better to use a signal-slot connection that will give you the result.
    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. QPushButton SetText
    By Ferric in forum Newbie
    Replies: 6
    Last Post: 30th October 2010, 00:59
  2. QPushButton setText Alignment
    By rokkamraja in forum Qt Programming
    Replies: 2
    Last Post: 19th October 2009, 10:44
  3. QPushButton SetText Color
    By Qt Coder in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2009, 17:21
  4. QPushButton setText problem
    By batileon in forum Qt Programming
    Replies: 3
    Last Post: 3rd December 2008, 03:02
  5. QPushButton - setText with "\n" not working
    By gboelter in forum Newbie
    Replies: 7
    Last Post: 10th February 2008, 03:27

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.