/*矩形*/
void MainWindow::on_actRectangle_triggered()
{
/*矩形的左上角起始坐标点-50 , -30 */
QGraphicsRectItem *item = new QGraphicsRectItem(-50 , -30 ,100, 60);
item->setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemIsMovable );
item->setBrush(Qt::yellow);
/*随机到一个位置*/
item->setPos(-100 + (qrand() % 200) , -60 + (qrand() % 120));
/*
* 图形的id号
* 针对key:Itemid,设置value:seqNum
*/
item->setData(Itemid, ++seqNum);
item->setData(ItemDescription, "QGraphicsRectItem");/*图形的描述*/
scene->addItem(item);
scene->clearSelection();
item->setSelected(true);
}
void MainWindow::on_actCircle_triggered()
{
/*圆形的左上角起始坐标点-50 , -50 */
auto *item = new QGraphicsEllipseItem(-50 , -50 ,100, 60);
item->setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemIsMovable );
item->setBrush(Qt::blue);
/*随机到一个位置*/
item->setPos(-100 + (qrand() % 200) , -60 + (qrand() % 120));
item->setData(Itemid, ++seqNum);/*图形的id号*/
item->setData(ItemDescription, "QGraphicsEllipseItem");/*图形的描述*/
scene->addItem(item);
scene->clearSelection();
item->setSelected(true);
}
void MainWindow::on_actTriangle_triggered()
{
/*三角形的左上角起始坐标点-50 , -50 */
auto *item = new QGraphicsPolygonItem;
QPolygonF points;
points.append(QPoint(0, -40));
points.append(QPoint(60, 40));
points.append(QPoint(-60, 40));
item->setPolygon(points);/*三角形设置,由3个点组成*/
item->setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemIsMovable );
item->setBrush(Qt::red);
/*随机到一个位置*/
item->setPos(-100 + (qrand() % 200) , -60 + (qrand() % 120));
item->setData(Itemid, ++seqNum);/*图形的id号*/
item->setData(ItemDescription, "QGraphicsPolygonItem");/*图形的描述*/
scene->addItem(item);
scene->clearSelection();
item->setSelected(true);
}
void MainWindow::on_actStraight_Line_triggered()
{
/*直线的左上角起始坐标点-100, 0 */
auto *item = new QGraphicsLineItem(-100, 0, 100, 0);
item->setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemIsMovable );
QPen pen;
pen.setColor(Qt::black);
pen.setWidth(5);
item->setPen(pen);
/*随机到一个位置*/
item->setPos(-100 + (qrand() % 200) , -60 + (qrand() % 120));
item->setData(Itemid, ++seqNum);/*图形的id号*/
item->setData(ItemDescription, "QGraphicsLineItem");/*图形的描述*/
scene->addItem(item);
scene->clearSelection();
item->setSelected(true);
}
void MainWindow::on_actTrapezoid_triggered()
{
/*梯形的左上角起始坐标点-50 , -50 */
auto *item = new QGraphicsPolygonItem;
QPolygonF points;
points.append(QPoint(-40, -40));
points.append(QPoint(40, -40));
points.append(QPoint(80, 40));
points.append(QPoint(-80, 40));
item->setPolygon(points);/*梯形设置,由4个点组成*/
item->setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemIsMovable );
item->setBrush(Qt::gray);
/*随机到一个位置*/
item->setPos(-100 + (qrand() % 200) , -60 + (qrand() % 120));
item->setData(Itemid, ++seqNum);/*图形的id号*/
item->setData(ItemDescription, "QGraphicsPolygonItem");/*图形的描述*/
scene->addItem(item);
scene->clearSelection();
item->setSelected(true);
}
void MainWindow::on_acItem_Text_triggered()
{
/*文字*/
QString str = QInputDialog::getText(this, "Input dialog", "please enter:");
auto *item = new QGraphicsTextItem(str);
item->setFlags(QGraphicsItem::ItemIsFocusable |
QGraphicsItem::ItemIsSelectable |
QGraphicsItem::ItemIsMovable );
QFont font;
font.setPointSize(20);
item->setDefaultTextColor(Qt::darkBlue);
item->setFont(font);
item->setTextWidth(10);
/*随机到一个位置*/
item->setPos(-100 + (qrand() % 200) , -60 + (qrand() % 120));
item->setData(Itemid, ++seqNum);/*图形的id号*/
item->setData(ItemDescription, "QGraphicsTextItem");/*图形的描述*/
scene->addItem(item);
scene->clearSelection();
item->setSelected(true);
}
211_QT_在QGraphicsView窗口中显示矩形、三角形、直线、文字、椭圆,由QGraphicsItem中各类型创建在QGraphicsScene中
最新推荐文章于 2024-08-20 00:02:02 发布