Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

GraphicsView and scene



  • why my this code is not running

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

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QGarphicsView w;
    QGraphicsScene *scene=new QGraphicsScene(0,0,400,400,&w);
    scene->setBackgroundBrush(Qt::yellow);
    w.setScene(scene);
    QGraphicsItem *rect=new QGraphicsItem(50,50,100,100);
    scene->addItem(rect);
    scene->addText("Hello World");
    w.show();

    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":http://qt-project.org/doc/qt-4.8/qgraphicsitem.html 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