1.关于QT中的Graphics绘图,定时器,动画,将窗口中的内容打印到图片上,打印机,打印预览
1
新建项目
A
修改pro中的内容如下:
HEADERS MyWidget.h SOURCES MyWidget.cpp QT |
B
编写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); 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
编写MyWidget.cpp,内容如下:
#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; //定义一个view _view = new QGraphicsView(this); //让view的背景颜色编程黄色的 _view->setBackgroundBrush(Qt::yellow); //在view中添加scene _view->setScene(_scene = new QGraphicsScene); //在scene中添加一条线 _scene->addItem(lineItem = new QGraphicsLineItem(QLineF(QPointF(0, 0), QPointF(100, 100)))); //在scene中画线 _scene->addItem(textItem = new QGraphicsTextItem("Hello world")); //在scene中添加一个pixmap _scene->addItem(pixmapItem = new QGraphicsPixmapItem(QPixmap("../aaa.png"))); //定义一个转换 QTransform trans; //度 trans.rotate(30); textItem->setPos(QPointF(0, 300)); textItem->setTransform(trans); textItem->setFont(QFont("aaa", 50, 700, true)); pixmapItem->setPos(100, 100); //Animation 等价于 cocos2dx中的Action(动作) QGraphicsItemAnimation* animation = new QGraphicsItemAnimation; animation->setItem(pixmapItem); //设置一个事件线,表示执行一次动作所需要的时间,以毫秒值为参数 QTimeLine* timeline = new QTimeLine(3000); //表示这个动作循环执行多少次 timeline->setLoopCount(2); //这个动画开始以timeline为配置执行动作 animation->setTimeLine(timeline); //秒内,移动到200,200这个位置 animation->setTranslationAt(1, 200, 200); //开始执行 timeline->start(); /**************上面代码是可以独立运行的****************/ //下面的方式定义一个定时器 _timer = new QTimer(); //秒钟执行一次 _timer->setInterval(1000); //使信号和槽函数联系起来执行 connect(_timer, SIGNAL(timeout()), this, SLOT(slotTimeout())); _timer->start(); //下面的方法让定时器只执行一次 //QTimer::singleShot(1000, this, SLOT(slotTimeout())); } /** * @brief MyWidget::slotTimeout 秒钟会执行下面的函数一次 */ void MyWidget::slotTimeout() { qDebug() << "Time out"; } //上面运行出的结果如下: void MyWidget::resizeEvent(QResizeEvent *) { // set the size of _view = MyWidget::size _view->setGeometry(QRect(QPoint(0, 0), size())); } void MyWidget::paintEvent(QPaintEvent *) { } /* * QPixmap 平台优化了的一种图,人看起来是一样的,但是在不同的平台,调用的不同平台的底层接口 * QImage 在所有的平台都是一样的,这里是一个位图 * QBitmap 灰度图 * QPicture 说白了就是一个轨迹图,通过这个轨迹图能够画图图像 */ void MyWidget::mousePressEvent(QMouseEvent *ev) { if(ev->button() == Qt::RightButton) { #if 0 //只需放开下面的代码即可执行,放开这里的时候,当右击鼠标的 //时候发现在相应的目录下出现了图片 // save the view 通过下面的一段代码实现了将窗口中的内容保存到图片上了 // 这里的size()是窗口的大小 QPixmap pixmap(size()); QPainter painter(&pixmap); painter, size().width(), size().height()), Qt::white); _view->render(&painter); pixmap.save("../bbb.png"); //这里运行的结果如下: #endif #if 0 //下面是打印预览的功能,放开此处的时候右击鼠标的时候会出现打印预览的功能 QPrintPreviewDialog dlg; connect(&dlg, SIGNAL(paintRequested(QPrinter*)), this, SLOT(slotPaintRequested(QPrinter*))); dlg.exec(); //这里的运行结果如下: #endif #if 0 //下面的打印的功能,放开此处会出现让选择打印机的窗口 QPrintDialog dlg; connect(&dlg, SIGNAL(accepted(QPrinter*)), this, SLOT(slotPaintRequested(QPrinter*))); dlg.exec(); //这里的运行结果如下: #endif } } void MyWidget::slotPaintRequested(QPrinter *printer) { QPainter painter(printer); _scene->render(&painter); //说明直接可以通过painter打印出文字 painter.drawText(QPoint(100, 100), "Fuck"); } int main(int argc, char* argv[]) { QApplication app (argc,argv); MyWidget w; w.showMaximized(); return app.exec(); } |
1.关于QT中的Graphics绘图,定时器,动画,将窗口中的内容打印到图片上,打印机,打印预览的更多相关文章
- Qt Creator中的3D绘图及动画教程(参照NeHe)
Qt Creator中的3D绘图及动画教程(参照NeHe) http://blog.csdn.net/cly116/article/details/47184729 刚刚学习了Qt Creator,发 ...
- vue开发中vue-resource + canvas 图片压缩、上传、预览
1.使用vue-resource上传,也可以自定义ajax上传: 2.使用<input type="file" @change="submit()" na ...
- github 上如何直接预览仓库中的html,搭建自己的主页
前言:最近在写vue+element ui 的一些demo,就在github上建了个仓库来管理,但是希望能直接在github上就能预览效果,所以才有了这篇文章.转载请注明出处:https://www. ...
- 解决vue-router中this.$router.push无法在新窗口中打开
解决vue-router中this.$router.push无法在新窗口中打开 let routeData = this.$router.resolve({ path: '/consult', que ...
- spyder中让生成的图像单独在窗口中显示
IPython 支持两种形式的绘图 终端输出图像新窗口输出图像方式 1 能够非常方便的保存输出记录(如将`IPython 终端输出转换成 Html 文件) 方式 2 则可以交互式的放大.拖动图片,并且 ...
- 在附件管理模块中增加对FTP 上传和预览的支持
在之前介绍的附件管理模块里面<Winform开发框架之通用附件管理模块>以及<Winform开发框架之附件管理应用>,介绍了附件的管理功能,通过对数据库记录的处理和文件的管理, ...
- vue中实现图片全屏缩放预览,支持移动端
# 安装 npm install vue-photo-preview --save # 引入 import preview from 'vue-photo-preview' import 'vue-p ...
- form表单系列中文件上传及预览
文件上传及预览 Form提交 Ajax 上传文件 时机: 如果发送的[文件]:->iframe, jQurey(),伪Ajax 预览 import os img_path = os.path.j ...
- OpenGL编程逐步深入(三)在窗口中显示一个三角形
这一节教程的内容会比较少,我们仅仅是对上一节教程中的代码进行扩展,在窗口中渲染一个三角形出来. 本节我们以下图所示正方形来讲解OpenGl中的坐标系统.当沿着Z轴负方向看时,可见顶点的坐标必须在这个正 ...
随机推荐
- 模板Link Cut Tree (动态树)
题目描述 给定N个点以及每个点的权值,要你处理接下来的M个操作.操作有4种.操作从0到3编号.点从1到N编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联 ...
- hihocoder 1388 fft循环矩阵
#1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal proce ...
- VS2012中C++,#include无法打开自己所写的头文件(.h)
最近刚开始学cocos2d-x,创建项目之后,自己按照<cocos2d-x 3.x 游戏开发>的教程写代码 先写了一个头文件 MyHelloWorldScene.h 然后在 AppDe ...
- IE6浏览器有哪些常见的bug,缺陷或者与标准不一致的地方,如何解决
IE6不支持min-height,解决办法使用css hack: .target { min-height: 100px; height: auto !important; height: 100px ...
- windows server 2008 R2 禁用ipv6和隧道适配器
在windows server 2008 R2操作系统下部署weblogic web application,部署完成后进行测试,发现测试页的地址使用的是隧道适配器的地址,而不是静态的ip地址,而且所 ...
- DELL、HP、IBM X86服务器命名规则
DELL.HP.IBM X86服务器命名规则 各大服务器厂家对于自己的服务器命名都有一定的规则,通常会根据服务器的外观(如塔式.机架式.刀片等).处理器(如Intel或者AMD等).架构等信息来命名. ...
- 解决 APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tas
报错信息:APPARENT DEADLOCK!!! Creating emergency threads for unassigned pending tasks! 在网上查了一下,大部分网友分析是c ...
- 软件测试assert
之前实习做过一段时间测试,现做个总结: 实习测试的是一款CM系统(case 系统),来记录IT部门处理的维修,服务,反馈,预定服务等case:b/s架构,人少小项目,实习时间短,去了已经快完工,主要测 ...
- 日常实用css布局技巧汇总
1.单行完整显示,多行省略显示. .box { width: 100px; //必要 display: -webkit-box; //必要 font-size: 14px; line-heig ...
- spring的事务配置方法
spring事务的配置有两种方式 1.xml配置的声明式事务配置 (1)配置数据源信息dataSource(使用阿里的数据源) <bean id="dataSource" c ...