Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to output Debug to Status Bar?
Forum Updated to NodeBB v4.3 + New Features

How to output Debug to Status Bar?

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 6 Posters 3.8k Views 2 Watching
  • 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.
  • M Marxsta3

    Hi, I have this piece of code in my imageviewer.cpp file.
    It works correctly and outputs the XY to the console via debug.
    How can I output the debug to childStatusBar->showMessage();?

    void ImageViewer::mouseMoveEvent(QMouseEvent *event){
    qDebug() << event->pos();
    }

    The main cpp is:

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    ImageViewer imageViewer;
    #if defined(Q_OS_SYMBIAN)
    imageViewer.showMaximized();
    #else

    auto childStatusBar = new QStatusBar(&imageViewer);
    childStatusBar->showMessage("Hi");
    
    imageViewer.setStatusBar(childStatusBar);
    
    imageViewer.show();
    

    #endif
    return app.exec();
    }

    JonBJ Online
    JonBJ Online
    JonB
    wrote on last edited by JonB
    #2

    @Marxsta3
    So does your code show the Hi? If it does, what is the issue/question?

    You cannot easily redirect qDebug() statements to status bar --- though it can be done --- you are supposed to write code to do so rather than using qDebug() if you intend messages to be shown there to user.

    Christian EhrlicherC 1 Reply Last reply
    2
    • JonBJ JonB

      @Marxsta3
      So does your code show the Hi? If it does, what is the issue/question?

      You cannot easily redirect qDebug() statements to status bar --- though it can be done --- you are supposed to write code to do so rather than using qDebug() if you intend messages to be shown there to user.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #3

      If you want to know what was passed to QStatusBar::showMessage() you can connect to the signal QStatusBar::messageChanged() as shown in the documentation

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Marxsta3
        wrote on last edited by
        #4

        Apologies for the code formatting.

        @JonB - yes it outputs "Hi" to the status bar.

        @Christian: QStatusbar::messagechanged() - can it capture the output and log it to the status bar, so I have the X, Y coordinates displayed?

        Would anyone know the code to achieve this?

        JonBJ Pl45m4P 2 Replies Last reply
        0
        • M Marxsta3

          Apologies for the code formatting.

          @JonB - yes it outputs "Hi" to the status bar.

          @Christian: QStatusbar::messagechanged() - can it capture the output and log it to the status bar, so I have the X, Y coordinates displayed?

          Would anyone know the code to achieve this?

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #5

          @Marxsta3 said in How to output Debug to Status Bar?:

          QStatusbar::messagechanged()

          This can only tell you about what is sent to the status bar. That is not your issue. I answered above about sending output to the status bar. You have to send the messages, as your code above does for Hi.

          If you want to send qDebug() output there, and you won't change your code (which would be easier), see qInstallMessageHandler(), and example at https://stackoverflow.com/questions/4954140/how-to-redirect-qdebug-qwarning-qcritical-etc-output.

          1 Reply Last reply
          2
          • M Marxsta3

            Apologies for the code formatting.

            @JonB - yes it outputs "Hi" to the status bar.

            @Christian: QStatusbar::messagechanged() - can it capture the output and log it to the status bar, so I have the X, Y coordinates displayed?

            Would anyone know the code to achieve this?

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by Pl45m4
            #6

            @Marxsta3

            I think your question was asked wrong :)
            You are asking for how to display your mouseEvent position ( x / y ) which you currently show in qDebug(), in your QStatusBar instead, right?
            (And not for any general Debug / Message Handler System in your Status Bar)

            If so:

            void ImageViewer::mouseMoveEvent(QMouseEvent *event)
            {
               if (someConditionToDisplayText)
               {
                   QPoint p = event->pos();
                   QString msg = "( " + QString::number(p.x()) + " / " + QString::number(p.y()) + " )";
                   statusBar()->showMessage(msg);
               }
            
            }
            

            ... assuming ImageViewer is a QMainWindow

            Code edited after @Bonnie 's hint :)


            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            B 1 Reply Last reply
            3
            • Pl45m4P Pl45m4

              @Marxsta3

              I think your question was asked wrong :)
              You are asking for how to display your mouseEvent position ( x / y ) which you currently show in qDebug(), in your QStatusBar instead, right?
              (And not for any general Debug / Message Handler System in your Status Bar)

              If so:

              void ImageViewer::mouseMoveEvent(QMouseEvent *event)
              {
                 if (someConditionToDisplayText)
                 {
                     QPoint p = event->pos();
                     QString msg = "( " + QString::number(p.x()) + " / " + QString::number(p.y()) + " )";
                     statusBar()->showMessage(msg);
                 }
              
              }
              

              ... assuming ImageViewer is a QMainWindow

              Code edited after @Bonnie 's hint :)

              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #7

              @Pl45m4 That code has just one problem, showMessage doesn't accept QPoint.
              So it needs to be converted to QString first.
              If OP's question is how to get the qDebug output string of QPoint, then

              QString debugStr;
              QDebug(&debugStr) << event->pos();
              statusBar()->showMessage(debugStr);
              
              Pl45m4P 1 Reply Last reply
              3
              • M Offline
                M Offline
                Marxsta3
                wrote on last edited by
                #8

                Thanks @Pl45m4 and @Bonnie.
                This worked correctly.

                QString debugStr;
                QDebug(&debugStr) << event->pos();
                statusBar()->showMessage(debugStr);
                

                In the imageviewer.cpp file I was trying:

                void ImageViewer::mouseMoveEvent(QMouseEvent *event)
                {
                    QString rgbValue;
                    rgbValue = QPixmap().toImage().pixel(mousePosition.x(), mousePosition.y());
                    QDebug(&rgbValue) << event->pos();
                    statusBar()->showMessage(rgbValue);
                }
                

                It seems to be a way to get RGB pixel values and the mouse position in one go? It doesn't work though at the QRgb type conflicts types. Is there an elegant way to get this to work?

                I'm going one step at a time but after the RGB part is working the last part of the puzzle is to check if CMYK return CMYK values, if RGB return RGB values. XY coordinates is nice to have.

                Based on the code above does anyone have any ideas or code to try? The forum has been invaluable.

                jsulmJ 1 Reply Last reply
                0
                • M Marxsta3

                  Thanks @Pl45m4 and @Bonnie.
                  This worked correctly.

                  QString debugStr;
                  QDebug(&debugStr) << event->pos();
                  statusBar()->showMessage(debugStr);
                  

                  In the imageviewer.cpp file I was trying:

                  void ImageViewer::mouseMoveEvent(QMouseEvent *event)
                  {
                      QString rgbValue;
                      rgbValue = QPixmap().toImage().pixel(mousePosition.x(), mousePosition.y());
                      QDebug(&rgbValue) << event->pos();
                      statusBar()->showMessage(rgbValue);
                  }
                  

                  It seems to be a way to get RGB pixel values and the mouse position in one go? It doesn't work though at the QRgb type conflicts types. Is there an elegant way to get this to work?

                  I'm going one step at a time but after the RGB part is working the last part of the puzzle is to check if CMYK return CMYK values, if RGB return RGB values. XY coordinates is nice to have.

                  Based on the code above does anyone have any ideas or code to try? The forum has been invaluable.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @Marxsta3 said in How to output Debug to Status Bar?:

                  rgbValue = QPixmap().toImage().pixel(mousePosition.x(), mousePosition.y());

                  What is this line supposed to do? You're creating a default constructed QPixmap - what's the point?

                  "It doesn't work though at the QRgb type conflicts types" - please show the code causing this and the error message. But I guess you refer to your rgbValue variable of type QString which is of course not compatible with what pixel() returns...

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

                  1 Reply Last reply
                  3
                  • M Offline
                    M Offline
                    Marxsta3
                    wrote on last edited by
                    #10

                    @jsulm correct the rgbValue variable of type QString is not compatible with the return type of pixel()

                    jsulmJ 1 Reply Last reply
                    0
                    • M Marxsta3

                      @jsulm correct the rgbValue variable of type QString is not compatible with the return type of pixel()

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      @Marxsta3 said in How to output Debug to Status Bar?:

                      correct the rgbValue variable of type QString is not compatible with the return type of pixel()

                      So, construct a QString from QRgb if you need a string.

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

                      1 Reply Last reply
                      1
                      • B Bonnie

                        @Pl45m4 That code has just one problem, showMessage doesn't accept QPoint.
                        So it needs to be converted to QString first.
                        If OP's question is how to get the qDebug output string of QPoint, then

                        QString debugStr;
                        QDebug(&debugStr) << event->pos();
                        statusBar()->showMessage(debugStr);
                        
                        Pl45m4P Offline
                        Pl45m4P Offline
                        Pl45m4
                        wrote on last edited by
                        #12

                        @Bonnie said in How to output Debug to Status Bar?:

                        That code has just one problem, showMessage doesn't accept QPoint.

                        My bad :) Wasn't thinking about that and haven't tested it.


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        1 Reply Last reply
                        1
                        • M Offline
                          M Offline
                          Marxsta3
                          wrote on last edited by
                          #13

                          The following doesn't give any cast issues. Just have to define mouse position.

                          void ImageViewer::mouseMoveEvent(QMouseEvent *event)
                          {
                              QString rgbValue;
                              rgbValue = QPixmap().toImage().pixel(mousePosition.x(), mousePosition.y());
                              QDebug(&rgbValue) << event->pos();
                              statusBar()->showMessage(rgbValue);
                          }
                          
                          Pl45m4P 1 Reply Last reply
                          0
                          • M Marxsta3

                            The following doesn't give any cast issues. Just have to define mouse position.

                            void ImageViewer::mouseMoveEvent(QMouseEvent *event)
                            {
                                QString rgbValue;
                                rgbValue = QPixmap().toImage().pixel(mousePosition.x(), mousePosition.y());
                                QDebug(&rgbValue) << event->pos();
                                statusBar()->showMessage(rgbValue);
                            }
                            
                            Pl45m4P Offline
                            Pl45m4P Offline
                            Pl45m4
                            wrote on last edited by Pl45m4
                            #14

                            @Marxsta3 said in How to output Debug to Status Bar?:

                            The following doesn't give any cast issues. Just have to define mouse position.

                            Why mousePosition?
                            You can simply use event->pos().x() (and y()) to get the single mouse coordinates from event as shown in my edited code.
                            This also doesn't involve QDebug... if you don't want to print your mouse position anymore at some point, because... well.. you should see it in your QStatusBar now :)


                            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                            ~E. W. Dijkstra

                            1 Reply Last reply
                            1
                            • M Offline
                              M Offline
                              Marxsta3
                              wrote on last edited by Marxsta3
                              #15

                              Thanks @Pl45m4 - I'm now trying to do two things at once.
                              Return the RGB value of a pixel point and it's XY coordinate.
                              This compiles but receiving the message:

                              QImage::pixel: coordinate (207,201) out of range.
                              Any thoughts on the structure of the code?

                              void ImageViewer::mouseMoveEvent(QMouseEvent *event)
                              {
                                  QString rgbValue;
                                  QPointF mousePosition = event->pos();
                                  rgbValue = QPixmap().toImage().pixel(mousePosition.x(), mousePosition.y());
                                  QDebug(&rgbValue) << event->pos();
                                  statusBar()->showMessage(rgbValue);
                              }
                              
                              JonBJ Pl45m4P 2 Replies Last reply
                              0
                              • M Marxsta3

                                Thanks @Pl45m4 - I'm now trying to do two things at once.
                                Return the RGB value of a pixel point and it's XY coordinate.
                                This compiles but receiving the message:

                                QImage::pixel: coordinate (207,201) out of range.
                                Any thoughts on the structure of the code?

                                void ImageViewer::mouseMoveEvent(QMouseEvent *event)
                                {
                                    QString rgbValue;
                                    QPointF mousePosition = event->pos();
                                    rgbValue = QPixmap().toImage().pixel(mousePosition.x(), mousePosition.y());
                                    QDebug(&rgbValue) << event->pos();
                                    statusBar()->showMessage(rgbValue);
                                }
                                
                                JonBJ Online
                                JonBJ Online
                                JonB
                                wrote on last edited by
                                #16

                                @Marxsta3
                                Just read the message and look at your code (nothing wrong with its "structure"). This can presumably only mean that the position, (207,201) is out of the bounds of QPixmap().toImage().

                                Which is hardly surprising, since QPixmap() is an empty QPixmap! If you want to look at mouse position on some pixmap you have, then look at that pixmap!

                                1 Reply Last reply
                                3
                                • M Marxsta3

                                  Thanks @Pl45m4 - I'm now trying to do two things at once.
                                  Return the RGB value of a pixel point and it's XY coordinate.
                                  This compiles but receiving the message:

                                  QImage::pixel: coordinate (207,201) out of range.
                                  Any thoughts on the structure of the code?

                                  void ImageViewer::mouseMoveEvent(QMouseEvent *event)
                                  {
                                      QString rgbValue;
                                      QPointF mousePosition = event->pos();
                                      rgbValue = QPixmap().toImage().pixel(mousePosition.x(), mousePosition.y());
                                      QDebug(&rgbValue) << event->pos();
                                      statusBar()->showMessage(rgbValue);
                                  }
                                  
                                  Pl45m4P Offline
                                  Pl45m4P Offline
                                  Pl45m4
                                  wrote on last edited by Pl45m4
                                  #17

                                  @Marxsta3 said in How to output Debug to Status Bar?:

                                  QImage::pixel: coordinate (207,201) out of range.
                                  Any thoughts on the structure of the code?

                                  Fill your Pixmap with data, like

                                  void ImageViewer::mouseMoveEvent(QMouseEvent *event)
                                  {
                                      QString rgbValue;
                                      QPointF mousePosition = event->pos();
                                      QPixmap pix(200, 200); // set some size
                                      pix.fill(Qt::blue);
                                      rgbValue = pix.toImage().pixel(mousePosition.x(), mousePosition.y());
                                      QDebug(&rgbValue) << event->pos();
                                      statusBar()->showMessage(rgbValue);
                                  }
                                  

                                  Also, you need to show it somewhere in order to get the actual sync'ed pixel value. Then you don't have to deal with "out of bound" issues since you are only moving on your image
                                  Currently you are moving your mouse over an empty window while checking this specific mouse position in your image or am I missing something?


                                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                  ~E. W. Dijkstra

                                  1 Reply Last reply
                                  1
                                  • M Offline
                                    M Offline
                                    Marxsta3
                                    wrote on last edited by Marxsta3
                                    #18

                                    Tried a few changes and still get the out of range notification.
                                    Will continue testing.

                                    Reading:
                                    https://www.qtcentre.org/threads/40754-Grab-the-color-of-a-pixel-of-the-screen

                                    JonBJ 1 Reply Last reply
                                    0
                                    • M Marxsta3

                                      Tried a few changes and still get the out of range notification.
                                      Will continue testing.

                                      Reading:
                                      https://www.qtcentre.org/threads/40754-Grab-the-color-of-a-pixel-of-the-screen

                                      JonBJ Online
                                      JonBJ Online
                                      JonB
                                      wrote on last edited by JonB
                                      #19

                                      @Marxsta3
                                      For your code to make any sense/be useful you must have some QPixmap somewhere outside of mouseMoveEvent which the mouse is moving over and for which you want to look at a pixel. No point you creating a pixmap in this method presumably. Just use whichever existing pixmap the mouse is moving over here, nothing to "try" or "test".

                                      1 Reply Last reply
                                      3
                                      • M Offline
                                        M Offline
                                        Marxsta3
                                        wrote on last edited by
                                        #20

                                        Thanks @JonB takes me a while to comprehend as I'm new to it. Would you have an example code while I figure it out for myself?

                                        JonBJ 1 Reply Last reply
                                        0
                                        • M Marxsta3

                                          Thanks @JonB takes me a while to comprehend as I'm new to it. Would you have an example code while I figure it out for myself?

                                          JonBJ Online
                                          JonBJ Online
                                          JonB
                                          wrote on last edited by
                                          #21

                                          @Marxsta3
                                          But there isn't any example code because we don't know what you have got!

                                          Can you please stop writing code/testing stuff and just think/describe what it is you want? You have said that you want to be inside mouseMoveEvent() and show a message about what pixel is at a certain point on a pixelmap image. Right? So that must mean you have some pixmap already that you are moving the mouse over, right? Otherwise why would you want to do what you are doing? So where is this QPixmap which you already have and are moving the mouse over? I don't know, only you do.

                                          1 Reply Last reply
                                          2

                                          • Login

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