第一次发QT的博文,本人对QT接触没多久,还在入门水平,大牛勿喷哈,之前因为C#  (.net framework)做出来的绘制矩形的程序闪的太厉害了,现在用QT重做一个

先上效果图


代码贴全了

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPalette>
#include<Qstring>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //设置背景色为白色
    QPalette palette(this->palette());
    palette.setColor(QPalette::Background, Qt::white);
    this->setPalette(palette);
    //初始化变量
    this->mousedown=false;
    this->release=false;
    ;
    ;
    //初始化画布
    pix=new QPixmap(w,h);
    pix->fill(Qt::white);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{

    QPainter paintpix(pix);
    QPainter paintform(this);
    if(this->mousedown)//鼠标移动时触发
    {
        paintform.drawPixmap(,,w,h,*pix);
        QRect* rect=new QRect(this->startpoint,this->endpoint);
        paintform.drawRect (*rect);

    }
    else if(!this->mousedown&&this->release)//鼠标释放时触发
    {
        if(this->startpoint.x()==this->endpoint.x()&&this->endpoint.y()==this->startpoint.y())//两点重合时什么也不画
        {
             paintform.drawPixmap(,,w,h,*pix);
            return;
        }

        QRect* rect=new QRect(this->startpoint,this->endpoint);
        paintpix.drawRect (*rect);
         paintform.drawPixmap(,,w,h,*pix);
        this->release=false;
    }
    else
    {
        paintform.drawPixmap(,,w,h,*pix);
    }

}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(this->mousedown)
    {
        this->endpoint=event->pos();
        update();
    }

}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    startpoint=event->pos();
    this->mousedown=true;

}

void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    this->endpoint=event->pos();
    this->mousedown=false;
    this->release=true;
    update();
}

void MainWindow::repaint()//重画时
{
    QPainter paintform(this);
    paintform.drawPixmap(,,w,h,*pix);
}

示例代码链接::http://pan.baidu.com/s/1eQ2TaJw

这个是C#版本的 http://www.cnblogs.com/magicianlyx/p/4979644.html

QT画矩形的更多相关文章

  1. QT第六天学习

    基本事件: 鼠标事件 键盘事件 绘制事件 1.QT中的事件: 事件是对各应用程序需要知道的由应用程序内部或外部产生的事情或动作的通称. QT中事件的处理: 在QT中使用一个对象来表示一个事件,继承自Q ...

  2. 界面编程之QT绘图和绘图设备20180728

    /*******************************************************************************************/ 一.绘图 整 ...

  3. QT学习笔记3:QT中语法说明

    一.Qt 类中Q_OBJECT的作用 QObject 是所有Qt对象的基类. QObject 是Qt模块的核心.它的最主要特征是关于对象间无缝通信的机制:信号与槽.使用connect()建立信号到槽的 ...

  4. Qt绘图

    Qt绘图的设置 QPainter::Antialiasing // 反锯齿 QPainter::TextAntialiasing // 文字反锯齿 QPainter::SmoothPixmapTran ...

  5. Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)

    QPainter绘图 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状态改变 绘图设备(QPixmap,QImage,QBitmap,QPict ...

  6. 32.QT绘图

    widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QPainter> #inclu ...

  7. Qt如何绘制(简单深刻,fearlazy的系列文章)

    我觉得绘制只需要考虑几个问题.简单地说就是谁在什么地方画什么? 在Qt中可以这么回答:QPainter在QPainterDevice中画想画的东西. 举个例子:用QtCreator向导新建一个项目,基 ...

  8. Qt 绘图(QBitmap,QPixmap,QImage,QPicture)

    QPainter绘图绘图设备(QPixmap,QImage,QBitmap,QPicture) 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状 ...

  9. QT内省机制、自定义Model、数据库

    本文将介绍自定义Model过程中数据库数据源的获取方法,我使用过以下三种方式获取数据库数据源: 创建 存储对应数据库所有字段的 结构体,将结构体置于容器中返回,然后根据索引值(QModelIndex) ...

随机推荐

  1. PowerBuilder -- 键盘对应的枚举值

    KeyCode values for keyboard keys Type of key KeyCode values and descriptions Mouse buttons KeyLeftBu ...

  2. 开源大数据引擎:Greenplum 数据库架构分析

    Greenplum 数据库是最先进的分布式开源数据库技术,主要用来处理大规模的数据分析任务,包括数据仓库.商务智能(OLAP)和数据挖掘等.自2015年10月正式开源以来,受到国内外业内人士的广泛关注 ...

  3. npm install --save 、--save-dev 、-D、-S 的区别与NODE_ENV的配置

    https://blog.csdn.net/jwl_willon/article/details/81054978 1.npm install <=> npm i --save   < ...

  4. 再看python多线程------threading模块

    现在把关于多线程的能想到的需要注意的点记录一下: 关于threading模块: 1.关于 传参问题 如果调用的子线程函数需要传参,要在参数后面加一个“,”否则会抛参数异常的错误. 如下: for i ...

  5. 【BZOJ4843】[Neerc2016]Expect to Wait 排序

    [BZOJ4843][Neerc2016]Expect to Wait Description ls最近开了一家图书馆,大家听说是ls开的,纷纷过来借书,自然就会出现供不应求的情况, 并且借书的过程类 ...

  6. ZOJ 3502 Contest <状态压缩 概率 DP>

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3502 #include <iostream> #incl ...

  7. MarkdownPad - The Markdown Editor for Windows http://markdownpad.com/

    MarkdownPad - The Markdown Editor for Windows http://markdownpad.com/

  8. cocos2d-js v3.1的坑

    前几天因为要用到cc.pool,所以就换了v3.1版本,结果连生成apk的出错(cocos code ide), log显示为:error: relocation overflow in R_ARM_ ...

  9. Java for LeetCode 099 Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  10. PAT天梯赛 L2-002. 链表去重 【STL】

    题目链接 https://www.patest.cn/contests/gplt/L2-002 思路 用结构体 存储 一个结点的地址 值 和下一个地址 然后从首地址开始 往下走 并且每个值的绝对值 都 ...