Qt如何用Label实现自定义按钮

HL_LOVE_C 2014-08-21 06:16:55
如何用QLabel实现自定义的Qt按钮?

实现自定义按钮(包括图片和文字,例如:图片所示。),并且点击按钮,弹出对话框!求完整代码?

急急急!!!本人是新手,求完整代码逃命,完整回答者追加给高分!本人开发环境是VS2010插件QT。
...全文
815 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
IceQusz 2017-03-18
  • 打赏
  • 举报
回复
版主开挂了
无想无念 2016-10-13
  • 打赏
  • 举报
回复
14年的帖子,为什么现在还没有结贴?版主呢?
HL_LOVE_C 2014-08-25
  • 打赏
  • 举报
回复
引用 17 楼 HL_LOVE_C 的回复:
本人开发环境是VS2010插件Qt.(我是用vs2010建的项目)
你说的那一行代码main文件中本来就有,不用加,我用qt creator创建项目,没有Qt Gui应用这一项,所以我只能创建的是Qt Widgets Application这一项,构建之后也报错了,错误有4个全部是main.cpp的问题。
HL_LOVE_C 2014-08-25
  • 打赏
  • 举报
回复
本人开发环境是VS2010插件Qt.(我是用vs2010建的项目)
WeiXiao_365 2014-08-25
  • 打赏
  • 举报
回复
你这个貌似是用Qt生成的项目不对吧,没有直接引入那些头文件,这样生成一个项目比较好:

WeiXiao_365 2014-08-25
  • 打赏
  • 举报
回复
引用 14 楼 HL_LOVE_C 的回复:
[quote=引用 13 楼 WeiXiao_365 的回复:] [quote=引用 12 楼 HL_LOVE_C 的回复:] [quote=引用 11 楼 WeiXiao_365 的回复:] [quote=引用 9 楼 HL_LOVE_C 的回复:] [quote=引用 8 楼 WeiXiao_365 的回复:] [quote=引用 7 楼 HL_LOVE_C 的回复:] [quote=引用 1 楼 WeiXiao_365 的回复:] #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; }
实现文件呢大哥?[/quote] 这个还要实现文件啊,。。,这样吧你加我QQ1179678986,我远程帮你调一下吧[/quote] 可是我上网包括登QQ就是远程,但是代码运行环境在本地,本地的不能上网,怎么远程调?不如你教我一下吧,我自己调,谢谢你了![/quote] 这个 我想想啊,这样把你加我qq,我把源文件发给你。你直接添加就应该能用了[/quote] 好滴,谢谢啦,。哦对了,现在我没下班,不能登QQ,你可以发我邮箱吗先?1042064831@qq.com[/quote] 可以[/quote] 大哥啊,编译不过啊,编译到main.cpp时还是显示报错 报错如下: 1> main.cpp(2):fatal error c1083:无法找到包括文件:“QWidget/QApplication”:no such file or directory[/quote] 在main.cpp中导入头文件: 就是在最开始的地方加入这行代码: #include <QApplication>
HL_LOVE_C 2014-08-25
  • 打赏
  • 举报
回复
引用 13 楼 WeiXiao_365 的回复:
[quote=引用 12 楼 HL_LOVE_C 的回复:] [quote=引用 11 楼 WeiXiao_365 的回复:] [quote=引用 9 楼 HL_LOVE_C 的回复:] [quote=引用 8 楼 WeiXiao_365 的回复:] [quote=引用 7 楼 HL_LOVE_C 的回复:] [quote=引用 1 楼 WeiXiao_365 的回复:] #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; }
实现文件呢大哥?[/quote] 这个还要实现文件啊,。。,这样吧你加我QQ1179678986,我远程帮你调一下吧[/quote] 可是我上网包括登QQ就是远程,但是代码运行环境在本地,本地的不能上网,怎么远程调?不如你教我一下吧,我自己调,谢谢你了![/quote] 这个 我想想啊,这样把你加我qq,我把源文件发给你。你直接添加就应该能用了[/quote] 好滴,谢谢啦,。哦对了,现在我没下班,不能登QQ,你可以发我邮箱吗先?1042064831@qq.com[/quote] 可以[/quote] 大哥啊,编译不过啊,编译到main.cpp时还是显示报错 报错如下: 1> main.cpp(2):fatal error c1083:无法找到包括文件:“QWidget/QApplication”:no such file or directory
小K小Q 2014-08-22
  • 打赏
  • 举报
回复
大的方向是提升为控件。具体代码,要有时间才能写给你。
HL_LOVE_C 2014-08-22
  • 打赏
  • 举报
回复
不行啊,老是报错 1> main.cpp(2):fatal error c1083:无法找到包括文件:“QWidget/QApplication”:no such file or directory 1> widget.cpp(2):fatal error c1083:无法找到包括文件:“ui_widget.h”:no such file or directory
WeiXiao_365 2014-08-22
  • 打赏
  • 举报
回复
引用 12 楼 HL_LOVE_C 的回复:
[quote=引用 11 楼 WeiXiao_365 的回复:] [quote=引用 9 楼 HL_LOVE_C 的回复:] [quote=引用 8 楼 WeiXiao_365 的回复:] [quote=引用 7 楼 HL_LOVE_C 的回复:] [quote=引用 1 楼 WeiXiao_365 的回复:] #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; }
实现文件呢大哥?[/quote] 这个还要实现文件啊,。。,这样吧你加我QQ1179678986,我远程帮你调一下吧[/quote] 可是我上网包括登QQ就是远程,但是代码运行环境在本地,本地的不能上网,怎么远程调?不如你教我一下吧,我自己调,谢谢你了![/quote] 这个 我想想啊,这样把你加我qq,我把源文件发给你。你直接添加就应该能用了[/quote] 好滴,谢谢啦,。哦对了,现在我没下班,不能登QQ,你可以发我邮箱吗先?1042064831@qq.com[/quote] 可以
HL_LOVE_C 2014-08-22
  • 打赏
  • 举报
回复
引用 11 楼 WeiXiao_365 的回复:
[quote=引用 9 楼 HL_LOVE_C 的回复:] [quote=引用 8 楼 WeiXiao_365 的回复:] [quote=引用 7 楼 HL_LOVE_C 的回复:] [quote=引用 1 楼 WeiXiao_365 的回复:] #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; }
实现文件呢大哥?[/quote] 这个还要实现文件啊,。。,这样吧你加我QQ1179678986,我远程帮你调一下吧[/quote] 可是我上网包括登QQ就是远程,但是代码运行环境在本地,本地的不能上网,怎么远程调?不如你教我一下吧,我自己调,谢谢你了![/quote] 这个 我想想啊,这样把你加我qq,我把源文件发给你。你直接添加就应该能用了[/quote] 好滴,谢谢啦,。哦对了,现在我没下班,不能登QQ,你可以发我邮箱吗先?1042064831@qq.com
WeiXiao_365 2014-08-22
  • 打赏
  • 举报
回复
引用 9 楼 HL_LOVE_C 的回复:
[quote=引用 8 楼 WeiXiao_365 的回复:] [quote=引用 7 楼 HL_LOVE_C 的回复:] [quote=引用 1 楼 WeiXiao_365 的回复:] #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; }
实现文件呢大哥?[/quote] 这个还要实现文件啊,。。,这样吧你加我QQ1179678986,我远程帮你调一下吧[/quote] 可是我上网包括登QQ就是远程,但是代码运行环境在本地,本地的不能上网,怎么远程调?不如你教我一下吧,我自己调,谢谢你了![/quote] 这个 我想想啊,这样把你加我qq,我把源文件发给你。你直接添加就应该能用了
HL_LOVE_C 2014-08-22
  • 打赏
  • 举报
回复
这个代码会报 1> main.cpp(2):fatal error c1083:无法找到包括文件:“QWidget/QApplication”:no such file or directory 1> widget.cpp(2):fatal error c1083:无法找到包括文件:“ui_widget.h”:no such file or directory 这个该怎么调?
HL_LOVE_C 2014-08-22
  • 打赏
  • 举报
回复
引用 8 楼 WeiXiao_365 的回复:
[quote=引用 7 楼 HL_LOVE_C 的回复:] [quote=引用 1 楼 WeiXiao_365 的回复:] #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; }
实现文件呢大哥?[/quote] 这个还要实现文件啊,。。,这样吧你加我QQ1179678986,我远程帮你调一下吧[/quote] 可是我上网包括登QQ就是远程,但是代码运行环境在本地,本地的不能上网,怎么远程调?不如你教我一下吧,我自己调,谢谢你了!
WeiXiao_365 2014-08-22
  • 打赏
  • 举报
回复
引用 7 楼 HL_LOVE_C 的回复:
[quote=引用 1 楼 WeiXiao_365 的回复:] #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; }
实现文件呢大哥?[/quote] 这个还要实现文件啊,。。,这样吧你加我QQ1179678986,我远程帮你调一下吧
HL_LOVE_C 2014-08-22
  • 打赏
  • 举报
回复
引用 1 楼 WeiXiao_365 的回复:
#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; }
实现文件呢大哥?
HL_LOVE_C 2014-08-22
  • 打赏
  • 举报
回复
求代码啊!!!
HL_LOVE_C 2014-08-21
  • 打赏
  • 举报
回复
1> main.cpp 1>main.cpp(2): fatal error C1083: 无法打开包括文件:“QtWidgets/QApplication”: No such file or directory 1> 正在生成代码... 1> 1>生成失败。 1>
WeiXiao_365 2014-08-21
  • 打赏
  • 举报
回复
#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; }

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧