PDA

View Full Version : QRubberBand class to draw a line



vermarajeev
2nd April 2007, 14:04
Hi how to use QRubberBand class to draw a line. According to docs, we can either draw a rectangle or a line. I'm able to draw a rectangle but not a line. Here is the sample code I have tried


Myline::Myline( QWidget *parent, Qt::WFlags flags)
: QWidget(parent, flags){
RBand = 0;
}

void Myline::mousePressEvent(QMouseEvent *event){
first_pt = event->pos();
last_pt = event->pos();
if (!RBand)
RBand = new QRubberBand(QRubberBand::Line, this);
RBand->setGeometry(QRect(first_pt, last_pt));
RBand->show();
}

void Myline::mouseMoveEvent(QMouseEvent *event) {
last_pt = event->pos();
RBand->setGeometry(QRect(first_pt, last_pt));
}

void Myline::mouseReleaseEvent(QMouseEvent *event) {
last_pt = event->pos();
RBand->hide();
}

Thanks

wysota
2nd April 2007, 14:27
What exactly happens? Do you see a rectangle?

vermarajeev
2nd April 2007, 14:34
What exactly happens? Do you see a rectangle?

Yes I see a rectange...though I have given shape as a line.

pdolbey
2nd April 2007, 19:33
Do you know, I suffered from a different problem where my QRubberBand drawn with QRubberBand:Rectangle always drew a filled rectangle, except when the widget style was set to "Plastique". So when I saw your post I looked up the documentation - I've replicated it below.

enum QRubberBand::Shape
This enum specifies what shape a QRubberBand should have. This is a drawing hint that is passed down to the style system, and can be interpreted by each QStyle.
QRubberBand::Line (0)

A QRubberBand can represent a vertical or horizontal line. Geometry is still given in rect() and the line will fill the given geometry on most styles. QRubberBand::Rectangle (1)

A QRubberBand can represent a rectangle. Some styles will interpret this as a filled (often semi-transparent) rectangle, or a rectangular outline.
Now from this, it looks to me that QRubberBand:Line is a drawing hint for how the rubber band box should be drawn - which seems a bit counter-intuitive with respect to naming. I've interpreted this one way and the OP another, and its not clear what the correct result is supposed to be - who would want just a vertical or a horizontal line? However, I'm going to try using "Line" in my code without the Plastique style and see if it gives the same result as he got (mine is overlaid onto an OpenCascade widget), because its the effect I actually wanted.
It looks like the QRubberBand widget is designed to support a "window selection" UI metaphor and I suspect the OP wants a "dynamic line" response. That's probably better modelled with a click to start, click to end type interaction and a repaint of the window for each mouse move event with the new "line" added. Without seeing the actual paint event it is difficult to see how to optimise this (Bit-blits, XORs, layers, back buffers) etc. But I'm sure once we see this, the gurus will be able to figure out the best option:D

Pete

Kumosan
2nd April 2007, 19:45
What happens when you set height/width explicitly like this:

RBand->setGeometry(first_pt.x(),0,1,parent->height());
RBand->setGeometry(0,first_pt.y(),parent->width(),1);

fullmetalcoder
2nd April 2007, 19:56
If your line is horizontal or vertical you should use a QFrame instead (just like Qt Designer does) and if it not then you can always create a custom widget drawing a proper QLine on paintEvent... I guess it shouldn't be to hard. :)

pdolbey
2nd April 2007, 21:28
Well I tried various combinations with some *very* wierd results - and my rubber band box is back to Rectangle with Plastique!

Pete

Kumosan
2nd April 2007, 21:38
Well I tried various combinations with some *very* wierd results - and my rubber band box is back to Rectangle with Plastique!

Pete

This is strange. I am using platique and exactly the code lines I gave you. Works like a charm. Tested on Qt 4.2.3 + Qt 4.3.0beta on Linux and Qt 4.2.3 on Windows XP. However I am not sure about the style on Windows. Never bothered to look.
.

vermarajeev
3rd April 2007, 04:50
What happens when you set height/width explicitly like this:

RBand->setGeometry(first_pt.x(),0,1,parent->height());
RBand->setGeometry(0,first_pt.y(),parent->width(),1);

Can we see your code????


If your line is horizontal or vertical you should use a QFrame instead (just like Qt Designer does) and if it not then you can always create a custom widget drawing a proper QLine on paintEvent... I guess it shouldn't be to hard.

I have already done that just wanted to see if I can achieve the same result using QRubberBand class.

Kumosan
3rd April 2007, 06:53
Can we see your code????

Sure.


http://spectrascan.svn.sourceforge.net/viewvc/spectrascan/SpectraScan/ImageHandling/MouseHandler/LineRubberBand.cpp?view=markup