运行结果(GIF动图):


widget.cpp

附代码:

xiaowen_QT_day25.pro

QT       += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0SOURCES += \main.cpp \widget.cppHEADERS += \widget.h# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void paintEvent(QPaintEvent *);
public slots:void setBlackSlot();void updateBlueSlot();void repaintYellowSlot();
private:QColor _bgColor;
};
#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include<QPainter>
#include<QDialog>
#include<QPushButton>
#include<QHBoxLayout>
Widget::Widget(QWidget *parent): QWidget(parent),_bgColor(Qt::red)
{/*布局控制窗口*/QDialog* ctrlDialog=new QDialog(this);QHBoxLayout* hBox=new QHBoxLayout(ctrlDialog);QPushButton* pb=new QPushButton("setBlackSlot");hBox->addWidget(pb);QPushButton* pb1=new QPushButton("updateblue");hBox->addWidget(pb1);QPushButton* pb2=new QPushButton("repaintYellow");hBox->addWidget(pb2);connect(pb,SIGNAL(clicked()),this,SLOT(setBlackSlot()));connect(pb1,SIGNAL(clicked()),this,SLOT(updateBlueSlot()));connect(pb2,SIGNAL(clicked()),this,SLOT(repaintYellowSlot()));ctrlDialog->show();}
void Widget::setBlackSlot()
{_bgColor=Qt::black;this->update();
}
void Widget::updateBlueSlot()
{_bgColor=Qt::blue;this->update();//调用update的时候,并不出马上触发painrEvent事件,//而是把绘画事件丢到消息循环中,等待消息循环调度,会多次update合并成一个。
}
void Widget::repaintYellowSlot()
{_bgColor=Qt::yellow;//this->update();this->repaint(0,0,150,100);//调用repaint的时候,马上触发paintEvent事件
}void Widget::paintEvent(QPaintEvent *)
{QPainter painter;painter.begin(this);painter.setBrush(_bgColor);painter.drawRect(this->rect());painter.end();
}Widget::~Widget()
{}

main.cpp

#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

QT每日一练day25:触发绘画事件相关推荐

  1. QT每日一练day24:绘画事件

    一.第一阶段 设置线条粗细 二.第二阶段:设置笔的样式 如:虚线 三.第三阶段:绘制矩形 四.第四阶段:设置颜色 轮廓颜色 笔刷颜色 笔刷样式 五.第五阶段:绘制其他形状图像和文字 六.第六阶段:图像 ...

  2. QT每日一练day22:键盘事件

    一.第一阶段--键盘按键按下事件 运行结果(GIF动图): xiaowen_QT_day22.pro QT += core guigreaterThan(QT_MAJOR_VERSION, 4): Q ...

  3. QT每日一练day21:鼠标事件

    一.第一阶段--鼠标点击和释放事件 运行结果(GIF动图): MouseEvent.pro #------------------------------------------------- # # ...

  4. QT每日一练day4:ubuntu中使用QT

    (本文主要是为了说明QT的跨平台特性)   一.安装QT sudo apt-get install qt5-default qtcreator cmake 二.打开QT 可以点击图标或命令行方式: 三 ...

  5. QT每日一练day1:第一个程序

    参考博文:Qt常见类.窗口类继承关系树图 QT每日一练(1):第一个程序 最终效果(GIF动图): step1: step2: step3: step4: step5: step6: step7: s ...

  6. QT每日一练day23:鼠标进入与离开事件

    运行结果(GIF动图): xiaowen_QT_day23.pro QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFI ...

  7. QT每日一练day20:事件处理机制

    运行结果: day20.pro QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += c++11# The fo ...

  8. QT每日一练day12:QDailog

    一.第一阶段 运行结果(GIF动图): day12.pro QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgetsCONFIG += ...

  9. QT每日一练day29:QT中的多线程探究

    一.未使用多线程,则w0先运行完后,w1才开始运行 运行结果(GIF动图): main.cpp xiaowen_QT_day29.pro QT+=widgets SOURCES += \main.cp ...

最新文章

  1. 调试技巧之 找准调试点
  2. tomcat下类加载顺序
  3. sierra mysql_macOS High Sierra 使用 Homebrew 安装 MYSQL 5.7
  4. mysql 数据库 额外_Manager额外参数怎么学?mysql数据库学习
  5. C# 使用 Index 和 Range 简化集合操作
  6. Log4j的应用实例
  7. 关于ioremap,request_mem_region
  8. PubMedQA生物医学研究问题解答数据集(2019)下载
  9. 组了个视频号的局,汇报下数据!
  10. ITIL 4和DevOps的关系?
  11. javascript服务端编程
  12. AOSP ~ Camera - YUV格式简介
  13. 百世赴美IPO拟募7.5亿美元,“另类”大佬周韶宁迎来新冒险
  14. 银河帝国----基地与地球
  15. excel表格打印每页都有表头_【Excel】打印超长表格,怎么才能每页都显示表头?...
  16. 动态代理(JDK动态代理和CGLIB代理)
  17. 【​观察】六脉神剑第四式-全面保障之移形换影
  18. 列表类型(sort,reverse,list(),append(),切片)、求中位数,平均数,标准差练习
  19. 今天推荐一下网友张迪的博客
  20. php线下支付,Paypal线下支付模块,附下载地址_PHP教程

热门文章

  1. Spring DI(依赖注入)Xml篇
  2. SSM实现的在线挂号预约管理系统源码
  3. 基于JAVA+SpringBoot+Mybatis+MYSQL的仿天猫商城
  4. 基于JAVA+SpringBoot+Mybatis+MYSQL的美食分享网站
  5. 基于JAVA+SpringMVC+Mybatis+MYSQL的博客系统
  6. 基于JAVA+SpringMVC+Mybatis+MYSQL的保险业务管理系统
  7. 基于JAVA+SpringBoot+Mybatis+MYSQL的疫情信息管理系统
  8. 换服务器要重新百度站长验证站点吗6,六个步骤搞定更换网站服务器
  9. OpenStack 云计算基础知识
  10. ECSHOP邮件验证后送积分