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用来执行绘制的 ...
随机推荐
- bzoj1644 [Usaco2007 Oct]Obstacle Course 障碍训练课
Description 考虑一个 N x N (1 <= N <= 100)的有1个个方格组成的正方形牧场.有些方格是奶牛们不能踏上的,它们被标记为了'x'.例如下图: . . B x . ...
- Word Ladder II 解答
Question Given two words (beginWord and endWord), and a dictionary's word list, find all shortest tr ...
- poj 2377 Bad Cowtractors(最大生成树!)
Description Bessie has been hired to build a cheap internet network among Farmer John's N (2 <= N ...
- eclipse中误删了servers文件
Eclipse中误删了servers文件,需要重新添加tomcat服务器,这时就会遇到在New Server对话框中选择了Tomcat 6/7后却无法单击"Next"按钮的问题,如 ...
- 改变UITableViewCell按下去的颜色
UIView *bgColorView = [[UIView alloc] init]; [bgColorView setBackgroundColor:[UIColor redColor]]; [c ...
- [Java Web]Struts2解决中文乱码问题
1.设置struts的字符编码,能够在struts.xml中添加下面代码: <constant name="struts.i18n.encoding" value=" ...
- 更新-----Scripts:执行双网卡绑定
#!/bin/bash #------------------------------------------------------------------------------- # Name: ...
- c-version:null]] could not deserialize the servlet-context scoped attribute with name: "MENU_LIST"
<Jul 26, 2013 10:45:02 AM CST> <Error> <HTTP> <BEA-101362> <[ServletConte ...
- 大数值基础、for与while循环的简单对比
- hbase性能调优之压缩测试
文章概述: 1.顺序写 2.顺序读 3.随机写 4.随机读 5.SCAN数据 0 性能测试工具 hbase org.apache.hadoop.hbase.PerformanceEvaluation ...