Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Errors in EventLogger. This eventlogger is designed on Qt5 but having these errors in QT 6.2

    General and Desktop
    6
    13
    159
    Loading More Posts
    • 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.
    • Aviral 0
      Aviral 0 last edited by

      Hi everyone! Hope you're well.
      I am trying to implement eventLogger in Qt 6.2 | The event logger code I have was made on QT 5 and have the errors. Please help me resolve it.

      #include "eventlogger.h"
      
      QEventLogger::QEventLogger(const QString & logFileBaseName,
                                 QWidget * mainWidget,
                                 bool screenshotsEnabled,
                                 QObject * parent) : QObject(parent), mainWidget(mainWidget), screenshotsEnabled(screenshotsEnabled)
      {
          // Build log file name.
          QDateTime now = QDateTime::currentDateTime();
          QString fullLogFileName = logFileBaseName + " " + now.toString(Qt::ISODate).replace(":", "-") + ".csv";
      
          // Open log file.
          this->logFile = new QFile(fullLogFileName);
          if (!this->logFile->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
              exit(1);
          this->log = new QTextStream(this->logFile);
      
          // Write header to log file.
          *log << "; Date and time are: " << now.toString(Qt::ISODate) << '\n';
          *log << "; Resolution: " << mainWidget->size().width() << 'x' << mainWidget->size().height() << '\n';
          *log << "time,input type,event type,target widget class,details\n";
          log->flush();
      
          // Create the dir in which screenshots will be stored, if requested.
          if (screenshotsEnabled) {
              screenshotDirName = "./screenshots " + now.toString(Qt::ISODate).replace(":", "-");
              qDebug() << QDir().mkdir(screenshotDirName);
          }
      
          // Start timer.
          this->time = new QTime();
          this->time->start();                             **No Member named start in QTime**
      }
      
      bool QEventLogger::eventFilter(QObject * obj, QEvent * event) {
          static QMouseEvent * mouseEvent;
          static QKeyEvent * keyEvent;
          static QHoverEvent * hoverEvent;
          static QFocusEvent * focusEvent;
          static QString eventType, details;
          static int inputType, mouseButton, modifierKey, id;
          static QString inputTypeAsString, className, targetWidget;
          inputType = NONE;
      
          inputTypeAsString = "";
      
          switch (event->type()) {
          case QEvent::MouseMove:
              inputType = MOUSE;
              eventType = "MouseMove";
              break;
          case QEvent::MouseTrackingChange:
              inputType = MOUSE;
              eventType = "MouseTrackingChange";
              break;
          case QEvent::MouseButtonPress:
              inputType = MOUSE;
              eventType = "MouseButtonPress";
              break;
          case QEvent::MouseButtonRelease:
              inputType = MOUSE;
              eventType = "MouseButtonRelease";
              break;
          case QEvent::MouseButtonDblClick:
              inputType = MOUSE;
              eventType = "MouseButtonDblClick";
              break;
          case QEvent::KeyPress:
              inputType = KEYBOARD;
              eventType = "KeyPress";
          case QEvent::KeyRelease:
              inputType = KEYBOARD;
              eventType = "KeyRelease";
              break;
          case QEvent::HoverEnter:
              inputType = HOVER;
              eventType = "HoverEnter";
              break;
          case QEvent::GraphicsSceneHoverEnter:
              inputType = HOVER;
              eventType = "GraphicsSceneHoverEnter";
              break;
          case QEvent::HoverLeave:
              inputType = HOVER;
              eventType = "HoverLeave";
              break;
          case QEvent::GraphicsSceneHoverLeave:
              inputType = HOVER;
              eventType = "GraphicsSceneHoverLeave";
              break;
          case QEvent::FocusIn:
              inputType = FOCUS;
              eventType = "FocusIn";
              break;
          case QEvent::FocusOut:
              inputType = FOCUS;
              eventType = "FocusOut";
              break;
          default:
              break;
          }
      
          if (inputType == MOUSE) {
              mouseEvent = static_cast<QMouseEvent *>(event);
      
              // Collect the mouse buttons that are pressed.
              mouseButton = mouseEvent->buttons();
              QString buttonsPressed;
              if (mouseButton & Qt::LeftButton)
                  buttonsPressed += 'L';
              if (mouseButton & Qt::MiddleButton)
                  buttonsPressed += 'M';
              if (mouseButton & Qt::RightButton)
                  buttonsPressed += 'R';
      
              // Build the details string.
              details = "";
              details += QString::number(mouseEvent->x()) + ';' + QString::number(mouseEvent->y());
              details += ';' + buttonsPressed;
      
              inputTypeAsString = "Mouse";
          }
          else if (inputType == KEYBOARD) {
              keyEvent = static_cast<QKeyEvent *>(event);
      
              // Collect the modifier keys that are pressed.
              modifierKey = keyEvent->modifiers();
              QString modifierKeysPressed;
              if (modifierKey & Qt::ShiftModifier)
                  modifierKeysPressed += ":shift";
              if (modifierKey & Qt::ControlModifier)
                  modifierKeysPressed += ":ctrl";
              if (modifierKey & Qt::AltModifier)
                  modifierKeysPressed += ":alt";
              if (modifierKey & Qt::MetaModifier)
                  modifierKeysPressed += ":meta";
      
              // TODO: support special keys, such as ESC and arrow keys, when
              // keyEvent->text() == "".
      
              // Build the details string.
              details = "";
              details += QString::number(keyEvent->key());
              details += ';' + keyEvent->text();
              details += ';' + modifierKeysPressed;
      
              inputTypeAsString = "Keyboard";
          }
          else if (inputType == HOVER) {
              hoverEvent = static_cast<QHoverEvent *>(event);
      
              // qDebug() << hoverEvent << hoverEvent->pos() << obj->metaObject()->className() << obj->inherits("QWidget");
          }
          else if (inputType == FOCUS) {
              focusEvent = static_cast<QFocusEvent *>(event);
      
              // qDebug() << focusEvent << obj->metaObject()->className();
          }
      
          if (!inputTypeAsString.isEmpty()) {
              className = obj->metaObject()->className();
              if (!this->widgetPointerToID.contains(className) || !this->widgetPointerToID[className].contains(obj)) {
                  this->widgetPointerToID[className][obj] = this->widgetPointerToID[className].size();
              }
              id = this->widgetPointerToID[className][obj];
              targetWidget = className + " " + QString::number(id);
              this->appendToLog(inputTypeAsString, eventType, targetWidget, details);
          }
      
          // Always propagate the event further.
          return false;
      }
      
      void QEventLogger::appendToLog(const QString & inputType, const QString & eventType, const QString & targetWidget, const QString & details) {
          static int elapsedTime;
      
          // Store the amount of time that has elapsed, so there are no inconsistencies between further usages.
          elapsedTime = this->time->elapsed();   **No Member named elapsed in Qtime**
      
          if (this->screenshotsEnabled && eventType.compare("MouseMove") != 0)
              (QPixmap::grabWidget(mainWidget).toImage()).save(screenshotDirName + "/" + QString::number(elapsedTime) + ".png", "PNG");    **No member named grabWidget in Pixmap**
      
          *(this->log) << elapsedTime << ',' << inputType<< ',' << eventType << ',' << targetWidget << ',' << details << '\n';
          //qDebug() << elapsedTime << inputType << eventType << targetWidget << details;
          this->log->flush();
      }
      

      I have highlighted errors in Bold Fonts. Also attaching Images.
      Screenshot 2023-02-01 184222.png

      1 Reply Last reply Reply Quote 0
      • C
        ChrisW67 @Aviral 0 last edited by

        @Aviral-0 said in Errors in EventLogger. This eventlogger is designed on Qt5 but having these errors in QT 6.2:

        if (this->screenshotsEnabled && eventType.compare("MouseMove") != 0)
        {
        QPixmap *gb;
        gb mainWidget->grab().toImage().save(screenshotDirName + "/" + QString::number(elapsedTime) + ".png", "PNG");
        }

        Its showing error, Please Help!
        

        As @SGaist points out, you are missing a "=".

        You are also not initialising your pointer (a C++ cardinal sin) but ultimately that's a moot point.
        QWidget::grab() returns an actual QPixmap, not a pointer to one.
        There is also no need to convert the QPixmap to a QImage in order to save it: QPixmap::save().

         if (this->screenshotsEnabled && eventType.compare("MouseMove") != 0)
         {
            QPixmap gb = mainWidget->grab();
            gb.save(screenshotDirName + "/" + QString::number(elapsedTime) + ".png", "PNG");\
            // Consider whether you need to check that the save was successful
        }
        
        1 Reply Last reply Reply Quote 2
        • Christian Ehrlicher
          Christian Ehrlicher Lifetime Qt Champion last edited by

          See the deprecation information about those functions in Qt5, e.g. https://doc.qt.io/qt-5/qtime-obsolete.html and https://doc.qt.io/qt-5/qpixmap-obsolete.html

          To measure a timespan, use QElapsedTimer, for QPixmap see the deprecated function page.

          Qt has to stay free or it will die.

          Aviral 0 2 Replies Last reply Reply Quote 2
          • Aviral 0
            Aviral 0 @Christian Ehrlicher last edited by

            @Christian-Ehrlicher Hi Thankyou for solution. I am new to QT, I have very little idea of how to do it. I would really appreciate if you help me with that piece of code! Thankyou and congrats for life time QT champion.

            1 Reply Last reply Reply Quote 0
            • Aviral 0
              Aviral 0 @Christian Ehrlicher last edited by

              @Christian-Ehrlicher I have solved the problem of QTime with QElapsedTimer
              But I can't find replacement of QPixmap. Please if you can find share.

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @Aviral 0 last edited by

                @Aviral-0 From the link @Christian-Ehrlicher gave you:

                QPixmap QPixmap::grabWidget(QObject *widget, const QRect &rectangle)
                
                This function is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.
                
                Use QWidget::grab() instead.
                

                So, what about using grab() instead of grabWidget()?

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                Aviral 0 2 Replies Last reply Reply Quote 1
                • Aviral 0
                  Aviral 0 @jsulm last edited by

                  @jsulm Yeah I am trying it, and its throwing error saying
                  call to non static member function without an object argument
                  If you know what change its aking?

                  Christian Ehrlicher 1 Reply Last reply Reply Quote 0
                  • Christian Ehrlicher
                    Christian Ehrlicher Lifetime Qt Champion @Aviral 0 last edited by

                    @Aviral-0 said in Errors in EventLogger. This eventlogger is designed on Qt5 but having these errors in QT 6.2:

                    all to non static member function without an object argument

                    Because QWidget::grab() is not a static function. You have to call it on an object (mainWidget I would guess) - plain c++ stuff.

                    Qt has to stay free or it will die.

                    1 Reply Last reply Reply Quote 1
                    • Aviral 0
                      Aviral 0 @jsulm last edited by Aviral 0

                      @jsulm I have done this, please suggest where I am wrong:

                       if (this->screenshotsEnabled && eventType.compare("MouseMove") != 0)
                        {
                          QPixmap *gb;
                          gb mainWidget->grab().toImage().save(screenshotDirName + "/" + QString::number(elapsedTime) + ".png", "PNG");
                      }
                      

                      Its showing error, Please Help!

                      C 1 Reply Last reply Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        Hi,

                        You are missing an = sign.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        Aviral 0 1 Reply Last reply Reply Quote 0
                        • Aviral 0
                          Aviral 0 @SGaist last edited by

                          @SGaist with = also its showing error

                          SGaist 1 Reply Last reply Reply Quote 0
                          • SGaist
                            SGaist Lifetime Qt Champion @Aviral 0 last edited by

                            @Aviral-0 said in Errors in EventLogger. This eventlogger is designed on Qt5 but having these errors in QT 6.2:

                            @SGaist with = also its showing error

                            You do realise that "showing error" gives no information to help you with ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply Reply Quote 1
                            • C
                              ChrisW67 @Aviral 0 last edited by

                              @Aviral-0 said in Errors in EventLogger. This eventlogger is designed on Qt5 but having these errors in QT 6.2:

                              if (this->screenshotsEnabled && eventType.compare("MouseMove") != 0)
                              {
                              QPixmap *gb;
                              gb mainWidget->grab().toImage().save(screenshotDirName + "/" + QString::number(elapsedTime) + ".png", "PNG");
                              }

                              Its showing error, Please Help!
                              

                              As @SGaist points out, you are missing a "=".

                              You are also not initialising your pointer (a C++ cardinal sin) but ultimately that's a moot point.
                              QWidget::grab() returns an actual QPixmap, not a pointer to one.
                              There is also no need to convert the QPixmap to a QImage in order to save it: QPixmap::save().

                               if (this->screenshotsEnabled && eventType.compare("MouseMove") != 0)
                               {
                                  QPixmap gb = mainWidget->grab();
                                  gb.save(screenshotDirName + "/" + QString::number(elapsedTime) + ".png", "PNG");\
                                  // Consider whether you need to check that the save was successful
                              }
                              
                              1 Reply Last reply Reply Quote 2
                              • A
                                AlmaCarreon Banned last edited by AlmaCarreon

                                This post is deleted!
                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post