1.关于QT中的Graphics绘图,定时器,动画,将窗口中的内容打印到图片上,打印机,打印预览
生活随笔
收集整理的這篇文章主要介紹了
1.关于QT中的Graphics绘图,定时器,动画,将窗口中的内容打印到图片上,打印机,打印预览
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1 新建項(xiàng)目
A? 修改pro中的內(nèi)容如下:
| HEADERS += \ ??? MyWidget.h ? SOURCES += \ ??? MyWidget.cpp ? QT += gui widgets printsupport |
B? 編寫(xiě)MyWidget.h
| #ifndef MYWIDGET_H #define MYWIDGET_H ? #include <QWidget> #include <QGraphicsScene> #include <QGraphicsLineItem> #include <QGraphicsPixmapItem> #include <QGraphicsTextItem> #include <QGraphicsPixmapItem> #include <QGraphicsItemAnimation> #include <QTimeLine> ? #include <QPrintPreviewDialog>? // preview #include <QPrintDialog>???????? // print ? // display scene #include <QGraphicsView> ? #include <QTimer> #include <QDateTime> ? class MyWidget : public QWidget { ??? Q_OBJECT public: ??? explicit MyWidget(QWidget *parent = 0); ??? void paintEvent(QPaintEvent *); ? ??? QGraphicsScene* _scene;? // data model ??? QGraphicsView* _view;?? // show the data model ? ??? void resizeEvent(QResizeEvent *); ??? void mousePressEvent(QMouseEvent *); ? ??? QTimer* _timer; signals: ? public slots: ??? void slotPaintRequested(QPrinter*); ??? void slotTimeout(); ? }; ? #endif // MYWIDGET_H |
C 編寫(xiě)MyWidget.cpp,內(nèi)容如下:
| #include "MyWidget.h" #include <QApplication> #include <QVBoxLayout> #include <QMouseEvent> #include <QDebug> #include <QPrinter> ? MyWidget::MyWidget(QWidget *parent) : ??? QWidget(parent) { ??? QGraphicsLineItem* lineItem; ??? QGraphicsTextItem* textItem; ??? QGraphicsPixmapItem* pixmapItem; ? ??? //定義一個(gè)view ??? _view = new QGraphicsView(this); ??? //讓view的背景顏色編程黃色的 ??? _view->setBackgroundBrush(Qt::yellow); ? ??? //在view中添加scene ??? _view->setScene(_scene = new QGraphicsScene); ? ??? //在scene中添加一條線(xiàn) ??? _scene->addItem(lineItem = new QGraphicsLineItem(QLineF(QPointF(0, 0), QPointF(100, 100)))); ??? //在scene中畫(huà)線(xiàn) ??? _scene->addItem(textItem = new QGraphicsTextItem("Hello world")); ??? //在scene中添加一個(gè)pixmap ??? _scene->addItem(pixmapItem = new QGraphicsPixmapItem(QPixmap("../aaa.png"))); ? ??? //定義一個(gè)轉(zhuǎn)換 ??? QTransform trans; ??? //這個(gè)轉(zhuǎn)換效果可以旋轉(zhuǎn)30度 ??? trans.rotate(30); ? ??? textItem->setPos(QPointF(0, 300)); ??? textItem->setTransform(trans); ??? textItem->setFont(QFont("aaa", 50, 700, true)); ? ??? pixmapItem->setPos(100, 100); ? ??? //Animation 等價(jià)于 cocos2dx中的Action(動(dòng)作) ??? QGraphicsItemAnimation* animation = new QGraphicsItemAnimation; ??? animation->setItem(pixmapItem); ? ??? //設(shè)置一個(gè)事件線(xiàn),表示執(zhí)行一次動(dòng)作所需要的時(shí)間,以毫秒值為參數(shù) ??? QTimeLine* timeline = new QTimeLine(3000); ??? //表示這個(gè)動(dòng)作循環(huán)執(zhí)行多少次 ??? timeline->setLoopCount(2); ? ??? //這個(gè)動(dòng)畫(huà)開(kāi)始以timeline為配置執(zhí)行動(dòng)作 ??? animation->setTimeLine(timeline); ??? //在1秒內(nèi),移動(dòng)到200,200這個(gè)位置 ??? animation->setTranslationAt(1, 200, 200); ? ??? //開(kāi)始執(zhí)行 ??? timeline->start(); ? ??? /**************上面代碼是可以獨(dú)立運(yùn)行的****************/ ??? //下面的方式定義一個(gè)定時(shí)器 ??? _timer = new QTimer(); ??? //每隔1秒鐘執(zhí)行一次 ??? _timer->setInterval(1000); ??? //使信號(hào)和槽函數(shù)聯(lián)系起來(lái)執(zhí)行 ??? connect(_timer, SIGNAL(timeout()), this, SLOT(slotTimeout())); ??? _timer->start(); ? ??? //下面的方法讓定時(shí)器只執(zhí)行一次 ??? //QTimer::singleShot(1000, this, SLOT(slotTimeout())); } ? /** * @brief MyWidget::slotTimeout 每隔1秒鐘會(huì)執(zhí)行下面的函數(shù)一次 */ void MyWidget::slotTimeout() { ??? qDebug() << "Time out"; } ? //上面運(yùn)行出的結(jié)果如下: void MyWidget::resizeEvent(QResizeEvent *) { ??? // set the size of _view = MyWidget::size ??? _view->setGeometry(QRect(QPoint(0, 0), size())); } ? void MyWidget::paintEvent(QPaintEvent *) { } ? /* ?* QPixmap 平臺(tái)優(yōu)化了的一種圖,人看起來(lái)是一樣的,但是在不同的平臺(tái),調(diào)用的不同平臺(tái)的底層接口 * QImage 在所有的平臺(tái)都是一樣的,這里是一個(gè)位圖 * QBitmap 灰度圖 * QPicture 說(shuō)白了就是一個(gè)軌跡圖,通過(guò)這個(gè)軌跡圖能夠畫(huà)圖圖像 */ void MyWidget::mousePressEvent(QMouseEvent *ev) { ??? if(ev->button() == Qt::RightButton) ??? { #if 0?? //只需放開(kāi)下面的代碼即可執(zhí)行,放開(kāi)這里的時(shí)候,當(dāng)右擊鼠標(biāo)的 ??????? //時(shí)候發(fā)現(xiàn)在相應(yīng)的目錄下出現(xiàn)了圖片 ??????? // save the view 通過(guò)下面的一段代碼實(shí)現(xiàn)了將窗口中的內(nèi)容保存到圖片上了 ??????? // 這里的size()是窗口的大小 ??????? QPixmap pixmap(size()); ??????? QPainter painter(&pixmap); ??????? painter.fillRect(QRect(0, 0, size().width(), size().height()), Qt::white); ? ??????? _view->render(&painter); ??????? pixmap.save("../bbb.png"); ? //這里運(yùn)行的結(jié)果如下: #endif #if 0 ??????? //下面是打印預(yù)覽的功能,放開(kāi)此處的時(shí)候右擊鼠標(biāo)的時(shí)候會(huì)出現(xiàn)打印預(yù)覽的功能 ??????? QPrintPreviewDialog dlg; ??????? connect(&dlg, SIGNAL(paintRequested(QPrinter*)), this, SLOT(slotPaintRequested(QPrinter*))); ??????? dlg.exec(); //這里的運(yùn)行結(jié)果如下: #endif #if 0?? //下面的打印的功能,放開(kāi)此處會(huì)出現(xiàn)讓選擇打印機(jī)的窗口 ??????? QPrintDialog dlg; ??????? connect(&dlg, SIGNAL(accepted(QPrinter*)), this, SLOT(slotPaintRequested(QPrinter*))); ??????? dlg.exec(); //這里的運(yùn)行結(jié)果如下: #endif ??? } } ? void MyWidget::slotPaintRequested(QPrinter *printer) { ??? QPainter painter(printer); ??? _scene->render(&painter); ??? //說(shuō)明直接可以通過(guò)painter打印出文字 ??? painter.drawText(QPoint(100, 100), "Fuck"); } ? int main(int argc, char* argv[]) { ??? QApplication app (argc,argv); ? ??? MyWidget w; ??? w.showMaximized(); ??? return app.exec(); } |
?
總結(jié)
以上是生活随笔為你收集整理的1.关于QT中的Graphics绘图,定时器,动画,将窗口中的内容打印到图片上,打印机,打印预览的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: shell的输入和输出
- 下一篇: 2.关于QT中的Dialog(模态窗口)