[vtkusers] paint on QVTKWidget

Girish Lande girish.lande at agiliad.com
Fri Mar 24 01:18:37 EDT 2017


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.
------------------------------------------------------------
------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20170324/699e2de5/attachment.html>


More information about the vtkusers mailing list