设为首页 加入收藏

TOP

一个简易画板的实现(Graphics View)(一)
2012-11-13 13:23:21 】 浏览:2274
Tags:一个 简易 画板 实现 Graphics  View

    本文配套源码

    这一次将介绍如何使用Graphics View来实现前面所说的画板。前面说了很多有关Graphics View的好话,但是没有具体的实例很难说究竟好在哪里。现在我们就把前面的内容使用Graphics View重新实现一下,大家可以对比一下看有什么区别。

    同前面相似的内容就不再叙述了,我们从上次代码的基础上进行修改,以便符合我们的需要。首先来看MainWindow的代码:

    mainwindow.cpp

    #include "mainwindow.h"

    MainWindow::MainWindow(QWidget *parent)

    : QMainWindow(parent)

    {

    QToolBar *bar = this->addToolBar("Tools");

    QActionGroup *group = new QActionGroup(bar);

    QAction *drawLineAction = new QAction("Line", bar);

    drawLineAction->setIcon(QIcon(":/line.png"));

    drawLineAction->setToolTip(tr("Draw a line."));

    drawLineAction->setStatusTip(tr("Draw a line."));

    drawLineAction->setCheckable(true);

    drawLineAction->setChecked(true);

    group->addAction(drawLineAction);

    bar->addAction(drawLineAction);

    QAction *drawRectAction = new QAction("Rectangle", bar);

    drawRectAction->setIcon(QIcon(":/rect.png"));

    drawRectAction->setToolTip(tr("Draw a rectangle."));

    drawRectAction->setStatusTip(tr("Draw a rectangle."));

    drawRectAction->setCheckable(true);

    group->addAction(drawRectAction);

    bar->addAction(drawRectAction);

    QLabel *statusMsg = new QLabel;

    statusBar()->addWidget(statusMsg);

    PaintWidget *paintWidget = new PaintWidget(this);

    QGraphicsView *view = new QGraphicsView(paintWidget, this);

    setCentralWidget(view);

    connect(drawLineAction, SIGNAL(triggered()), this, SLOT(drawLineActionTriggered()));

    connect(drawRectAction, SIGNAL(triggered()), this, SLOT(drawRectActionTriggered()));

    connect(this, SIGNAL(changeCurrentShape(Shape::Code)), paintWidget, SLOT(setCurrentShape(Shape::Code)));

    }

    void MainWindow::drawLineActionTriggered()

    {

    emit changeCurrentShape(Shape::Line);

    }

    void MainWindow::drawRectActionTriggered()

    {

    emit changeCurrentShape(Shape::Rect);

    }

    由于mainwindow.h的代码与前文相同,这里就不再贴出。而cpp文件里面只有少数几行与前文不同。由于我们使用Graphics View,所以,我们必须把item添加到QGprahicsScene里面。这里,我们创建了scene的对象,而scene对象需要通过view进行观察,因此,我们需要再使用一个QGraphcisView对象,并且把这个view添加到MainWindow里面。

   

首页 上一页 1 2 3 4 5 6 下一页 尾页 1/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇JNA 调用 so 库 下一篇C/C++下使用SQLite轻量级数据库

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目