PDA

View Full Version : QRubberBand problem on QGLWidget



nightroad
7th April 2011, 13:21
Hi there,

We discussed about that couple of days ago, lately i was give up but i have to use that so i wanna share some piece of code ,first of all i made a test project which have QGraphicsView and QGLWidget two of them have same CustomRB implementation QGraphicsView works well but QGLWidget looks like bugly , so there is my code and screenshot :


class hsGView : public QGraphicsView
{
Q_OBJECT

public:
hsGView(QWidget *parent = 0);
private :
CustomRB *rubberBand;
QPoint origin;

protected:

void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
};

#endif // HSGVIEW_H

#include "hsgview.h"

hsGView::hsGView(QWidget *parent) : QGraphicsView(parent)
{
rubberBand = new CustomRB(QRubberBand::Rectangle,this);
}

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

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

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



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 mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void mouseReleaseEvent(QMouseEvent *e);
void keyPressEvent(QKeyEvent *e);
};

#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());
update();
}

void HSGLWidget::keyPressEvent( QKeyEvent *e )
{

}

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




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

}
protected:

void resizeEvent(QResizeEvent *ev)
{
}

void paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QApplication::processEvents();

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();
}

};


here is the screenshot :
http://i51.tinypic.com/2ynjy3s.png

high_flyer
7th April 2011, 13:36
It seems to me you GL windget is not doing all it should, but I have little time to look deeper in to this.
Have a look at this example:
http://doc.trolltech.com/4.7/opengl-overpainting.html

nightroad
7th April 2011, 13:58
Thanks again (; high_flyer ,

I implemented overpainting example to my test project it doesn't make any sense , still flooding RubberBand rectangle :s

nightroad
8th April 2011, 06:26
someone have any idea about that ?

nightroad
12th April 2011, 07:10
any idea ? is it bug or something ?

nightroad
15th April 2011, 06:51
anyone knows, is it bug ?