Saturday 15 September 2012

qt - drawing a point over an image on QLabel -


I have displayed a picture on QLabel and want to coordinate a point on the image on the mouse click event. I am able to get the coordinates, but the painter is picturing the image on the label below my image, I want it above my image.

My code is:

main.cpp

  #include "imageviewer.h" # include & lt; QApplication & gt; Int main (int argc, char * argv []) {Q Application A (argc, argv); Imageviewer w; W.showMaximized (); Back a.exec (); }   

imageviewer.h #include & lt; QPushButton & gt; Category Image Viewer: Public QLabel {Q_OBJECT Public: Clear imageviewer (QWidget * parent = 0); Private slot: zero mousepress event (QMouseEvent * E); Zero paint event (QPaintEvent * E); Private: QLabel * label 1; Int mFirstX; Int mFirstY; Boole MifstClick; Bull Empentflag; }; #endif

imageviewer.cpp #include & lt; QtGui & gt; # Include & lt; QHBoxLayout & gt; # Include & lt; QVBoxLayout & gt; # Include "imageviewer.h" # Include & lt; QDebug & gt; Imageviewer :: imageviewer (QWidget * Basic): QLabel (parent) {label 1 = new QLabel; Label 1- & gt; Setasys Policy (QSizePolicy :: Ignored, QSizePolicy :: ignored); QPixmap pm1 ("/ home / nishu / picture / img_0002.jpg"); Label1-> SetPixmap (PM1); Label1-> AdjustSize (); Label1-> SetScaledContents (right); QHBoxLayout * hlayout1 = new QHBoxLayout; Hlayout1-> AddWidget (label1); SetLayout (hlayout1); } Zero Image Viewer: MousePress Event (QMouseEvent * E) {mFirstX = 0; MFirstY = 0; MFirstClick = true mpaintflag = false; If (e-> (button) == Qt :: LeftButton {// Store first point if (mFirstClick) {mFirstX = E-> X (); MFirstY = E- & gt; Y (); MFirstClick = false; Mpaintflag = True; QDebug () & lt; & Lt; "Coordinate of the first image" & lt; & Lt; MFirstX & lt; & Lt; "," & Lt; & Lt; MFirstY; Updates(); }}} Void imageviewer :: paintEvent (QPaintEvent * E) {QLabel :: paintEvent (e); If (mpaintflag) {quenener painter (this); Quepen Paintpen (Qt :: Red); Paintpen.setWidth (10); QPoint p1; P1.setX (mFirstX); P1.setY (mFirstY); Painter.setPen (paintpen); Painter.drawpoint (p1); }}

Help me solve my problem?

line QPainter painter (this); With set QPainter to drag you to your main widget instead of QLabel's pixmap. Change the block on it and it will work:

  if (mpaintflag) {QImage tmp (label1-> pixmap () - & gt; toImage ()); Coupenter painter (& amp; tmp); Quepen Paintpen (Qt :: Red); Paintpen.setWidth (10); QPoint p1; P1.setX (mFirstX); P1.setY (mFirstY); Painter.setPen (paintpen); Painter.drawpoint (p1); Label1-> SetPixmap (QPixmap :: fromImage (tmp)); }   

Edit:

Just seen, that you generated from QLabel, not QWidget, as I automatically assumed, considering the layout . In fact, you do not need the layout inside the label1 and our imageviewer class. This whole thing of subclassing is that you treat behaviors and filter events the way you want Apply it and then you add them to the main widget if it is needed

EDIT2:

Imageviewer class should be derived from QLabel, delete label 1 and layout, and image But do not paint on the image viewer, i.e. this < / Code>. After that you need to add a new class to your program, which is taken from QMainwindow or QWidget, for example, where you should include your imageviewer class, create a layout and make it your class Add as:

  #include "imageviewer.h" // .... is somewhere in the producer .... imageviewer * viewer1 = new imageviewer (this); // Creating a new object for Image Visual Viewer 1- & gt; Set pixmap (...); Hlayout1-> AddWidget (viewer1);    

No comments:

Post a Comment