Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problems with QGraphicsView subclassing
Forum Updated to NodeBB v4.3 + New Features

Problems with QGraphicsView subclassing

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.4k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davidesalvetti
    wrote on last edited by
    #1

    Hi everybody,

    I'm having some problems subclassing QGraphicsView. I'm trying to reimplement the following functions:

    QGraphicsView::mouseMoveEvent(QMouseEvent *event)
    QGraphicsView::paintEvent(QPaintEvent *)

    I have a Dialog and what I have to do is display a chart and some other buttons, and when I move the mouse on the chart I want to draw a Cursor. I have read in some topics that the best way to do that is subclassing QGraphicsView and reimplement the two functions above. So here is my code. I take example from this topic for the subclass http://www.qtcentre.org/threads/8749-Subclassing-QGraphicsView

    @
    customgv.h
    @
    #include <QGraphicsView>
    #include <QMouseEvent>

    class QWidget;

    class customgv : public QGraphicsView
    {
    Q_OBJECT

    public:
    QPoint mousePoint;
    void mouseMoveEvent(QMouseEvent * event);
    void paintEvent(QPaintEvent *);

    public:
    customgv(QWidget *parent= NULL);
    ~customgv();
    };
    @

    customgv.cpp

    @

    #include <QtGui>
    #include "customgv.h"

    customgv::customgv(QWidget* parent)
    : QGraphicsView(parent)
    {

    setGeometry(5,5,500,500);
    setMouseTracking(true);
    

    }

    customgv::~customgv()
    {

    }

    void customgv::mouseMoveEvent(QMouseEvent *event)
    {

    mousePoint = event->pos();
    update();
    

    }

    void customgv::paintEvent(QPaintEvent *)
    {

    QPainter painter(this);
    
    painter.setPen(QPen(Qt::black, 1));
    painter.drawLine(mousePoint.x(),this->geometry().bottom() , mousePoint.x(),this->geometry().top());
    

    }
    @
    chart.cpp
    @

    //in the constructor of my new dialog chart.cpp i simply do this
    //custom is defined as a pointer to my subclass in chart.h like customgv *custom
    custom = new customgv(this);
    QChartView *chartView = new QChartView(chart,custom);

    @

    The problem is that the mouseMoveEvent is not working, when I move the mouse on the chart nothing happens and in debugging it never entries in the function. The only error qt gives me is the following:

    QWidget::paintEngine: Should no longer be called
    QPainter::begin: Paint device returned engine == 0, type: 1
    QPainter::setPen: Painter not active

    this error appears only when I open the dialog that includes my QGraphicsView subclass.
    Of course I am missing something but I can't understand what.

    Thank you all in advance.

    raven-worxR 1 Reply Last reply
    0
    • D davidesalvetti

      Hi everybody,

      I'm having some problems subclassing QGraphicsView. I'm trying to reimplement the following functions:

      QGraphicsView::mouseMoveEvent(QMouseEvent *event)
      QGraphicsView::paintEvent(QPaintEvent *)

      I have a Dialog and what I have to do is display a chart and some other buttons, and when I move the mouse on the chart I want to draw a Cursor. I have read in some topics that the best way to do that is subclassing QGraphicsView and reimplement the two functions above. So here is my code. I take example from this topic for the subclass http://www.qtcentre.org/threads/8749-Subclassing-QGraphicsView

      @
      customgv.h
      @
      #include <QGraphicsView>
      #include <QMouseEvent>

      class QWidget;

      class customgv : public QGraphicsView
      {
      Q_OBJECT

      public:
      QPoint mousePoint;
      void mouseMoveEvent(QMouseEvent * event);
      void paintEvent(QPaintEvent *);

      public:
      customgv(QWidget *parent= NULL);
      ~customgv();
      };
      @

      customgv.cpp

      @

      #include <QtGui>
      #include "customgv.h"

      customgv::customgv(QWidget* parent)
      : QGraphicsView(parent)
      {

      setGeometry(5,5,500,500);
      setMouseTracking(true);
      

      }

      customgv::~customgv()
      {

      }

      void customgv::mouseMoveEvent(QMouseEvent *event)
      {

      mousePoint = event->pos();
      update();
      

      }

      void customgv::paintEvent(QPaintEvent *)
      {

      QPainter painter(this);
      
      painter.setPen(QPen(Qt::black, 1));
      painter.drawLine(mousePoint.x(),this->geometry().bottom() , mousePoint.x(),this->geometry().top());
      

      }
      @
      chart.cpp
      @

      //in the constructor of my new dialog chart.cpp i simply do this
      //custom is defined as a pointer to my subclass in chart.h like customgv *custom
      custom = new customgv(this);
      QChartView *chartView = new QChartView(chart,custom);

      @

      The problem is that the mouseMoveEvent is not working, when I move the mouse on the chart nothing happens and in debugging it never entries in the function. The only error qt gives me is the following:

      QWidget::paintEngine: Should no longer be called
      QPainter::begin: Paint device returned engine == 0, type: 1
      QPainter::setPen: Painter not active

      this error appears only when I open the dialog that includes my QGraphicsView subclass.
      Of course I am missing something but I can't understand what.

      Thank you all in advance.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by raven-worx
      #2

      @davidesalvetti
      your biggest mistake is that you do not call the base class implementation which (basically - unless you know what you are doing) you always should do when subclassing event-handlers - or virtual functions.

      void customgv::mouseMoveEvent(QMouseEvent *event)
      {
      mousePoint = event->pos();
      update();
      QGraphicsView::mouseMoveEvent(event);
      }
      

      Also i think reimplementing drawForeground() is more what you want rather than paintEvent():

      void customgv::drawForeground(QPainter * painter, const QRectF & rect)
      {
           QGraphicsView::drawForeground(painter, rect);
      
          painter.setPen(QPen(Qt::black, 1));
          painter.drawLine(mousePoint.x(),this->geometry().bottom() , mousePoint.x(),this->geometry().top());
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      D 1 Reply Last reply
      5
      • raven-worxR raven-worx

        @davidesalvetti
        your biggest mistake is that you do not call the base class implementation which (basically - unless you know what you are doing) you always should do when subclassing event-handlers - or virtual functions.

        void customgv::mouseMoveEvent(QMouseEvent *event)
        {
        mousePoint = event->pos();
        update();
        QGraphicsView::mouseMoveEvent(event);
        }
        

        Also i think reimplementing drawForeground() is more what you want rather than paintEvent():

        void customgv::drawForeground(QPainter * painter, const QRectF & rect)
        {
             QGraphicsView::drawForeground(painter, rect);
        
            painter.setPen(QPen(Qt::black, 1));
            painter.drawLine(mousePoint.x(),this->geometry().bottom() , mousePoint.x(),this->geometry().top());
        }
        
        D Offline
        D Offline
        davidesalvetti
        wrote on last edited by
        #3

        @raven-worx

        Thanks for your anwer.

        I'm sorry, I forgot to say that I already tried adding the base class implementation, but the mouseMoveEvent didn't work, that's why I tried without that line, but of course it's not correct.

        I understand your second advice, I will use drawForeground() instead of paintEvent() in the future.

        Anyway I tried to modify all my code as you said, but mouseMoveEvent doesn't work. I tried debugging and putting a break in the mouseMoveEvent function, but it never breaks.

        I'm wondering if subclassing QGraphicsView is correct for my purpose. Maybe I have to subclass QChartView?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Asperamanca
          wrote on last edited by
          #4

          If you want a specific cursor while over the chart, it would make sense to have the chart as a separate GraphicsItem. Create your own QGraphicsItem for the chart with your paint code, and use "setCursor" to define which cursor should be visible.

          Subclassing QGraphicsView should not be necessary at all in this case.

          D 1 Reply Last reply
          1
          • A Asperamanca

            If you want a specific cursor while over the chart, it would make sense to have the chart as a separate GraphicsItem. Create your own QGraphicsItem for the chart with your paint code, and use "setCursor" to define which cursor should be visible.

            Subclassing QGraphicsView should not be necessary at all in this case.

            D Offline
            D Offline
            davidesalvetti
            wrote on last edited by
            #5

            @Asperamanca
            Thank you for the answer.

            what I really need is not only to change the cursor while i'm over the chart, I need to draw a vertical Line on the chart that follow the mouse position, and when i click the mouse the line has to be fixed at that particular position.

            For this purpose I think I need a QGraphicsView that contain the QChartView, create the QChart and then create a GraphicsItem to draw the line. Am I wrong? Shoul I do something different?

            Any advice is appreciated.

            A 1 Reply Last reply
            0
            • D davidesalvetti

              @Asperamanca
              Thank you for the answer.

              what I really need is not only to change the cursor while i'm over the chart, I need to draw a vertical Line on the chart that follow the mouse position, and when i click the mouse the line has to be fixed at that particular position.

              For this purpose I think I need a QGraphicsView that contain the QChartView, create the QChart and then create a GraphicsItem to draw the line. Am I wrong? Shoul I do something different?

              Any advice is appreciated.

              A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              @davidesalvetti
              QChart derives from QGraphicsWidget and thus indirectly from QGraphicsItem. I personally don't know QChart (so I don't know whether it's the right class for you to display what you want to display), but in any case, I would much rather suclass QChart (always making sure you call the base class methods when you replace a method), and put your logic in there.

              For example, reimplement paint, call the base class painting code first, then draw the vertical line where you need it.

              D 1 Reply Last reply
              1
              • A Asperamanca

                @davidesalvetti
                QChart derives from QGraphicsWidget and thus indirectly from QGraphicsItem. I personally don't know QChart (so I don't know whether it's the right class for you to display what you want to display), but in any case, I would much rather suclass QChart (always making sure you call the base class methods when you replace a method), and put your logic in there.

                For example, reimplement paint, call the base class painting code first, then draw the vertical line where you need it.

                D Offline
                D Offline
                davidesalvetti
                wrote on last edited by
                #7

                Thank you a lot for your answer. I will try to find a solution based on your advices. Thanks again!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved