bokadam
25th June 2012, 13:25
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:
// w is QMainWindow
QWidget* label = w.findChild<QLabel*>("label");
((QLabel*)label)->setText("text old"); // works fine
bool res_label = QMetaObject::invokeMethod(label, "setText", Q_ARG(QString, "text new")); // works fine, returns true
QWidget* lineEdit = w.findChild<QLineEdit*>("lineEdit");
((QLineEdit*)lineEdit)->setText("text old"); // works fine
bool res_line_edit = QMetaObject::invokeMethod(lineEdit, "setText", Q_ARG(QString, "text new")); // works fine, returns true
QWidget* textBrowser = w.findChild<QTextBrowser*>("textBrowser");
((QTextBrowser*)textBrowser)->setText("text old"); // works fine
bool res_text_browser = QMetaObject::invokeMethod(textBrowser, "setText", Q_ARG(QString, "text new")); // works fine, returns true
QWidget* pushButton = w.findChild<QPushButton*>("pushButton");
((QPushButton*)pushButton)->setText("text old"); // works fine
bool res_push_button = QMetaObject::invokeMethod(pushButton, "setText", Q_ARG(QString, "text new")); // NOT WORKING, RETURNS FALSE
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?
// w is QMainWindow
QWidget* label = w.findChild<QLabel*>("label");
((QLabel*)label)->setText("text old"); // works fine
bool res_label = QMetaObject::invokeMethod(label, "setText", Q_ARG(QString, "text new")); // works fine, returns true
QWidget* lineEdit = w.findChild<QLineEdit*>("lineEdit");
((QLineEdit*)lineEdit)->setText("text old"); // works fine
bool res_line_edit = QMetaObject::invokeMethod(lineEdit, "setText", Q_ARG(QString, "text new")); // works fine, returns true
QWidget* textBrowser = w.findChild<QTextBrowser*>("textBrowser");
((QTextBrowser*)textBrowser)->setText("text old"); // works fine
bool res_text_browser = QMetaObject::invokeMethod(textBrowser, "setText", Q_ARG(QString, "text new")); // works fine, returns true
QWidget* pushButton = w.findChild<QPushButton*>("pushButton");
((QPushButton*)pushButton)->setText("text old"); // works fine
bool res_push_button = QMetaObject::invokeMethod(pushButton, "setText", Q_ARG(QString, "text new")); // NOT WORKING, RETURNS FALSE
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?