Quantcast

paint on QVTKWidget

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

paint on QVTKWidget

Girish Lande
Hi All,

I am trying to show painting effect on QVTKwidget using QT painting mechanism.
As User press mouse button and move cursor I want to paint circular region under cursor. For this I have derived my widget class from QVTKwidget  and am overriding its paintevent method. 

If I do same operation while overriding my class from QWidget it works fine,
But wth QVTKWidget I am not having any luck. It doesn't show painting effect.

Following is my source code . Could you please help me figure out problem in my source code ?

header file
-----------------------
#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include <QVTKWidget.h>

class MyWidget : public QVTKWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = 0);

protected:
   // QPaintEngine * paintEngine() const;
    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent* event);
    int x;
    int y;
std::vector<int> vec;
bool m_pressed;

signals:

public slots:
};

#endif // MYWIDGET_H

Source file
------------------------

#include "mywidget.h"
#include <QPainter>
#include <QPen>
#include <QPolygon>
#include <QMouseEvent>
#include <QLabel>
#include <qdebug.h>

MyWidget::MyWidget(QWidget *parent) : QVTKWidget(parent)
{
x=100;
y=100;
m_pressed = false;
}

//QPaintEngine * MyWidget::paintEngine() const
//{
//  return QVTKWidget::paintEngine();
//}

void MyWidget::paintEvent(QPaintEvent *event)
{
    //create a QPainter and pass a pointer to the device.
    //A paint device can be a QWidget, a QPixmap or a QImage
    QPainter painter(this);

    //create a black pen that has solid line
    //and the width is 2.
    painter.setBrush(QBrush(Qt::red));
    painter.setPen(QPen(Qt::red));
    int radius = 20;
    bool point_Flag = false;
    if (!point_Flag) {
        qDebug() << "vector size:" <<vec.size();
        int xx,yy;
        for(int i=0;i<vec.size();i+=2) {
            //draw a point
            xx = vec[i];
            yy=vec[i+1];
            painter.drawEllipse(QPointF(xx,yy), radius, radius);
        }
    } else {
        int mx,my;
        for(int i=0;i<vec.size();i+=2) {
            //draw a point
            mx = vec[i];
            my = vec[i+1];
            int x1 = mx - radius;
            int x2 = mx + radius;
            int y1 = my - radius;
            int y2 = my + radius;

            double inc = 1;
            for (int _x = x1, deltax = -radius; _x <= x2; _x+=inc, deltax+=inc)
            {
                for (int _y = y1, deltay = -radius; _y <= y2; _y+=inc, deltay+=inc)
                {
                    float dist = sqrtf(deltax*deltax + deltay*deltay);
                    if (dist<radius) {
                        painter.drawPoint(_x,_y);
                    }
                }
            }
        }
    }
    QVTKWidget::paintEvent(event);
}

void MyWidget::mousePressEvent(QMouseEvent *event)
{
    x=event->x();
    y=event->y();
    m_pressed = true;
    QVTKWidget::mousePressEvent(event);
}

void MyWidget::mouseReleaseEvent(QMouseEvent *event)
{
    x=event->x();
    y=event->y();
    vec.clear();
    update();
    m_pressed = false;
    QVTKWidget::mouseReleaseEvent(event);
}

void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
    if (m_pressed) {
        x=event->x();
        y=event->y();
        vec.push_back(x);
        vec.push_back(y);
        update();
    }
    QVTKWidget::mouseMoveEvent(event);
}


--
thanks & regards,
Girish


-------------------------------------------------------------------------------------------------------------------------
Disclaimer: This email message including any attachments is confidential, and may be privileged and proprietary to Agiliad. If you are not the intended recipient, please notify us immediately by replying to this message and destroy all copies of this message including any attachments. You are NOT authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. Thank you.
------------------------------
------------------------------------------------------------------------------------------
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ

Search the list archives at: http://markmail.org/search/?q=vtkusers

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/vtkusers
Loading...