PDA

View Full Version : QRubberBand problem



nightroad
28th March 2011, 12:43
Hi there,

I'm using customGL widget which derived from QGLWidget. I wanna use QRubberBand on this widget my code like this ;


class HSGLWidget : public QGLWidget
{
Q_OBJECT

public:
HSGLWidget(QWidget *parent = 0);

private:
QRubberBand *rubberBand;
QPoint origin;

protected:

void initializeGL();
void resizeGL(int w, int h);
//void paintGL();

void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void keyPressEvent(QKeyEvent *e);
};


void HSGLWidget::mousePressEvent( QMouseEvent *e )
{
origin = e->pos();
if (!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();

}

void HSGLWidget::mouseMoveEvent( QMouseEvent *e )
{
rubberBand->setGeometry(QRect(origin, e->pos()).normalized());

}

void HSGLWidget::mouseReleaseEvent(QMouseEvent *e)
{
rubberBand->hide();
}



It's working with this implementation but there is something weird when i press the mouse button and moving mouse i see many Qrubberband flooding on the HSGLWidget if i move mouse quickly there is few if move slowly there is huge flooding. What is the problem am i using something wrong or this is QRubberBand issue , thanks for you help.

high_flyer
28th March 2011, 13:03
show your implementation - specially where you allocate 'rubberBand'.

nightroad
28th March 2011, 13:06
thx for reply ; I allocated it


if (!rubberBand)
rubberBand = new QRubberBand(QRubberBand::Rectangle, this);

high_flyer
28th March 2011, 13:18
If its not a case of re-allocation of the rubber band, then it probably has to do with the way you update the QGLWidget such that the old content is not cleared, and only the new one is added.

nightroad
28th March 2011, 13:44
If its not a case of re-allocation of the rubber band, then it probably has to do with the way you update the QGLWidget such that the old content is not cleared, and only the new one is added.

yup, it's look like my problem , thenm i don't know how can i refresh my HSGLWidget , could you help me about that ?

high_flyer
28th March 2011, 14:46
I can try, but there are all kind of things that can play in to this.
I will try the simple stuff first.
Show the implementation of the constructor.

nightroad
28th March 2011, 15:53
thanks for your help high_flyer ,

to be honest i just trying to figur out using of RubberBand on simply GLWidget, so there is no code at ctor,

nightroad
29th March 2011, 07:04
I really stuck with refresh CustomWidgets content, could you help figure out this ?

high_flyer
29th March 2011, 08:41
I mean the ctor of your QGLWidget implementation.

nightroad
29th March 2011, 08:48
hi, high_flyer ;

As i said before for moment there is no code at ctor. I'm just trying using QRubberBand on QGLWidget so there is problem as you noticed QGLWidget doesn't refresh itself and there is problem like flooding QRubberBand, can i use QRubberBand on QGLWidget actually i wanna know this atm ... Thanks for your interest ...

high_flyer
29th March 2011, 09:47
Ok, so it means you have default flags on it - but then I don't know why its not getting updates when you drag the rubber band on it.
You can try sub classing QRubberBand, and override the resizeEvent() and send a signal from it.
And in your QGLWidget, intercept that signal, and call update().

nightroad
29th March 2011, 09:58
Thanks for advice high_flyer, i've actually tried bind a pushbutton clicked signal with qglwidget its , update and repaint slots but it doesn't any effect, i even tried custom rubberband which derived form Qrubberband override paint events also it's not working too =) , so let me know is it possible to use QRubberBand on QGLWdiget or not ?

high_flyer
29th March 2011, 10:20
is it possible to use QRubberBand on QGLWdiget or not ?
It must be possible, since QGLWidget is a QWidget.

I don't know what it is you are doing wrong, based on the information you provided so far...

Try to make a test project where you only have a widget and QRubberBand drawn on it.
Once you get it to work there, try comparing between that and you QGLWidget usage of QRubberBand.

nightroad
29th March 2011, 11:38
hi high_flyer ;

I found something when i was digging to google :

myRubberBand = new QRubberBand( QRubberBand::Rectangle, this );
00090 if (myRubberBand)
00091 {
00092 // If you don't set a style, QRubberBand doesn't work properly
00093 // take these lines out if you don't believe me.
00094 // QStyle* ps = (QStyle*) new QutlProxyStyle("Plastique");
00095 QStyle* ps = (QStyle*) new QPlastiqueStyle();
00096 myRubberBand->setStyle( ps );
00097 }

many people have same problem with QGLWidget so there is a solution who claim that but it's not workinf still :s

Added after 5 minutes:

So i made a simple test project i send fully code what i've done ;


#ifndef HSGLWIDGET_H
#define HSGLWIDGET_H

#include <QtOpenGL/QGLWidget>
#include <QMouseEvent>
#include <QRubberBand>
#include <QPoint>

class CustomRB : public QRubberBand
{
public:
CustomRB(Shape s, QWidget *parent) : QRubberBand(s,parent) {

}

void paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);

QPainter painter;
QPen pen = QPen(Qt::yellow);
pen.setWidth(5);
pen.setStyle(Qt::DashLine);

QBrush brush = QBrush(Qt::red);

painter.begin(this);
painter.setPen(pen);
painter.setOpacity(0.5);
painter.setBrush(brush);
painter.drawRect(event->rect());
painter.end();
}

};


class HSGLWidget : public QGLWidget
{
Q_OBJECT


public:
HSGLWidget(QWidget *parent = 0);

private:
CustomRB *rubberBand;
QPoint origin;

protected:

void initializeGL();
void resizeGL(int w, int h);
//void paintGL();

void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void keyPressEvent(QKeyEvent *e);
};


#endif // HSGLWIDGET_H

#include "hsglwidget.h"

HSGLWidget::HSGLWidget(QWidget *parent)
:QGLWidget(parent),rubberBand(0)
{
rubberBand = new CustomRB(QRubberBand::Rectangle, this);

}

void HSGLWidget::initializeGL()
{

}

void HSGLWidget::resizeGL( int w, int h )
{

}

void HSGLWidget::mousePressEvent( QMouseEvent *e )
{
origin = e->pos();
rubberBand->setUpdatesEnabled(true);
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
}

void HSGLWidget::mouseMoveEvent( QMouseEvent *e )
{
rubberBand->setGeometry(QRect(origin, e->pos()).normalized());
}

void HSGLWidget::keyPressEvent( QKeyEvent *e )
{

}

void HSGLWidget::mouseReleaseEvent(QMouseEvent *e)
{
rubberBand->setGeometry(QRect(origin, e->pos()).normalized());
rubberBand->show();
}


I promote to Widget in qtcreator with hsglwidget so it's not working properly ...

high_flyer
29th March 2011, 12:48
What happens if in the original code you posted in the firt post you do this:


void HSGLWidget::mouseMoveEvent( QMouseEvent *e )
{
rubberBand->setGeometry(QRect(origin, e->pos()).normalized());
update(); //add this
}

nightroad
29th March 2011, 15:11
What happens if in the original code you posted in the firt post you do this:


void HSGLWidget::mouseMoveEvent( QMouseEvent *e )
{
rubberBand->setGeometry(QRect(origin, e->pos()).normalized());
update(); //add this
}



Mate Thanks for your effort , it doesn't any effect on QGLWidget =) , anyway i already give up =.=