一般说只在button中点击获得事件,作出相应的反应。而往往需要在QLabel上作出点击和触碰的效果。

我用qlabel做出了一个效果,当鼠标碰到label区域,label底下出现一条线,离开后线条消失。当点击label后变颜色

以下是我的代码

// label.h

#include<QLabel>

classlabel:publicQLabel
{
Q_OBJECT
public:
explicitlabel(QStringstr,QWidget*parent=);
virtualvoidmousePressEvent(QMouseEvent*event);
voidmouseReleaseEvent(QMouseEvent*event);
voidenterEvent(QEvent*);
voidleaveEvent(QEvent*);
intcount;
voidpaintEvent(QPaintEvent*event);
boolover;
boolpress;

signals:
//自定义clicked()信号,在mousePressEvent事件发生时触发
voidclicked();
publicslots:
voidchange_color();
};

//label.cpp

#include"label.h"
#include<QMouseEvent>
#include<QPainter>
#include<QPalette>

label::label(QStringstr,QWidget*parent):
QLabel(parent)
{

QPalettepalette;
palette.setColor(QPalette::WindowText,QColor(,,));

this->setText(str);
this->setPalette(palette);

setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
//this->setStyleSheet("background-color:blue");
this->setCursor(Qt::PointingHandCursor);

count=;
press=false;
over=false;
connect(this,SIGNAL(clicked()),this,SLOT(change_color()));
}

voidlabel::mousePressEvent(QMouseEvent*event)
{
//如果单击了就触发clicked信号
if(event->button()==Qt::LeftButton)
{
//触发clicked信号
count++;
press=true;
emitclicked();

}
//将该事件传给父类处理
QLabel::mousePressEvent(event);
}

voidlabel::mouseReleaseEvent(QMouseEvent*event)
{
press=false;
update();
}

voidlabel::enterEvent(QEvent*)
{
over=true;
update();
}

voidlabel::leaveEvent(QEvent*)
{
over=false;
update();
}

voidlabel::change_color()
{
if(count%)
this->setStyleSheet("background-color:red");
else
this->setStyleSheet("background-color:blue");
}

voidlabel::paintEvent(QPaintEvent*event)
{

QPainterpaint(this);
paint.setPen(QPen(Qt::yellow,));
if(over)
{

paint.drawLine(,this->height()-,this->width()-,this->height()-);

}
else
{
paint.setPen(Qt::NoPen);
}
QLabel::paintEvent(event);
}
//MainWindow.h
#include<QWidget>
#include"label.h"


classMainWindow:publicQWidget
{
Q_OBJECT

public:
label*la;
explicitMainWindow(QWidget*parent=);
~MainWindow();
};
//MainWindow.cpp
#include"mainwindow.h"
#include<QLabel>

MainWindow::MainWindow(QWidget*parent):
QWidget(parent)
{
setFixedSize(,);
la=newlabel("192.168.199.245",this);

la->setGeometry(,,,);

}

MainWindow::~MainWindow()
{

}
//main.cpp
#include"mainwindow.h"
#include<QApplication>

intmain(intargc,char*argv[])
{
QApplicationa(argc,argv);
MainWindoww;
w.show();

returna.exec();
}
仅供初学者参考

在QLabel上点击获得的效果的更多相关文章

  1. iOS UIButton加在window上点击无效果问题

    UIButton加在window上,点击没有效果,找了很久,原来是没有加上这名:[self.window makeKeyAndVisible]; self.window = [[UIWindow al ...

  2. UITableView或UIScrollVIew上的UIButton的高亮效果

    UITableView或UIScrollVIew上的UIButton的高亮效果 原文地址:http://www.jianshu.com/p/b4331f06bd34 最近做项目的时候发现,UIScro ...

  3. WIN7系统IIS上发布站点后水印效果失效的解决方法

    关于使用一般处理程序给图片添加水印的方法,请参考: 使用一般处理程序(IHttpHandler)制作图片水印 有些时候,给图片添加水印了,在本机运行也都正常,但是发布到IIS上后就没有水印效果了.本人 ...

  4. 超强的纯 CSS 鼠标点击拖拽效果

    背景 鼠标拖拽元素移动,算是一个稍微有点点复杂的交互. 而在本文,我们就将打破常规,向大家介绍一种超强的仅仅使用纯 CSS 就能够实现的鼠标点击拖拽效果. 在之前的这篇文章中 -- 不可思议的纯 CS ...

  5. EasyTouch绑定事件在电脑上点击有效Android上无效的解决方法

    最近做一个RPG类的游戏发现使用EasyTouch虚拟摇杆插件在电脑上点击有效Android上无效,查找资料发现是Easy Joystick中的一个属性interaction type要设置成 Dir ...

  6. Qt之添加QLabel的点击事件

    QLabel功能为显示了一个字符串或者图片等信息,它本身没有click信号.也就不能够响应click点击事件,有什么办法来实现来,我们可以子类化QLabel,实现MouseXXXEvent.class ...

  7. TypeWonder – 在任何网站上实时预览字体效果

    TypeWonder 让网页字体的选择过程变得轻松愉快.它可以帮助您在任何网站上快速测试 Web 字体效果!输入网站网址,就能够即时预览的字体的实际效果,还可以从数百种字体中进行挑选,您还可以得到所需 ...

  8. nodejs 微信中使用file组件上传图片在某些机型上点击无反应

    看下下面的代码: <form action="/" class="file_upload" method="post" enctype ...

  9. ios点击产生波纹效果

    ios点击产生波纹效果 by 伍雪颖 - (void)viewDidLoad { [super viewDidLoad]; RippleView = [[UIView alloc] initWithF ...

随机推荐

  1. lesson4:使用锁Lock来解决重复下单的问题

    demo源码:https://github.com/mantuliu/javaAdvance 中的类Lesson4IndependentLock 在上一节中,我们分析了Lock的源代码并一起实践了粗粒 ...

  2. Runtime.getRuntime().addShutdownHook(shutdownHook);

    今天在阅读Tomcat源码的时候,catalina这个类中使用了下边的代码,不是很了解,所以google了一下,然后测试下方法,Tomcat中的相关代码如下: Runtime.getRuntime() ...

  3. unity3d优化IOS

    1. using UnityEngine; class GarbageCollectManager : MonoBehaviour {       public int frameFreq = 30; ...

  4. 《C专家编程》之一

    一.解决函数返回指针的几种方法 1. 返回一个指向字符串常量的指针. 例子: char* func() { rturn "Only work for simple strings" ...

  5. linux开关机命令

    1.reboot重启 2.shutdown -r now 立即重启 root用户使用,与reboot命令相同 3.shutdown -r 10 过10分钟后重启root用户使用 4.shutdown ...

  6. no drawer view found with gravity RIGHT(Android实现侧滑菜单从右面滑出) 解决办法

    代码如下: <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width ...

  7. osgi与webservice

    osgi简介: http://osgia.com/ http://blog.csdn.net/xiaokui008/article/details/9662933 http://wdhdd889.it ...

  8. 编程语言大牛王垠:编程的智慧,带你少走弯路 [本文转载CocoaChina]

    作者:王垠 授权本站转载. 编程是一件创造性的工作,是一门艺术.精通任何一门艺术,都需要很多的练习和领悟,所以这里提出的“智慧”,并不是号称三天瘦二十斤的减肥药,它并不能代替你自己的勤奋.然而我希望它 ...

  9. StrokeStart与StrokeEnd动画

    通过修改CAShapeLayer的StrokeStart与StrokeEnd的值来实现画图动画 效果图: 代码部分: #import "ViewController.h" @int ...

  10. 武汉科技大学ACM:1002: 华科版C语言程序设计教程(第二版)例题6.6

    Problem Description 明天就要英语考试了,小明明正在挑灯夜 战背单词.小明明发现单词很难背,背一个忘一个.经过仔细研究,小明明发现单词难背的原因是因为某个字符的出现,破坏了整个单词的 ...