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. Subclass QGraphicsView issues [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Subclass QGraphicsView issues [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 3.3k Views 1 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.
  • J Offline
    J Offline
    justinflowers
    wrote on last edited by
    #1

    Hey guys,

    Been trying to subclass QGraphicsView to add a keystroke listener. I'm getting an undefined reference when I call the constructor for my view with a scene. How do I properly define the constructor for a subclassed QGraphicsView taking in a scene such that it handles it in the exact same way a regular QGraphicsView would?

    Line in main I'm getting the error:
    @
    PieguyView view(&scene);
    // before I was using QGraphicsView view(&scene);
    @

    pieguyview.h:
    @
    #ifndef PIEGUYVIEW_H
    #define PIEGUYVIEW_H

    #include <QGraphicsView>
    #include<QKeyEvent>
    class PieguyView : public QGraphicsView
    {
    Q_OBJECT
    public:
    explicit PieguyView(QObject *parent = 0);

    protected:
    void keyPressEvent ( QKeyEvent * event );
    signals:

    public slots:

    };

    #endif // PIEGUYVIEW_H
    @

    pieguyview.cpp:
    @
    #include "pieguyview.h"
    #include<QDebug>

    void PieguyView::keyPressEvent ( QKeyEvent * event ){

    switch ( event->key())
    {
        case Qt::Key_Up:
            qDebug() << "Up";
            break;
    }
    
    QGraphicsView::keyPressEvent(event);
    

    }
    @

    exact error:
    @
    C:\Users\Justin\pieguy\main.cpp:26: error: undefined reference to `PieguyView::PieguyView(QObject*)'--
    @

    [andreyc EDIT]: Added @ around code.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on last edited by
      #2

      QGraphicsView constructor that you were using is defined like this
      @
      QGraphicsView::QGraphicsView(QGraphicsScene * scene, QWidget * parent = 0)
      @

      So you need to define the same constructor for your class

      • header file
        @
        ...
        PieguyView(QGraphicsScene * scene, QObject *parent = 0);
        ...
        @

      • source file
        @
        ...
        PieguyView::PieguyView(QGraphicsScene * scene, QObject *parent)
        : QGraphicsView(scene, parent)
        ...
        @

      1 Reply Last reply
      0
      • J Offline
        J Offline
        justinflowers
        wrote on last edited by
        #3

        Thanks for the reply!
        Tried that and getting a new error:
        @
        C:\Users\Justin\pieguy\pieguyview.cpp:18: error: invalid conversion from 'QObject*' to 'QWidget*' [-fpermissive]
        : QGraphicsView(scene, parent){
        ^
        @

        pieguyview.h:
        @
        #ifndef PIEGUYVIEW_H
        #define PIEGUYVIEW_H

        #include <QGraphicsView>
        #include<QKeyEvent>
        class PieguyView : public QGraphicsView
        {
        public:
        PieguyView(QGraphicsScene * scene, QObject *parent = 0);

        protected:
        void keyPressEvent ( QKeyEvent * event );
        signals:

        public slots:

        };

        #endif // PIEGUYVIEW_H
        @

        pieguyview.cpp:
        @
        #include "pieguyview.h"
        #include<QDebug>

        void PieguyView::keyPressEvent ( QKeyEvent * event ){

        switch ( event->key())
        {
            case Qt::Key_Up:
                qDebug() << "Up";
                break;
        }
        
        QGraphicsView::keyPressEvent(event);
        

        }

        PieguyView::PieguyView(QGraphicsScene * scene, QObject *parent)
        : QGraphicsView(scene, parent){

        }
        @

        [andreyc EDIT]: Added '@' around code. Please use @ to highlight the code

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

          Please use '@' to highlight the code.

          About the error. I copy-pasted part of your definition and did not verify that it was a QObject and not a QWidget.
          Correct declaration and definition should be

          • header file
            @
            ...
            PieguyView(QGraphicsScene * scene, QWidget *parent = 0);
            ...
            @

          • source file
            @
            ...
            PieguyView::PieguyView(QGraphicsScene * scene, QWidget *parent)
            : QGraphicsView(scene, parent)
            ...
            @

          1 Reply Last reply
          0
          • J Offline
            J Offline
            justinflowers
            wrote on last edited by
            #5

            Thank you so much!
            And many apologies for missing the @s, will do in the future haha.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on last edited by
              #6

              Glad that it works for you.
              Please add [SOLVED] to the title of your original post. It will help other people to find a possible solution.

              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