Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Drawing on an Image!



  • Hello,

    So, I am working on a project. Basically I have data from a camera about the color that the camera is viewing. The camera gives me color values that I can express in the form of an x and a y coordinate that can be plotted on a chromaticity graph.

    I was thinking that I could display an image on the Qt interface of a chromaticity graph and then, using the x and y coordinates, draw some sort of point in the specified location of the image of the graph.

    What are the Qt classes I should be looking at in order to accomplish this goal or is there a better way? Thank you in advance.

    Juan



  • is the chromaticity graph a fixed image?



  • Yes, It would be something like this:

    0_1540305397147_c1a7e2f0-df49-4fb1-83ef-76f6da724996-image.png


  • Lifetime Qt Champion

    Hi
    If the graph is just a plain image, you can just draw on it using QPainter.
    A custom widget with its own paintEvent could easy be made or
    simply use a QLabel and its setPixmap method.
    You can connect a QPainter to an image and that way draw on the image.

    here is sample of painting on a pixmap using QLabel to display it
    https://www.dropbox.com/s/qwde8lb0fkjrrh3/mycheapgraph.zip?dl=0
    result:
    alt text



  • Thank you so much @mrjj. I want to be able to have a real-time update of a coordinate on the graph. So if the user presses a button, a coordinate point, maybe the shape of a plus or X with show up at a specific place on the graph image, but if the user presses the button again and the coordinate is different, it removes the first point and places a new coordinate symbol in the new place. Which of the two methods you presented would be more geared towards this.


  • Lifetime Qt Champion

    @jrod2much
    Hi
    The custom widget with paintEvent should be slightly faster than QLabel+pixmap.
    Also, it's easier to keep a copy of graph image around so you can erase a point by
    copying it over with a rect from the original image. ( so its partial update / erase for fast(er) performance )

    However, its not surely the QLabel way would be too slow (at all), but overall a custom widget
    is preferred as its also a more self-contained solution and could handle all
    erase/redraw of points transparent to the class using the widget.



  • @mrjj Thank you so much, worked like a charm!


  • Lifetime Qt Champion

    @jrod2much
    Super. Good work. :)



  • @mrjj Hi, I'm Brazilian and I'm answering you through google translator. I'm sorry if anything is wrong with writing.
    I would like to thank you for your code example that you provided here. I did not think in any corner how to draw on a label and you saved me. I only needed this to continue my college project.


  • Lifetime Qt Champion

    @Pedro-Jaime
    Welcome to the forums.
    Actually google translate worked good.
    Im glad it was helpful to your project.


Log in to reply