Qt怎么样用Label实现自定义按钮

C++语言 码拜 7年前 (2017-04-21) 2868次浏览
怎么样用QLabel实现自定义的Qt按钮?
实现自定义按钮(包括图片和文字,例如:图片所示。),并且点击按钮,弹出对话框!求完整代码?
急急急!本人是新手,求完整代码逃命,完整回答者追加给高分!本人开发环境是VS2010插件QT。
解决方案

100

引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:
Quote: 引用:

#ifndef GCLICKLABEL_H
#define GCLICKLABEL_H
#include <QLabel>
#include <QMouseEvent>
#include <QDialog>
class GClickLabel: public QLabel
{
Q_OBJECT
public:
explicit GClickLabel(QWidget *parent = 0);
void setEnterNeedUnderLine(bool need);
void setTextColor(const QColor &color);
void setTextHoverColor(const QColor &color);
void setHoverColor(const QColor &color);
void setText(const QString &text);
void setImage(const QString imagePath);
signals:
void clicked();
void enterLabel();
void leaveLabel();
protected:
void mouseReleaseEvent(QMouseEvent * event);
void mousePressEvent(QMouseEvent *ev);
void enterEvent(QEvent *);
void leaveEvent(QEvent *);
private:
bool m_needUnderLine;
QColor m_color;
QColor m_hoverColor;
QColor m_widgetHoverColor;
QDialog *m_dialog;
};
#endif // GCLICKLABEL_H
源文件:
#include “GClickLabel.h”
GClickLabel::GClickLabel(QWidget *parent) :
QLabel(parent), m_needUnderLine(false), m_widgetHoverColor(“”)
{
setCursor(Qt::PointingHandCursor);
m_dialog = new QDialog(this);
}
void GClickLabel::setEnterNeedUnderLine(bool need)
{
m_needUnderLine = need;
}
void GClickLabel::setTextColor(const QColor &color)
{
m_color = color;
m_hoverColor = color;
QString style = QString(“GClickLabel {color: %1;}”).arg(color.name());
setStyleSheet(style);
}
void GClickLabel::setTextHoverColor(const QColor &color)
{
m_hoverColor = color;
}
void GClickLabel::setText(const QString &text)
{
this->setProperty(“Text4GTF”, text);
QLabel::setText(text);
}
void GClickLabel::setImage(const QString imagePath)
{
QPixmap pixmap(imagePath);
this->setPixmap(pixmap);
}
void GClickLabel::mouseReleaseEvent(QMouseEvent *event)
{
if(!rect().contains(event->pos()))
return;
if(event->button() == Qt::LeftButton)
{
m_dialog->exec();
emit clicked();
}
}
void GClickLabel::mousePressEvent(QMouseEvent *ev)
{
Q_UNUSED(ev);
// 不在Press中发送信号,否则弹出Dialog时,Dialog不激活
//    if(!rect().contains(ev->pos()))
//        return;
//    if(ev->button() == Qt::LeftButton)
//        emit clicked();
}
void GClickLabel::enterEvent(QEvent *)
{
if(!text().isEmpty())
{
QString style;
if (m_widgetHoverColor.isValid())
{
style = QString(“GClickLabel {color: %1;background-color: %2}”).arg(m_hoverColor.name(), m_widgetHoverColor.name());
}
else
{
style = QString(“GClickLabel {color: %1}”).arg(m_hoverColor.name());
}
setStyleSheet(style);
}
QFont font = this->font();
font.setUnderline(m_needUnderLine);
this->setFont(font);
emit enterLabel();
}
void GClickLabel::leaveEvent(QEvent *)
{
if(!text().isEmpty())
{
QString style = QString(“GClickLabel {color: %1;}”).arg(m_color.name());
setStyleSheet(style);
}
QFont font = this->font();
font.setUnderline(false);
this->setFont(font);
emit leaveLabel();
}
void GClickLabel::setHoverColor( const QColor &color )
{
m_widgetHoverColor = color;
}

实现文件呢大哥?

这个还要实现文件啊,。,这样吧你加本人QQ1179678986,本人远程帮你调一下吧Qt怎么样用Label实现自定义按钮

可是本人上网包括登QQ就是远程,但是代码运行环境在本地,本地的不能上网,怎么远程调?不如你教本人一下吧,本人本人调,谢谢你了!

这个Qt怎么样用Label实现自定义按钮
本人想想啊,这样把你加本人qq,本人把源文件发给你。你直接添加就应该能用了Qt怎么样用Label实现自定义按钮

好滴,谢谢啦,。哦对了,现在本人没下班,不能登QQ,你可以发本人邮箱吗先?1042064831@qq.com

可以

大哥啊,编译不过啊,编译到main.cpp时还是显示报错
报错如下:
1> main.cpp(2):fatal error  c1083:无法找到包括文件:“QWidget/QApplication”:no such file or directory

在main.cpp中导入头文件:
就是在最开始的地方加入这行代码:
#include <QApplication>


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Qt怎么样用Label实现自定义按钮
喜欢 (0)
[1034331897@qq.com]
分享 (0)