Results 1 to 2 of 2

Thread: QMetaObject::invokeMethod, call from main->slot to MainWindow->slot how i can do it?

  1. #1
    Join Date
    Mar 2018
    Posts
    34
    Thanks
    18
    Qt products
    Qt5
    Platforms
    Windows

    Default QMetaObject::invokeMethod, call from main->slot to MainWindow->slot how i can do it?

    Is it possible to call slot "myMessageOutput_2" in MainWindow class from "myMessageOutput" in main.cpp.
    I have to do it in this way because i have to catch what is printed in the "qtcreator_process_stub.exe" by a static function imported from an extenal library; so i can use what is catched in msg like i want in class MainWindow

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QApplication>
    4. #include <QDebug>
    5. #include <QMetaObject>
    6. #include <QString>
    7.  
    8. void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    9. {
    10. printf("1 PRINT - %s", msg.toUtf8().data());
    11. //QUESTION - How to do:
    12. QMetaObject::invokeMethod(MainWindow, "myMessageOutput_2",
    13. , /*What connection is needed*/, Q_ARG(QtMsgType, type),
    14. QARG(const QMessageLogContext*, &context), Q_ARG(const QString*, &msg));
    15. //QUESTION
    16. }
    17.  
    18. int main(int argc, char *argv[])
    19. {
    20. QApplication a(argc, argv);
    21. qInstallMessageHandler(myMessageOutput);
    22. MainWindow w;
    23. w.show();
    24.  
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    and the classic Mainwindow files:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class MainWindow : public QMainWindow
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. MainWindow(QWidget *parent = nullptr);
    12. ~MainWindow();
    13.  
    14. Q_INVOKABLE void myMessageOutput_2(QtMsgType type, const QMessageLogContext &context, const QString &msg);
    15. };
    16. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. #include "mainwindow.h"
    2. ....
    3. void MainWindow::myMessageOutput_2(QtMsgType type, const QMessageLogContext &context, const QString &msg)
    4. {
    5. // .. Do all i want ..
    6. }
    To copy to clipboard, switch view to plain text mode 

    Please write the right call that i try do in wrong way(see //QUESTION row on main.cpp)?
    Last edited by andreaQt; 16th November 2020 at 11:32.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    503
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QMetaObject::invokeMethod, call from main->slot to MainWindow->slot how i can do

    Hi, first of all your MainWindow is a local variable in main(), so accessing it from another function is not possible. To fix that you could change it into a global pointer.
    Second, assuming that the MainWindow variable is accessible: what about a simple function call, e.g. w->myMessageOutput_2() instead of the invokeMethod() call?

    Ginsengelf

Similar Threads

  1. Replies: 2
    Last Post: 9th March 2018, 22:56
  2. Replies: 2
    Last Post: 8th November 2016, 05:44
  3. Replies: 3
    Last Post: 9th May 2013, 16:37
  4. Replies: 3
    Last Post: 20th January 2012, 16:23
  5. Replies: 0
    Last Post: 19th January 2012, 16:52

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.