天天看點

常見的QGraphicsItem 練習

各種item的介紹參考以下部落格連結

https://www.cnblogs.com/aiguona/p/10002255.html

https://www.jianshu.com/p/7724fbd07b1a

本文隻是對以下的item做了練習的代碼進行記錄,未對其進行詳細介紹

1.QGraphicsSimpleTextItem

//------------QGraphicsSimpleTextItem-----簡單文本标簽----------------//
    QGraphicsSimpleTextItem *simpleItem = new QGraphicsSimpleTextItem;
    simpleItem->setText("這是一個QGraphicsSimpleTextItem");
    QFont font = simpleItem->font();
    font.setPixelSize(20);
    font.setItalic(true);
    font.setUnderline(true);
    simpleItem->setFont(font);
    simpleItem->setBrush(QBrush(QColor(0,160,230)));
    scene->addItem(simpleItem);
   
           
常見的QGraphicsItem 練習

2.QGraphicsTextItem

//-----------QGraphicsTextItem---------------------------------------------//
    QGraphicsTextItem *textItem = new QGraphicsTextItem;
    textItem->setPlainText("這是一個QGraphicsTextItem");  // 純文字
    textItem->setDefaultTextColor(QColor(0, 160, 230));  // 文本色
    textItem->setPos(50,60);
    textItem->setTextWidth(qreal(60));
    QFont font2 = textItem->font();
    font.setPixelSize(20);  // 像素大小
    font.setItalic(true);  // 斜體
    font.setUnderline(true);  // 下劃線
    textItem->setFont(font2);
    scene->addItem(textItem);
           
常見的QGraphicsItem 練習

3.QGraphicsLineItem

//-----------QGraphicsLineItem-----------------------------//
	
	QGraphicsLineItem *lineItem = new QGraphicsLineItem;
	lineItem->setLine(20, 120, 120,120);	
	scene->addItem(lineItem);
           
常見的QGraphicsItem 練習

4.QGraphicsPixmapItem

//--------------QGraphicsPixmapItem------------------------------------------//
	QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem;
	QPixmap pixmap("E://xxx//program//Graphics//debug/1.png");
	pixmapItem->setPixmap(pixmap.scaled(150,150));
	scene->addItem(pixmapItem);
           
常見的QGraphicsItem 練習

5.QGraphicsRectItem

//--------------QGraphicsRectItem------------------------------------------//
	QGraphicsRectItem *rectItem = new QGraphicsRectItem;
	QPen pen2 = rectItem->pen();
	pen2.setWidth(5);
	pen2.setColor(Qt::green);//外邊框的顔色
	rectItem->setPen(pen2);
	rectItem->setBrush(QBrush(QColor(0,160,230)));//填充顔色
	rectItem->setRect(QRectF(150,0,200,100));
	scene->addItem(rectItem);   
           
常見的QGraphicsItem 練習

6.QGraphicsEllipseItem

//-------------QGraphicsEllipseItem -------------------------------------------//
	QGraphicsEllipseItem *ellipseitem = new QGraphicsEllipseItem;
	QPen pen3 = ellipseitem->pen();
	pen3.setWidth(5);
	pen3.setColor(Qt::white);
	ellipseitem->setPen(pen3);
	ellipseitem->setBrush(QBrush(QColor(0, 160, 230)));
	ellipseitem->setRect(QRectF(0, 240, 200, 100));
	ellipseitem->setStartAngle(16*90);//QT在繪制弧度時是以1/16為機關畫角度,是以在設定角度時*16
	ellipseitem->setSpanAngle(16*270);//跨度是指橢圓的角度是16*270
	scene->addItem(ellipseitem);
           
常見的QGraphicsItem 練習

7.QGraphicsPathItem

//--------------QGraphicsPathItem------------------------------------------//
	const float Pi = 3.14159f;
	QGraphicsPathItem *pathitem = new QGraphicsPathItem;
	//畫星星
	QPainterPath starPath;
	starPath.moveTo(90,50);
	
	for (int i = 1; i < 5; ++i) {
		starPath.lineTo(50 + 40 * std::cos(0.8 * i * Pi),
			50 + 40 * std::sin(0.8 * i * Pi));
	}
	starPath.closeSubpath();
	pathitem->setPath(starPath);
	QPen pen4 = ellipseitem->pen();
	pen4.setWidth(5);
	pen4.setColor(Qt::white);
	pathitem->setPen(pen4);
	pathitem->setBrush(QBrush(QColor(0, 160, 230)));

	scene->addItem(pathitem);
           
常見的QGraphicsItem 練習

8.QGraphicsPolygonItem

//-----------------QGraphicsPolygonItem -----------------------//
	QGraphicsPolygonItem *pItem = new QGraphicsPolygonItem();

	// 繪制多邊形
	QPolygonF polygon;
	polygon << QPointF(200.0, 120.0) << QPointF(230.0, 130.0)
		<< QPointF(260.0, 180.0) << QPointF(200.0, 200.0);
	pItem->setPolygon(polygon);

	// 設定畫筆、畫刷
	QPen pen5 = pItem->pen();
	pen5.setWidth(2);
	pen5.setColor(Qt::black);
	pItem->setPen(pen5);
	pItem->setBrush(QBrush(QColor(0, 160, 230)));
	scene->addItem(pItem);
           
常見的QGraphicsItem 練習
Qt

繼續閱讀