创建图元
發(fā)布時(shí)間:2023/12/14
编程问答
24
豆豆
生活随笔
收集整理的這篇文章主要介紹了
创建图元
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
創(chuàng)建圖元
- 程序
- 主窗口函數(shù)(各種圖元的顯示)
- 閃爍橢圓函數(shù)
- 星星動(dòng)畫函數(shù)
- 效果展示
程序
主窗口函數(shù)(各種圖元的顯示)
.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>
#include <QGraphicsScene>class MainWindow : public QMainWindow
{Q_OBJECT
public:MainWindow(QWidget
*parent
= 0);~MainWindow();void initScene(); void createActions(); void createMenus();
public slots
:void slotNew(); void slotClear(); void slotAddEllipseItem(); void slotAddPolygonItem(); void slotAddTextItem(); void slotAddRectItem(); void slotAddAlphaItem(); void slotAddFlashItem(); void slotAnimationItem();
private:QGraphicsScene
*scene
;QAction
*newAct
;QAction
*clearAct
;QAction
*exitAct
;QAction
*addEllipseItemAct
;QAction
*addPolygonItemAct
;QAction
*addTextItemAct
;QAction
*addRectItemAct
;QAction
*addAlphaItemAct
;QAction
*addFlashItemAct
;QAction
*addAnimItemAct
;
};#endif
.cpp
#include "mainwindow.h"
#include <QGraphicsView>
#include <QMenu>
#include <QMenuBar>
#include <QGraphicsEllipseItem>
#include "flashitem.h"
#include "startitem.h"
#include <QGraphicsItemAnimation>
#include <QTimeLine>MainWindow
::MainWindow(QWidget
*parent
): QMainWindow(parent
)
{createActions();createMenus();scene
=new QGraphicsScene
;scene
->setSceneRect(-200,-200,400,400);initScene();QGraphicsView
*view
=new QGraphicsView
;view
->setScene(scene
);view
->setMinimumSize(400,400);view
->show();setCentralWidget(view
);resize(550,450);setWindowTitle("Graphics Items");
}MainWindow
::~MainWindow()
{}
void MainWindow
::createActions()
{newAct
= new QAction(tr("新建"),this);clearAct
= new QAction(tr("清除"),this);exitAct
= new QAction(tr("退出"),this);addEllipseItemAct
= new QAction(tr("加入 橢圓"),this);addPolygonItemAct
= new QAction(tr("加入 多邊形"),this);addTextItemAct
= new QAction(tr("加入 文字"),this);addRectItemAct
= new QAction(tr("加入 長方形"),this);addAlphaItemAct
= new QAction(tr("加入 透明圖片"),this);addFlashItemAct
=new QAction(tr("加入 閃爍圓"),this);addAnimItemAct
=new QAction(tr("加入 星星"),this);connect(newAct
,SIGNAL(triggered()),this,SLOT(slotNew()));connect(clearAct
,SIGNAL(triggered()),this,SLOT(slotClear()));connect(exitAct
,SIGNAL(triggered()),this,SLOT(close()));connect(addEllipseItemAct
,SIGNAL(triggered()),this,SLOT
(slotAddEllipseItem()));connect(addPolygonItemAct
,SIGNAL(triggered()),this,SLOT
(slotAddPolygonItem()));connect(addTextItemAct
,SIGNAL(triggered()),this,SLOT
(slotAddTextItem()));connect(addRectItemAct
,SIGNAL(triggered()),this,SLOT
(slotAddRectItem()));connect(addAlphaItemAct
,SIGNAL(triggered()),this,SLOT
(slotAddAlphaItem()));connect(addFlashItemAct
,SIGNAL(triggered()),this,SLOT
(slotAddFlashItem()));connect(addAnimItemAct
,SIGNAL(triggered()),this,SLOT
(slotAnimationItem()));
}
void MainWindow
::createMenus()
{QMenu
*fileMenu
= menuBar()->addMenu(tr("文件"));fileMenu
->addAction(newAct
);fileMenu
->addAction(clearAct
);fileMenu
->addSeparator();fileMenu
->addAction(exitAct
);QMenu
*itemsMenu
= menuBar()->addMenu(tr("元素"));itemsMenu
->addAction(addEllipseItemAct
);itemsMenu
->addAction(addPolygonItemAct
);itemsMenu
->addAction(addTextItemAct
);itemsMenu
->addAction(addRectItemAct
);itemsMenu
->addAction(addAlphaItemAct
);itemsMenu
->addAction(addFlashItemAct
);itemsMenu
->addAction(addAnimItemAct
);
}
void MainWindow
::initScene()
{int i
;for(i
=0;i
<3;i
++)slotAddEllipseItem();for(i
=0;i
<3;i
++)slotAddPolygonItem();for(i
=0;i
<3;i
++)slotAddTextItem();for(i
=0;i
<3;i
++)slotAddRectItem();for(i
=0;i
<3;i
++)slotAddAlphaItem();for(i
=0;i
<3;i
++)slotAddFlashItem();for(i
=0;i
<3;i
++)slotAnimationItem();
}
void MainWindow
::slotNew()
{slotClear();initScene();MainWindow
*newWin
= new MainWindow
;newWin
->show();
}
void MainWindow
::slotClear()
{QList
<QGraphicsItem
*> listItem
= scene
->items();while(!listItem
.empty()){scene
->removeItem(listItem
.at(0));listItem
.removeAt(0);}
}
void MainWindow
::slotAddEllipseItem()
{QGraphicsEllipseItem
*item
= new QGraphicsEllipseItem(QRectF(0,0,80,60));item
->setPen(Qt
::NoPen
);item
->setBrush(QColor(qrand()%256,qrand()%256,qrand()%256));item
->setFlag(QGraphicsItem
::ItemIsMovable
);scene
->addItem(item
);item
->setPos((qrand()%int(scene
->sceneRect().width()))-200,(qrand()%int(scene
->sceneRect().height()))-200);
}
void MainWindow
::slotAddPolygonItem()
{QVector
<QPoint
> v
;v
<<QPoint(30,-15)<<QPoint(0,-30)<<QPoint(-30,-15)<<QPoint(-30,15)<<QPoint(0,30)<<QPoint(30,15);QGraphicsPolygonItem
*item
= new QGraphicsPolygonItem(QPolygonF(v
));item
->setBrush(QColor(qrand()%256,qrand()%256,qrand()%256));item
->setFlag(QGraphicsItem
::ItemIsMovable
);scene
->addItem(item
);item
->setPos((qrand()%int(scene
->sceneRect().width()))-200,(qrand()%int(scene
->sceneRect().height()))-200);}
void MainWindow
::slotAddTextItem()
{QFont
font("Times",16);QGraphicsTextItem
*item
= new QGraphicsTextItem("Hello Qt");item
->setFont(font
);item
->setFlag(QGraphicsItem
::ItemIsMovable
);item
->setDefaultTextColor(QColor(qrand()%256,qrand()%256,qrand
()%256));scene
->addItem(item
);item
->setPos((qrand()%int(scene
->sceneRect().width()))-200,(qrand()%int(scene
->sceneRect().height()))-200);
}
void MainWindow
::slotAddRectItem()
{QGraphicsRectItem
*item
= new QGraphicsRectItem(QRectF(0,0, 60,60));QPen pen
;pen
.setWidth(3);pen
.setColor(QColor(qrand()%256,qrand()%256,qrand()%256));item
->setPen(pen
);item
->setBrush(QColor(qrand()%256,qrand()%256,qrand()%256));item
->setFlag(QGraphicsItem
::ItemIsMovable
);scene
->addItem(item
);item
->setPos((qrand()%int(scene
->sceneRect().width()))-200,(qrand()%int(scene
->sceneRect().height()))-200);
}
void MainWindow
::slotAddAlphaItem()
{QGraphicsPixmapItem
*item
=scene
->addPixmap(QPixmap("image.png"));item
->setFlag(QGraphicsItem
::ItemIsMovable
);item
->setPos((qrand()%int(scene
->sceneRect().width()))-200,(qrand()%int(scene
->sceneRect().height()))-200);
}void MainWindow
::slotAddFlashItem()
{FlashItem
*item
= new FlashItem
;scene
->addItem(item
);item
->setPos((qrand()%int(scene
->sceneRect().width()))-200,(qrand()%int(scene
->sceneRect().height()))-200);
}void MainWindow
::slotAnimationItem()
{StartItem
*item
=new StartItem
;QGraphicsItemAnimation
*anim
=new QGraphicsItemAnimation
;
歡迎分享!
轉(zhuǎn)載請(qǐng)說明來源于"生活随笔",并保留原作者的名字。
本文地址:创建图元