Hi.
I'd like to know how to draw a selection rectangle over a label (QLabel) so that the QLabel continues to show. I think I need to somehow call QLabel::PaintEvent, but I don't know how.
Thanks.
Hi.
I'd like to know how to draw a selection rectangle over a label (QLabel) so that the QLabel continues to show. I think I need to somehow call QLabel::PaintEvent, but I don't know how.
Thanks.
tommy (16th November 2007)
Thanks for the idea. I looked up QRubberBand but can't get it to work (getting "origin" and "m_pixmapLabel" not defined errors). I am misunderstanding something. I put my code below. Thanks!
Qt Code:
#include <QApplication> #include <QtGui> #include "definitions.h" int origin =0; { QPixmap pm; pm.load("image.bmp"); m_pixmapLabel->setPixmap(pm); mainLayout->addWidget(m_pixmapLabel); setLayout(mainLayout); } { } { origin = event->pos(); rubberBand->show(); } { rubberBand->hide(); } int main(int argc, char *argv[]) { MyWidget window; window.show(); return application.exec(); }To copy to clipboard, switch view to plain text mode
Well, the documentation expects you to know basics of C++. You must define them as member variables:
PS. I presumed you meant "rubberBand" with "m_pixmapLabel" since QRubberBand docs talk nothing about "m_pixmapLabel".Qt Code:
class MyWidget { ... private: QPoint origin; QRubberBand* rubberBand; };To copy to clipboard, switch view to plain text mode
J-P Nurmi
tommy (16th November 2007)
Thanks again jpn. I had forgotten QPoint and I made the change. QRubberBand was declared correctly all along. Nevertheless the code won't work (compains about origin and mouseReleaseEvent). I pasted the current code again - in case you have a minute. Thanks a lot.
Qt Code:
#include <QApplication> #include <QtGui> #include "tom.h" { origin.setX(0); origin.setY(0); QPixmap pm; pm.load("image.bmp"); m_pixmapLabel->setPixmap(pm); mainLayout->addWidget(m_pixmapLabel); setLayout(mainLayout); } { } { origin = event->pos(); rubberBand->show(); } { rubberBand->hide(); } int main(int argc, char *argv[]) { MyWidget window; window.show(); return application.exec(); }To copy to clipboard, switch view to plain text mode
Qt Code:
#ifndef TOM_H #define TOM_H #include <QtGui> { Q_OBJECT public: protected: private: QLabel* m_pixmapLabel; QRubberBand* rubberBand; QPoint origin; }; #endifTo copy to clipboard, switch view to plain text mode
missing a semicolon after mouseReleaseEvent in the header.....
tommy (16th November 2007)
Always paste the error whenever you bump into such situation, please. Seeing the error message helps us a lot to tell where's the problem. We are not walking compilers...
This is clearly wrong, "origin" was not declared as pointer. You can simply drop the line.
You can drop these as well. As you can see from the documentation, the default constructor of QPoint already initializes the point to (0,0).
J-P Nurmi
tommy (16th November 2007)
jpn (16th November 2007)
Hey now, what is this blatant nepotism... Admins handing thanks out to each other like it was candy..
Hi jpn,
I think you got it wrong. I think you are a walking compiler - and more. You're better than a compiler because you can trouble-shoot so much better than a compiler . Anyway, thank you so much for being so helpful and patient with someone who doesn't yet know a whole lot about Qt or C++.
If I may ask one last question here, that would be:
What's the best way to get local coordinates out of the rubberband once the label which I rubberband (this is a verb as in "to rubberband something") becomes a part of my main layout?
I intend to start using the rubberband for cropping.
Thanks
Well... J-P is not an admin And I'm really an AI - ask Kumosan, he'll tell ya...
Indeed, I'm no admin. And I never thought I was spreading too many "thanks". Also, believe or not, that really cheered me up!
Child widget's coordinates are always relative to its parent. You can use various QWidget::mapTo*() and QWidget::mapFrom*() methods to map the coordinates if needed.
J-P Nurmi
Once you're done dragging the rubberband (in the mouseReleaseEvent for example) you can use rubberBand->geometry() to get the rectangle defining this rubber band (in the coordinates of the parent).
That's great, the rubberband works great for me now and I also know how to get the coordinates out, but the problem is that my picture that I display (as part of the label) is a bit smaller than the label; also, the label does not extend all the way to the edges of the main layout. As a result of that the upper left coordinate of my picture is about (10,10) as opposed to the desired (0,0). And worse yet: as I expand and shrink the window, my label swims around in it and the coordinates change all the time. How'm I ever going to use this approach for cropping of my pixmap image.
Map the coordinates of the rubberband to the label geometry using QWidget::mapTo.
tommy (17th November 2007)
....so which way does the mapTo() work?
If this is the syntax:
myPoint = mapTo(my_QLabel_name, QPoint(0,0));
is the myPoint then going to hold the absolute coordinates (two positive numbers) of the upper left of the label named my_QLabel_name?
What function then separates myPoint (a QPoint) into two numbers : x and y?
It's obvious once you take a look at QPoint docs, isn't it? Try to avoid asking every single question that comes to mind on the forums, please. You know, it doesn't make yourself look very good.. Docs are the number #1 resource after all!
J-P Nurmi
Thanks for your help, I figured it all out now
Last edited by tommy; 17th November 2007 at 20:20.
Bookmarks