QT 绘制按钮 paintEvent enterEvent leaseEvent mouseEvent
案例2:绘制按钮
main.cpp
#include<QApplication>
#include “demoWidget.h”
int main(int args , int argv)
{
QApplication app(args , argv);
DemoWidget w;
w.resize(400,400);
w.setVisible(true);
return app.exec();
}
main.pro
TEMPLATE=app
SOURCES=main.cpp demoWidget.cppdemoPushButton.cpp
HEADERS=demoWidget.h demoPushButton.h
CONFIG=release qt
QT=core gui
TARGET=main
demoWidget.h
#ifndef DEMO2_WIDGET_H
#define DEMO2_WIDGET_H
#include<QWidget>
#include “demoPushButton.h”
class DemoWidget: public QWidget
{
public:
DemoWidget(QWidget * parent=NULL);
public:
DemoPushButton * btn;
};
#endif
demoWidget.cpp
#include “demoWidget.h”
#include “demoPushButton.h”
DemoWidget::DemoWidget(QWidget *parent):QWidget(parent)
{
btn = new DemoPushButton(this);
btn->resize(100,30);
btn->move(100,100);
}
绘制按钮:
demoPushButton.h
#include<QPushButton>
#include<QPaintEvent>
#include<QMouseEvent>
#ifndef DEMO2_PUSHBUTTON_H
#define DEMO2_PUSHBUTTON_H
class DemoPushButton : public QPushButton
{
public:
DemoPushButton(QWidget * parent=NULL);
protected:
virtual void paintEvent(QPaintEvent * e);
virtual void enterEvent(QEvent * e);
virtual void leaveEvent(QEvent * e);
virtual void mouseEvent(QMouseEvent*e);
private:
bool israised;
};
#endif
demoPushButton.cpp
#include “demoPushButton”
#include<QColor>
#include<QBrush>
#include<QPen>
#include<QPoint>
#include<QPainter>
DemoPushButton::DemoPushButton(QWidget*parent)
:QPushButton(parent),israised(true)
{
setMouseTracking(true); //跟踪鼠标移动事件
}
void DemoPushButton::paintEvent(QPaintEvent* e)
{
//绘制按钮的边界
int w=width(); //按钮宽度
int h=height(); //按钮高度
QColor clrw(255,255,255);
QColor clrb(0,0,0);
QBrush brw(clrw);
QBrush brb(clrb);
QPen penw(brw,2);
QPen penb(brb,2);
QPoint pttop(w/2,0);
QPoint ptbottom(w/2,h);
QPoint ptleft(0,h/2);
QPoint ptright(w,h/2);
QPainter g(this);
if(israised)
{
g.setPen(penw);
}
else
{
g.setPen(penb);
}
g.drawLine(pttop,ptleft);
g.drawLine(pttop,ptright);
if(israised)
{
g.setPen(penb);
}
else
{
g.setPen(penw);
}
g.drawLine(ptbottom,ptleft);
g.drawLine(ptbotton,ptrright);
}
void DemoPushButton::enterEvent(QEvent * e)
{
israised=false;
repaint(); //重新绘制按钮
}
void DemoPushButton::leaveEvent(QEvent * e)
{
israised=true;
repaint();
}
void DemoPushButton::mouseEvent(QMouseEvent * e)
{
float w=this->width();
float h=this->height()
int x=e->x();
int y=e->y();
float k=h/w; //斜率
if( y>-k*x+h/2 &&
y>=k*x-h/2 &&
y<=k*x+h/2 &&
y<=-k*x+3*h/2)
{
israise=false;
}
else
{
israise=true;
}
repaint();
}
QT 绘制按钮 paintEvent enterEvent leaseEvent mouseEvent的更多相关文章
- Qt 圆角按钮,面版自动布局
一.前言 在部分界面开发中,有时需要动态添加控件或按钮到面板中,在不需要时又需要删除该控件,故模仿视频开发中的设置屏蔽词,通过自己绘制的按钮与排布面板控件实现. 实现效果如下: 说明: 1.输入框可设 ...
- QT绘制饼图
QT版本:QT5.6.1 QT绘制饼图,出问题的代码如下 void DrawPieDialog::paintEvent(QPaintEvent *event) { float startAngle=0 ...
- Qt绘制文本一
QPainterPath,使用 drawText且设置字体,再使用painter.drawText方式 效果图: void WgtText::paintEvent(QPaintEvent *event ...
- Qt 学习之路 2(24):Qt 绘制系统简介
Qt 学习之路 2(24):Qt 绘制系统简介 豆子 2012年10月30日 Qt 学习之路 2 77条评论 Qt 的绘图系统允许使用相同的 API 在屏幕和其它打印设备上进行绘制.整个绘图系统基于Q ...
- Qt浮动按钮的实现(使用窗口背景透明、实现只显示浮动按钮的目的)
Qt浮动按钮的实现 效果如下: 图3 估计很多做Qt有一定经验的朋友会觉得这个效果不难,但是这是一个需求奇葩的实际业务中做出的效果.笔者会想讲下客户的需求和整体框架的矛盾. 整个项目主要是由Qt搭建的 ...
- 自绘实现半透明水晶按钮(继承CButton,设置BS_OWNERDRAW风格,覆盖DrawItem函数绘制按钮,把父窗口的背景复制到按钮上,实现视觉上的透明,最后通过AlphaBlend实现半透明)
运行效果 实现方法 1.给按钮加上BS_OWNERDRAW样式2.重载DrawItem函数,在这里绘制按钮3.关键之处就是把父窗口的背景复制到按钮上,实现视觉上的透明4.最后通过AlphaBlend实 ...
- QT绘制半透明窗体(改写paintEvent,超级简单)
在派生类中重载QDialog的void paintEvent(QPaintEvent *)事件,在这个函数中加入以下代码 QPainter painter(this); QLinearGradi ...
- Qt绘制异形窗体
异形窗体即不规则窗体,一般采用png图片,一般绘制异形窗体分两步: 1.设置遮罩区 2.绘制图片 使用png图片的透明部分作为遮罩区,然后绘制图片,这样我们就看到一个只绘制了非透明部分的图形,废话 ...
- Qt 学习之路 :Qt 绘制系统简介
Qt 的绘图系统允许使用相同的 API 在屏幕和其它打印设备上进行绘制.整个绘图系统基于QPainter,QPainterDevice和QPaintEngine三个类. QPainter用来执行绘制的 ...
随机推荐
- bzoj3407 [Usaco2009 Oct]Bessie's Weight Problem 贝茜的体重问题
Description 贝茜像她的诸多姊妹一样,因为从约翰的草地吃了太多美味的草而长出了太多的赘肉.所以约翰将她置于一个及其严格的节食计划之中.她每天不能吃多过H(5≤日≤45000)公斤的干 ...
- javadoc 生成帮助文档时,注意以下几点
参考:http://www.w3school.com.cn/tags/tag_pre.asp javadoc 生成帮助文档时,注意以下几点: 1.函数功能描述的结尾一定要有句号,英文句号或中文句号均可 ...
- poj 3411 Paid Roads(dfs)
Description A network of m roads connects N cities (numbered to N). There may be more than one road ...
- POJ3071:Football(概率DP)
Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, …, 2 ...
- ZendStudio快捷键 注释的快捷键
注:本文省略“通用快捷键”描述,诸如:ctrl+N=新建,ctrl+O=打开,ctrl+C=复制,ctrl+V,ctrl+X……等等几乎所有软件都通用的一组快捷键,而着重介绍zde独有的快捷键,了解并 ...
- UNIX下解压缩文件
用法示例以Sun Solaris (其他unix如linux.aix大体相同)为例=========================================================== ...
- AIX-vi操作-提示Unknown terminal type的问题解决方法
AIX-vi操作-提示Unknown terminal type的问题解决方法AIX Version 5.3$ vi /etc/profilelinux: Unknown terminal type[ ...
- ffmpeg的使用
-i "转换前的视频绝对路径\文件名.mp4" -movflags faststart+rtphint "转换后的视频绝对路径\文件名.mp4" 上面这条语句是 ...
- javascript this关键字
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- XMAL 中x名称控件的Auttribute
1 X:Class 作用告诉XAML编译器将XAML标签的编译结果与后台代码中指定的类合并,只能用于根节点,并且与之同名的类需要有Partial 例如窗口 2 X:ClassModifier 作用告诉 ...