常见的QtGraphicsItem


1 创建一个以widget作为基类的项目,为了方便删除widget头文件以及其源文件,当然可以不删除


2 因为要用到c++11特性,所以在pro文件中添加语句:


[cpp]  ​​view plain​​​  ​​​copy​



  1. CONFIG += c++11


#include <QtWidgets>
#include <QApplication>


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//图形视图框架基本步骤:
//1 创建一个Scene
QGraphicsScene scene(0, 0, 500, 500);
//2 创建所需要的Item
auto hello = new QGraphicsSimpleTextItem("Hello Qt!");//auto用了c++11标准
QFont font = a.font();//设置字体样式
font.setPixelSize(30);
hello->setFont(font);
hello->setPos(100, 100);//设置位置
scene.addItem(hello);

auto reimu = new QGraphicsPixmapItem(QPixmap(":/image/images/reimu.png"));//添加图片
reimu->setPos(50, 50);
scene.addItem(reimu);

QPainterPath path;// 添加路径
path.moveTo(0, 0);
path.lineTo(50, 50);
path.lineTo(100, 50);
auto pathItem = new QGraphicsPathItem(path);
pathItem->setPos(10, 10);
scene.addItem(pathItem);

QGraphicsView view(&scene);
view.setMinimumSize(400, 400);
view.setMaximumSize(600, 600);
view.setSceneRect(0, 0, 500, 500);

view.show();

return a.exec();
}