头文件MyWidget.h
#ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> #include<QGraphicsScene> //场景 #include<QGraphicsLineItem> //线的场景 #include<QGraphicsPixmapItem> //图像的场景 #include<QGraphicsView> //将场景表现出来 class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); QGraphicsScene *_scene; //data model QGraphicsView *_view; //show data model signals: public slots: }; #endif // MYWIDGET_H
#include "MyWidget.h" #include<QApplication> MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { _view = new QGraphicsView(this); //将场景在主窗口界面画出来 _view->setScene(_scene = new QGraphicsScene); //将场景设置在该窗口中 _scene->addItem(new QGraphicsLineItem(QLineF(QPointF(0, 0), QPointF(100, 100)))); //在创景窗口中画一条直线 } int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget w; w.show(); app.exec(); }
注意:此时画图的场景在主窗口的最左上方的位置,其大小刚好能花开图。
问题:怎样解决该问题使得图像在整个窗口进行展示呢?
源文件MyWidget.cpp
#include "MyWidget.h" #include<QApplication> #include<QVBoxLayout> MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { QVBoxLayout *vblayout = new QVBoxLayout(this); _view = new QGraphicsView(this); //将场景在主窗口界面画出来 vblayout->addWidget(_view); _view->setScene(_scene = new QGraphicsScene); //将场景设置在该窗口中 _scene->addItem(new QGraphicsLineItem(QLineF(QPointF(0, 0), QPointF(300, 300)))); //在创景窗口中画一条直线 } int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget w; w.show(); app.exec(); }运行结果:
缺点:放大之后,图像失真,没法判断该线的起始点位置,应该怎么解决呢?
#ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> #include<QGraphicsScene> //场景 #include<QGraphicsLineItem> //线的场景 #include<QGraphicsPixmapItem> //图像的场景 #include<QGraphicsView> //将场景表现出来 class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); QGraphicsScene *_scene; //data model QGraphicsView *_view; //show data model //通过检测窗口变大的虚函数 void resizeEvent(QResizeEvent *); signals: public slots: }; #endif // MYWIDGET_H
#include "MyWidget.h" #include<QApplication> MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { _view = new QGraphicsView(this); //将场景在主窗口界面画出来 _view->setScene(_scene = new QGraphicsScene); //将场景设置在该窗口中 _scene->addItem(new QGraphicsLineItem(QLineF(QPointF(0, 0), QPointF(300, 300)))); //在创景窗口中画一条直线 } void MyWidget::resizeEvent(QResizeEvent *) { //set the size of _view = MyWidget::size _view->setGeometry(QRect(QPoint(0, 0), size())); //最后的size()调用的是主窗口的大小 } int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget w; w.show(); app.exec(); }
MyWidget.h
#ifndef MYWIDGET_H #define MYWIDGET_H #include <QWidget> #include<QGraphicsScene> //场景 #include<QGraphicsLineItem> //线的场景 #include<QGraphicsPixmapItem> //图像的场景 #include<QGraphicsTextItem> //文本图像场景 #include<QGraphicsItemAnimation> //动画 #include<QTimeLine> #include<QMouseEvent> #include<QTimer> //创建定时器 //#include<QPrintPreviewDialog> //打印预览,为什么没有改头文件 //#include<QPrintDialog> //打印 #include<QGraphicsView> //将场景表现出来 class MyWidget : public QWidget { Q_OBJECT public: explicit MyWidget(QWidget *parent = 0); QGraphicsScene *_scene; //data model QGraphicsView *_view; //show data model //通过检测窗口变大的虚函数 void resizeEvent(QResizeEvent *); //鼠标点击保存图片 void mousePressEvent(QMouseEvent *); QTimer *_timer; signals: public slots: void slotTimeOut(); }; #endif // MYWIDGET_H
#include "MyWidget.h" #include<QApplication> #include<QPixmap> //平台相关的,不建议使用QImage,QBitmapp,QPicture #include<qDebug>; MyWidget::MyWidget(QWidget *parent) : QWidget(parent) { _view = new QGraphicsView(this); //将场景在主窗口界面画出来 _view->setBackgroundBrush(Qt::yellow);//设置背景颜色 _view->setScene(_scene = new QGraphicsScene); //将场景设置在该窗口中 QGraphicsLineItem *line; _scene->addItem(line = new QGraphicsLineItem(QLineF(QPointF(0, 0), QPointF(300, 300)))); //在创景窗口中画一条直线 line->setPen(QPen(Qt::DashDotDotLine)); QGraphicsTextItem *text; _scene->addItem(text = new QGraphicsTextItem("Hello, China!")); text->setPos(QPointF(20, 20)); //设置字体的位置 text->setFont(QFont("宋体", 50, 80, true)); //设置字体的属性 QGraphicsPixmapItem *_pixmapitem; _scene->addItem(_pixmapitem = new QGraphicsPixmapItem(QPixmap("../pic.jpg"))); _pixmapitem->setPos(QPointF(20, 150)); //将图动起来 QGraphicsItemAnimation *animation = new QGraphicsItemAnimation; animation->setItem(_pixmapitem); // _scene->addItem(animation); //怎样动需要QTimeLiness函数 QTimeLine *timeline = new QTimeLine(3000); //持续三秒钟 timeline->setLoopCount(3); //设置循环次数 animation->setTimeLine(timeline); // animation->rotationAt(.5); //以怎样的角度进行旋转 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()); } void MyWidget::slotTimeOut() { qDebug()<<"Slot time out"; //每隔一秒钟打印一次 sender()->deleteLater(); //一会就释放掉该定时,只打印一次 } void MyWidget::resizeEvent(QResizeEvent *) { //set the size of _view = MyWidget::size _view->setGeometry(QRect(QPoint(0, 0), size())); //最后的size()调用的是主窗口的大小 } void MyWidget::mousePressEvent(QMouseEvent *ev) { #if 0 //点击鼠标保存图片,所有的类都可以用这种方法进行保存 if(ev->button() == Qt::LeftButton) { QPixmap pixmap(size()); QPainter painter(&pixmap); //默认的背景是黑的,怎么设置成白色的呢? painter.fillRect(QRectF(0, 0, size().width(), size().height()), Qt::white); _scene->render(&painter); // _view->render(&painter); pixmap.save("../copy.jpg"); qDebug()<<"pressed!"; } #endif } int main(int argc, char *argv[]) { QApplication app(argc, argv); MyWidget w; w.showMaximized(); app.exec(); }