Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Integrating QML and C++ problem
Forum Update on Monday, May 27th 2025

Integrating QML and C++ problem

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
9 Posts 4 Posters 613 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexey Moiseev
    wrote on last edited by Alexey Moiseev
    #1

    I do as described in the tutorial: https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html

    I am writing a simple logger transmitting messages from within the program to the text area of the application.:
    .h

    #ifndef LOGGER_H
    #define LOGGER_H
    
    #include <QObject>
    
    class Logger : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)
    
    public:
        explicit Logger(QObject *parent = nullptr);
    
    signals:
        void messageChanged();
    
    public:
        QString message() const;
        Logger& operator << (QString value);
        void setMessage(const QString& value);
    
    private:
        QString log_message;
    };
    
    #endif // LOGGER_H
    

    .cpp

    #include "include/logger.h"
    
    Logger::Logger(QObject *parent) : QObject(parent)
    {
    }
    
    QString Logger::message() const
    {
        return log_message;
    }
    
    Logger& Logger::operator << (QString value)
    {
        setMessage(value);
    
        return *this;
    }
    
    void Logger::setMessage(const QString& value)
    {
        if (log_message != value) {
            log_message = value;
    
            emit messageChanged();
        }
    }
    
    

    main.cpp

    qmlRegisterType<Logger>("edu.logger", 0, 1, "Logger");
    

    And trying to get the value in the form.

    Logger {
         id: log;
         onMessageChanged: logWidget.append(log.message)  <------- This does not work
    }
    
    TextArea {
         id: logWidget
         placeholderText: qsTr("Clear log")
         text: log.message        <------- It works
    }             
    

    For some reason, onMessageChanged event does not occur. There are no error messages, variables are changing.

    sierdzioS 1 Reply Last reply
    0
    • A Alexey Moiseev

      I do as described in the tutorial: https://doc.qt.io/qt-5/qtqml-cppintegration-topic.html

      I am writing a simple logger transmitting messages from within the program to the text area of the application.:
      .h

      #ifndef LOGGER_H
      #define LOGGER_H
      
      #include <QObject>
      
      class Logger : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)
      
      public:
          explicit Logger(QObject *parent = nullptr);
      
      signals:
          void messageChanged();
      
      public:
          QString message() const;
          Logger& operator << (QString value);
          void setMessage(const QString& value);
      
      private:
          QString log_message;
      };
      
      #endif // LOGGER_H
      

      .cpp

      #include "include/logger.h"
      
      Logger::Logger(QObject *parent) : QObject(parent)
      {
      }
      
      QString Logger::message() const
      {
          return log_message;
      }
      
      Logger& Logger::operator << (QString value)
      {
          setMessage(value);
      
          return *this;
      }
      
      void Logger::setMessage(const QString& value)
      {
          if (log_message != value) {
              log_message = value;
      
              emit messageChanged();
          }
      }
      
      

      main.cpp

      qmlRegisterType<Logger>("edu.logger", 0, 1, "Logger");
      

      And trying to get the value in the form.

      Logger {
           id: log;
           onMessageChanged: logWidget.append(log.message)  <------- This does not work
      }
      
      TextArea {
           id: logWidget
           placeholderText: qsTr("Clear log")
           text: log.message        <------- It works
      }             
      

      For some reason, onMessageChanged event does not occur. There are no error messages, variables are changing.

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Alexey-Moiseev said in Integrating QML and C++ problem:

      text: log.getLastMessage <------- It works

      How? There is no getLastMessage property in your Logger class!

      How do you call your setMessage() method or modify message property?

      (Z(:^

      A 1 Reply Last reply
      0
      • sierdzioS sierdzio

        @Alexey-Moiseev said in Integrating QML and C++ problem:

        text: log.getLastMessage <------- It works

        How? There is no getLastMessage property in your Logger class!

        How do you call your setMessage() method or modify message property?

        A Offline
        A Offline
        Alexey Moiseev
        wrote on last edited by Alexey Moiseev
        #3

        @sierdzio Sorry, too many versions of the same I wrote too. Mixed when inserted. Fixed the code in the initial post. Existing functions are called.
        I call setMessage from an overloaded operator "<<", where setMessage changes the variable. If you display the values through qDebug, then you can see that the values come and change. But the signal does not call. As I pointed out in the code, when log.message is called separately, it works. But the onMessageChanged event does not occur.

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by
          #4

          That's weird. I can think of only 3 explanations:

          • you're trying to log the same message, so if (log_message != value) { condition is not met, and signal is not emitted (BTW. it's quite bizarre to have a logger which prevents the same message from being logged twice!)
          • TextArea.append() somehow does not work... but that's unlikely. Or maybe text is added below your viewport? append() adds a newline so you might miss it if your UI is small
          • log.message does not return correct value when invoked in a slot... but that's unlikely

          One possible solution: pass message as argument in messageChanged(const QString &msg) signal, then use it in slot onMessageChanged: logWidget.append(msg).

          (Z(:^

          A 1 Reply Last reply
          1
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #5

            Why did the question get downvoted? This isn't SO.

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            1
            • sierdzioS sierdzio

              That's weird. I can think of only 3 explanations:

              • you're trying to log the same message, so if (log_message != value) { condition is not met, and signal is not emitted (BTW. it's quite bizarre to have a logger which prevents the same message from being logged twice!)
              • TextArea.append() somehow does not work... but that's unlikely. Or maybe text is added below your viewport? append() adds a newline so you might miss it if your UI is small
              • log.message does not return correct value when invoked in a slot... but that's unlikely

              One possible solution: pass message as argument in messageChanged(const QString &msg) signal, then use it in slot onMessageChanged: logWidget.append(msg).

              A Offline
              A Offline
              Alexey Moiseev
              wrote on last edited by Alexey Moiseev
              #6

              @sierdzio

              1. That was the first thing I checked.
              2. The text is inserted where necessary, it works:
              TextArea {
                   text: log.message
              }
              
              1. log.message returns the correct value, because № 2.
              2. This solution was the second that I tried.
              J.HilkJ 1 Reply Last reply
              0
              • A Alexey Moiseev

                @sierdzio

                1. That was the first thing I checked.
                2. The text is inserted where necessary, it works:
                TextArea {
                     text: log.message
                }
                
                1. log.message returns the correct value, because № 2.
                2. This solution was the second that I tried.
                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @Alexey-Moiseev

                can you check the following? Maybe append is the issue here.

                Logger {
                id: log;
                onMessageChanged: logWidget.append(log.message) <------- This does not work
                onMessageChanged: logWidget.text = log.message <------- Does this work ?
                }


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                A 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @Alexey-Moiseev

                  can you check the following? Maybe append is the issue here.

                  Logger {
                  id: log;
                  onMessageChanged: logWidget.append(log.message) <------- This does not work
                  onMessageChanged: logWidget.text = log.message <------- Does this work ?
                  }

                  A Offline
                  A Offline
                  Alexey Moiseev
                  wrote on last edited by
                  #8

                  @J-Hilk No. Like any other action written in onMessageChanged. For some reason, onMessageChanged is not called. I also can’t check the emit function call, because it's void.

                  J.HilkJ 1 Reply Last reply
                  0
                  • A Alexey Moiseev

                    @J-Hilk No. Like any other action written in onMessageChanged. For some reason, onMessageChanged is not called. I also can’t check the emit function call, because it's void.

                    J.HilkJ Offline
                    J.HilkJ Offline
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @Alexey-Moiseev and when you simply print something to the console ?

                    onMessageChanged:console.log("onMessageChanged", log.message)
                    

                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved