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

    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 Online
    Pl45m4P Online
    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 Offline
        JonBJ Offline
        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 Online
          Pl45m4P Online
          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 Offline
              JonBJ Offline
              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 Offline
                  JonBJ Offline
                  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
                  • M Offline
                    M Offline
                    Marxsta3
                    wrote on last edited by
                    #22

                    @JonB i've got an app that loads a bmp, jpg or tiff in a window. The pixmap must be used to load the image into the window. I can see it used several times. What I would like to do is load an image and then with mouse pressed down (or hovering) over the image it detects the RGB values for each pixel. I'm wanting to extract a few bits of info. The XY coordinate of the mouse, the rgb values of the pixel at that point, then later I want to adapt it to choose to allow either an rgb or cmyk image. The final status bar readings will be 1) XY coord 2) RGB (or CMYK) pixel value 3) The grey value at that point. I hope it's clearer now

                    JonBJ Pl45m4P 2 Replies Last reply
                    0
                    • M Marxsta3

                      @JonB i've got an app that loads a bmp, jpg or tiff in a window. The pixmap must be used to load the image into the window. I can see it used several times. What I would like to do is load an image and then with mouse pressed down (or hovering) over the image it detects the RGB values for each pixel. I'm wanting to extract a few bits of info. The XY coordinate of the mouse, the rgb values of the pixel at that point, then later I want to adapt it to choose to allow either an rgb or cmyk image. The final status bar readings will be 1) XY coord 2) RGB (or CMYK) pixel value 3) The grey value at that point. I hope it's clearer now

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #23

                      @Marxsta3
                      So whatever variable you have your pixmap already loaded into for showing, and you are moving your mouse over, is the one you want to call pix.toImage() on in mouseMoveEvent(). That's all there is to say and should make sense.

                      1 Reply Last reply
                      1
                      • M Marxsta3

                        @JonB i've got an app that loads a bmp, jpg or tiff in a window. The pixmap must be used to load the image into the window. I can see it used several times. What I would like to do is load an image and then with mouse pressed down (or hovering) over the image it detects the RGB values for each pixel. I'm wanting to extract a few bits of info. The XY coordinate of the mouse, the rgb values of the pixel at that point, then later I want to adapt it to choose to allow either an rgb or cmyk image. The final status bar readings will be 1) XY coord 2) RGB (or CMYK) pixel value 3) The grey value at that point. I hope it's clearer now

                        Pl45m4P Online
                        Pl45m4P Online
                        Pl45m4
                        wrote on last edited by Pl45m4
                        #24

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

                        The final status bar readings will be 1) XY coord 2) RGB (or CMYK) pixel value 3) The grey value at that point. I hope it's clearer now

                        So basically like OpenCV's namedWindow with an image?
                        At least the Linux version shows RGB pixel values and X / Y coordinate in some status bar.

                        You could also consider to change your design for that.
                        Make an image class based on QLabel, let's call it "ImageLabel"

                        • re-implement mouseMoveEvent, like you did in your QMainWindow class before.
                        • enable mouseTracking
                        • add a signal to show it in ImageViewer's status bar and pass everything else you need with it (position, pixel in RGB, grayscale value)

                        Btw: QImage doesn't support the CMYK color model. QImage is mostly RGB(A), (A)RGB or BGR
                        Check the formats here.


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

                        ~E. W. Dijkstra

                        Pl45m4P 1 Reply Last reply
                        1
                        • Pl45m4P Pl45m4

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

                          The final status bar readings will be 1) XY coord 2) RGB (or CMYK) pixel value 3) The grey value at that point. I hope it's clearer now

                          So basically like OpenCV's namedWindow with an image?
                          At least the Linux version shows RGB pixel values and X / Y coordinate in some status bar.

                          You could also consider to change your design for that.
                          Make an image class based on QLabel, let's call it "ImageLabel"

                          • re-implement mouseMoveEvent, like you did in your QMainWindow class before.
                          • enable mouseTracking
                          • add a signal to show it in ImageViewer's status bar and pass everything else you need with it (position, pixel in RGB, grayscale value)

                          Btw: QImage doesn't support the CMYK color model. QImage is mostly RGB(A), (A)RGB or BGR
                          Check the formats here.

                          Pl45m4P Online
                          Pl45m4P Online
                          Pl45m4
                          wrote on last edited by
                          #25

                          ImageViewer_s.gif


                          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
                            #26

                            Oh my word @Pl45m4 that is precisely it. Would you be able to share the source code? Did you have to use OpenCV?

                            Pl45m4P 1 Reply Last reply
                            0
                            • M Marxsta3

                              Oh my word @Pl45m4 that is precisely it. Would you be able to share the source code? Did you have to use OpenCV?

                              Pl45m4P Online
                              Pl45m4P Online
                              Pl45m4
                              wrote on last edited by Pl45m4
                              #27

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

                              Make an image class based on QLabel, let's call it "ImageLabel"

                              • re-implement mouseMoveEvent, like you did in your QMainWindow class before.
                              • enable mouseTracking
                              • add a signal to show it in ImageViewer's status bar and pass everything else you need with it (position, pixel in RGB, grayscale value)

                              @Marxsta3 It's "just" this :)

                              Header

                              #ifndef IMAGELABEL_H
                              #define IMAGELABEL_H
                              
                              #include <QLabel>
                              #include <QMouseEvent>
                              
                              
                              class ImageLabel : public QLabel
                              {
                                  Q_OBJECT
                              
                              public:
                                  ImageLabel(QWidget *parent=nullptr);
                              
                              protected:
                                  void mouseMoveEvent(QMouseEvent* ev) override;
                                  void leaveEvent(QEvent*ev) override;
                              
                              
                              signals:
                                  void mouseOverImage(QPoint px, int r, int g, int b, int gray);
                              
                              };
                              
                              #endif // IMAGELABEL_H
                              

                              code file

                              #include "imagelabel.h"
                              #include <QRgb>
                              #include <QImage>
                              
                              ImageLabel::ImageLabel(QWidget* parent):QLabel(parent)
                              {
                                  setMouseTracking(true);
                                  setScaledContents(true);
                              }
                              
                              void ImageLabel::mouseMoveEvent(QMouseEvent *ev)
                              {
                              
                                  if(pixmap() == nullptr)
                                  {
                                      QLabel::mouseMoveEvent(ev);
                                      return;
                                  }
                                  QPoint px = ev->pos();
                              
                                  QRgb rgb = pixmap()->scaled(this->width(), this->height()).toImage().pixel(px.x(), px.y());
                                  int grayVal = qGray(rgb);
                                  emit mouseOverImage(ev->pos(), qRed(rgb), qGreen(rgb), qBlue(rgb), grayVal);
                              
                                  QLabel::mouseMoveEvent(ev);
                              }
                              
                              void ImageLabel::leaveEvent(QEvent *ev)
                              {
                                  // mouse left ImageLabel
                                  // -> trigger reset on statusBar
                                  emit mouseOverImage(QPoint(), 0, 0, 0, 0);
                                  QLabel::leaveEvent(ev);
                              }
                              

                              And in your ImageViewer...
                              The string formatting is a mess, but you get the point ;)

                              ImageLabel * imgLbl = new ImageLabel(this);
                              
                              connect(imgLbl, &ImageLabel::mouseOverImage, [this](QPoint p, int r, int g, int b, int gray){
                              
                                      QString msg = "( " + QString::number(p.x()) + " / " + QString::number(p.y()) + " )" +
                                              "\t\t\tR: " +  QString::number(r) +
                                              "\tG: " + QString::number(g) +
                                                "\tB: " + QString::number(b) +
                                              "\t\t\tGRAY: " + QString::number(gray);
                                      // reset statusBar if mouse left the image
                                      if(p == QPoint())
                                          statusBar()->clearMessage();
                                      else
                                          statusBar()->showMessage(msg);
                              
                              
                                  });
                              
                                  QPixmap pix("Path/to/Image.jpg");
                              
                                  imgLbl->setPixmap(pix);
                              
                                  setCentralWidget(imgLbl);
                              

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

                              Did you have to use OpenCV?

                              No, I said, it's like OpenCV's window
                              Have a look here

                              • https://stackoverflow.com/questions/68405635/zooming-functionality-in-opencv-imshow-in-ubuntu

                              This is the default window to show images using OpenCV on Ubuntu. There you can see the coordinates and RGB values from where the cursor is.


                              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
                                #28

                                Great Pl45m4, it works.

                                Thank you everyone for your input.

                                1 Reply Last reply
                                0
                                • Pl45m4P Pl45m4 referenced this topic on
                                • Pl45m4P Pl45m4 referenced this topic on

                                • Login

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