How to output Debug to Status Bar?
-
I think your question was asked wrong :)
You are asking for how to display yourmouseEvent
position ( x / y ) which you currently show inqDebug()
, in yourQStatusBar
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 aQMainWindow
Code edited after @Bonnie 's hint :)
@Pl45m4 That code has just one problem,
showMessage
doesn't acceptQPoint
.
So it needs to be converted toQString
first.
If OP's question is how to get theqDebug
output string ofQPoint
, thenQString debugStr; QDebug(&debugStr) << event->pos(); statusBar()->showMessage(debugStr);
-
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.
-
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.
@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...
-
@jsulm correct the rgbValue variable of type QString is not compatible with the return type of pixel()
@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.
-
@Pl45m4 That code has just one problem,
showMessage
doesn't acceptQPoint
.
So it needs to be converted toQString
first.
If OP's question is how to get theqDebug
output string ofQPoint
, thenQString debugStr; QDebug(&debugStr) << event->pos(); statusBar()->showMessage(debugStr);
-
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); }
-
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); }
@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 useevent->pos().x()
(andy()
) to get the single mouse coordinates fromevent
as shown in my edited code.
This also doesn't involveQDebug
... if you don't want to print your mouse position anymore at some point, because... well.. you should see it in yourQStatusBar
now :) -
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); }
-
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); }
@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 ofQPixmap().toImage()
.Which is hardly surprising, since
QPixmap()
is an emptyQPixmap
! If you want to look at mouse position on some pixmap you have, then look at that pixmap! -
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); }
@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? -
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 -
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@Marxsta3
For your code to make any sense/be useful you must have someQPixmap
somewhere outside ofmouseMoveEvent
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". -
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?
@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 thisQPixmap
which you already have and are moving the mouse over? I don't know, only you do. -
@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
-
@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
-
@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
@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 onQLabel
, let's call it "ImageLabel"- re-implement
mouseMoveEvent
, like you did in yourQMainWindow
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 mostlyRGB(A)
,(A)RGB
orBGR
Check the formats here. - re-implement
-
@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 onQLabel
, let's call it "ImageLabel"- re-implement
mouseMoveEvent
, like you did in yourQMainWindow
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 mostlyRGB(A)
,(A)RGB
orBGR
Check the formats here. - re-implement