Important: Please read the Qt Code of Conduct -

GraphicsView and scene

  • why my this code is not running

    @#include <QtGui/QApplication>
    //#include "MainWindow.h"

    int main(int argc, char *argv[])
    QApplication a(argc, argv);
    QGarphicsView w;
    QGraphicsScene *scene=new QGraphicsScene(0,0,400,400,&w);
    QGraphicsItem *rect=new QGraphicsItem(50,50,100,100);
    scene->addText("Hello World");;

    return a.exec&#40;&#41;;


    it shows the following error

    1.QGraphicsView was not declared in this scope.
    2.expected ';' before 'w'
    3. 'w ' was not declared in this scope
    4.Invalid use of incomplete type 'struct QGraphicsItem'
    5.forwared declaration of 'struct QGraphicsItem'

  • Well, firstly you must change QGarphicsView for QGraphicsView, then you can not allocate an abstract object, QGraphicsItem is an abstract object, so you need to use QGraphicsRectItem, and if you want you can derive from QGraphicsItem. Pleace read "this": information.

  • Moved to General, it's a Qt question. Also, the audience there should be much larger to answer your question.

  • In addition to what Mariø™ said: You need to include the header for the item you are using. So if you want to use QGraphicsRectItem you should
    #include <QGraphicsRectItem>

Log in to reply