Subclass QGraphicsView issues [SOLVED]



  • 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.



  • 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)
      ...
      @



  • 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



  • 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)
      ...
      @



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



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.