Show low level Modbus packet in UI
-
Hi
you mean like
http://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler
so all QDebug goes to your own function ? -
wrote on 8 Nov 2018, 14:50 last edited by
It might be a solution if there's no way to retrieve the raw data (the whole packets) directly from the Modbus classes. Like the
sendData
andreveiceData
of EasyModbus.I'll try it. Thanks.
-
It might be a solution if there's no way to retrieve the raw data (the whole packets) directly from the Modbus classes. Like the
sendData
andreveiceData
of EasyModbus.I'll try it. Thanks.
-
wrote on 8 Nov 2018, 15:03 last edited by
Following this method, I can put
myMessageOutput
inside myMainWindow
class? Otherwise I don't understand how I can show the data in the UI.int main(int argc, char *argv[]) { QLoggingCategory::setFilterRules(QStringLiteral("qt.modbus* = true")); QApplication a(argc, argv); MainWindow w; qInstallMessageHandler(w.myMessageOutput); w.showMaximized(); return a.exec(); }
it says:
error: reference to non-static member function must be called
but that function is not static:
class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg); }
-
Following this method, I can put
myMessageOutput
inside myMainWindow
class? Otherwise I don't understand how I can show the data in the UI.int main(int argc, char *argv[]) { QLoggingCategory::setFilterRules(QStringLiteral("qt.modbus* = true")); QApplication a(argc, argv); MainWindow w; qInstallMessageHandler(w.myMessageOutput); w.showMaximized(); return a.exec(); }
it says:
error: reference to non-static member function must be called
but that function is not static:
class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg); }
@Mark81
Hi
It has to a be a global function. Cannot be a member. sadly.
I used a global QObject class (pointer) as as proxy so i could emit from the global function.
Not pretty but only other alternative is a file or ifstream redirection
with cout and a custom std::streambuf child. -
wrote on 8 Nov 2018, 15:50 last edited by
Tried with no luck, surely due to lack of my knowledge:
class MessageHandler: public QObject { Q_OBJECT MessageHandler() {} public: static MessageHandler *instance() { if (!_instance) _instance = new MessageHandler; return _instance; } static void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { emit messageOutput_received(type, context, msg); } signals: static void messageOutput_received(QtMsgType type, const QMessageLogContext &context, const QString &msg); protected: static MessageHandler *_instance; }; MessageHandler *MessageHandler::_instance = nullptr; int main(int argc, char *argv[]) { QLoggingCategory::setFilterRules(QStringLiteral("qt.modbus* = true")); QApplication a(argc, argv); MainWindow w; MessageHandler *mh = MessageHandler::instance(); qInstallMessageHandler(&mh->messageOutput); QObject::connect(mh, &MessageHandler::messageOutput_received, &w, &MainWindow::myMessageOutput); w.showMaximized(); return a.exec(); }
But it does't like the connect function:
error: no matching function for call to 'QObject::connect(MessageHandler*&, void ()(QtMsgType, const QMessageLogContext&, const QString&), MainWindow, void (MainWindow::*)(QtMsgType, const QMessageLogContext&, const QString&))'
QObject::connect(mh, &MessageHandler::messageOutput_received, &w, &MainWindow::myMessageOutput);
^ -
Tried with no luck, surely due to lack of my knowledge:
class MessageHandler: public QObject { Q_OBJECT MessageHandler() {} public: static MessageHandler *instance() { if (!_instance) _instance = new MessageHandler; return _instance; } static void messageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { emit messageOutput_received(type, context, msg); } signals: static void messageOutput_received(QtMsgType type, const QMessageLogContext &context, const QString &msg); protected: static MessageHandler *_instance; }; MessageHandler *MessageHandler::_instance = nullptr; int main(int argc, char *argv[]) { QLoggingCategory::setFilterRules(QStringLiteral("qt.modbus* = true")); QApplication a(argc, argv); MainWindow w; MessageHandler *mh = MessageHandler::instance(); qInstallMessageHandler(&mh->messageOutput); QObject::connect(mh, &MessageHandler::messageOutput_received, &w, &MainWindow::myMessageOutput); w.showMaximized(); return a.exec(); }
But it does't like the connect function:
error: no matching function for call to 'QObject::connect(MessageHandler*&, void ()(QtMsgType, const QMessageLogContext&, const QString&), MainWindow, void (MainWindow::*)(QtMsgType, const QMessageLogContext&, const QString&))'
QObject::connect(mh, &MessageHandler::messageOutput_received, &w, &MainWindow::myMessageOutput);
^Hi
The MessageHandler should not be as a singleton ( with static)
Just a small proxy that can be used to send in the global function.
MessageHandler *globalMSG; // and new it in main.cpp after QApplication
// the new function
void myMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) {
...
globalMSG->emit(xxxxx)
}Globals are not pretty so maybe someone has a more clean solution !
( i didnt find one so i used this ) -
@mrjj said in Show low level Modbus packet in UI:
Maybe Mr. @aha_1980 knows if possible.
I'm not aware of an official way to do this.
qInstallMessageHandler
might indeed by a solution to that.Regards
-
wrote on 8 Nov 2018, 17:22 last edited by
Got it. It works, but a native way to retrieve the raw data will be very appreciated!
-
Then please file a suggestion at bugreports.qt.io (and give a link to it here).
11/11