PDA

View Full Version : problem QGLwidget responding clicking



gch0214
21st May 2012, 07:24
I added a VerticalLayout(600px*600px) in UI ,

adding it via these code:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
w = new RenjuWidget();
QRect r(0,0,600,600);
ui->verticalLayout->setGeometry(r);
ui->verticalLayout->addWidget(w); //this adds the widget
this->move(0,0);
}


my widget code is :

class RenjuWidget : public QGLWidget
{


Q_OBJECT


public:


RenjuWidget( QWidget * parent = 0 );
~RenjuWidget();


protected:


int posx, posy;


void initializeGL();
void paintGL();
void resizeGL(int width, int height);


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

my mouse click responding function is:

void RenjuWidget::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton)
{
posx = e->x();
posy = e->y();
cout<<posx<<"\t"<<posy<<endl;
paintGL();
//QMessageBox::information(NULL,QString("%1").arg(posx),QString("%1").arg(posy),QMessageBox::Ok,QMessageBox::NoButton) ;
//if I use messagebox , the problem doesn't appear
return ;
}
}

draw function is to draw a triangle on the left side if you click on the left half or a square on the right side if you clicked on the right half.

void RenjuWidget::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();


if(posx < 300)
{
glTranslatef( -1.5, 0.0, -10.0 );


glBegin( GL_TRIANGLES );
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.0, 1.0, 0.0 );
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -1.0, -1.0, 0.0 );
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 1.0, -1.0, 0.0 );
glEnd();
}


if(posx > 300)
{
glTranslatef( 1.5, 0.0, -10.0 );


glBegin( GL_QUADS );
glColor3f(1.0 , 0.0 , 0.0);
glVertex3f( -1.0, 1.0, 0.0 );
glColor3f(0.0 , 1.0 , 0.0);
glVertex3f( 1.0, 1.0, 0.0 );
glColor3f(0.0 , 0.0 , 1.0);
glVertex3f( 1.0, -1.0, 0.0 );
glColor3f(1.0 , 1.0 , 1.0);
glVertex3f( -1.0, -1.0, 0.0 );
glEnd();
}


glLoadIdentity();
}

but here comes the problem :

the picture didn't change after i clicked on it , it has to lost focus and get foucus again to redraw the picture

for example : I clicked on the left part of the widget , but it doesn't change , if i click on the desktop(or another window) , it changes immediately.

so why?:confused:

Added after 12 minutes:

OKAY……I get it solved....

I have to add "update();" in the mouse clicking function.