Game Programming using Qt 5 Beginner's Guide
上QQ阅读APP看书,第一时间看更新

Time for action – Creating a sine graph project

Use Qt Creator to create a new Qt Widgets project and name it sine_graph. On the Class Information page of the wizard, select QWidget as the base class and input View as the class name. Uncheck the Generate form checkbox and finish the wizard.

We want the View class to be the graphics view, so you need to change the base class to QGraphicsView (the wizard doesn't suggest such an option). For this, edit the class declaration to look like class View : public QGraphicsView ... and the constructor implementation to look like View::View(QWidget *parent) : QGraphicsView(parent) ....

Next, edit the View constructor to enable anti-aliasing and set a new graphics scene for our view:

setRenderHint(QPainter::Antialiasing);
setScene(new QGraphicsScene);

The view doesn't delete the associated scene on destruction (because you may have multiple views for the same scene), so you should delete the scene manually in the destructor:

delete scene();

You can try to run the application and check that it displays an empty view.