一看代码就不用多做什么解释,新建一个举行,将它显示在window小窗口上,需要弄清楚三个概念, QGraphicsScene, QGraphicsRectItem, QGraphicsView三个类各起到什么作用。
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsRectItem>
#include <QGraphicsView>
/*
Prereqs:
-basic knowledge of c++ (pointers and memory management)
-VERY basic knowledge of Qt (widgets)
Tutorial Topics:
-QGraphicsScene
-QGraphicsItem (QGraphicsRectItem)
-QGraphicsView
*/
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// create a scene
QGraphicsScene * scene = new QGraphicsScene();
// create an item to put into the scene
QGraphicsRectItem * rect = new QGraphicsRectItem();
rect->setRect(0,0,100,100);
// add the item to the scene
scene->addItem(rect);
// add a view to visualize the scene
QGraphicsView * view = new QGraphicsView(scene);
view->show();
return a.exec();
}