Qt PushButton在hover时换图

daxuezdj7 2016-11-22 11:58:11


现在要实现一个按钮中,左边是一个小图标,右边是文字。
在鼠标移到按钮上时,换另外一个图标。

小图标是使用icon加进去的。

请问各位怎么有什么解决方案吗
...全文
2351 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Little柯南 2017-11-16
  • 打赏
  • 举报
回复
来给我邮箱,帮你搞个demo
Little柯南 2017-11-15
  • 打赏
  • 举报
回复
ico与png你可以用转换工具转变啊
Zeroonezeroone 2017-11-14
  • 打赏
  • 举报
回复
引用 5 楼 u010111033 的回复:
QPushButton:hover{ /*鼠标悬停在按钮上时*/ image: url(:/images/loadin.png); }
这种不管用啊,你自己实现了吗?
daxuezdj7 2016-12-12
  • 打赏
  • 举报
回复
引用 10 楼 Inhibitory 的回复:
QSS 不支持按钮的 hover 切换图片,需要处理鼠标事件实现
bool FramelessWindowCentralWidget::eventFilter(QObject *watched, QEvent *e) {
    // 鼠标进入或离开关闭按钮时设置为不同状态的图标
    if (watched == ui->closeButton) {
        if (e->type() == QEvent::Enter) {
            ui->closeButton->setIcon(QIcon(":/icon/close-hover.png"));
        } else if (e->type() == QEvent::Leave) {
            ui->closeButton->setIcon(QIcon(":/icon/close.png"));
        }
    }

    return QWidget::eventFilter(watched, e);
}
感谢10楼,试了可用。要加一句。 button->installEventFilter(this);
DreamLife. 2016-11-29
  • 打赏
  • 举报
回复
引用 10 楼 Inhibitory 的回复:
QSS 不支持按钮的 hover 切换图片,需要处理鼠标事件实现
bool FramelessWindowCentralWidget::eventFilter(QObject *watched, QEvent *e) {
    // 鼠标进入或离开关闭按钮时设置为不同状态的图标
    if (watched == ui->closeButton) {
        if (e->type() == QEvent::Enter) {
            ui->closeButton->setIcon(QIcon(":/icon/close-hover.png"));
        } else if (e->type() == QEvent::Leave) {
            ui->closeButton->setIcon(QIcon(":/icon/close.png"));
        }
    }

    return QWidget::eventFilter(watched, e);
}
这个方法好,收藏了了,不过这个方法是不是需要给每个button都注册一个鼠标事件了
allen_00 2016-11-28
  • 打赏
  • 举报
回复
qt5.3以后 QSS 完全支持 前面版本没去试过 [code=css ]QPushButton { border:none; background-image: url(:xxx.png); background-repeat:no-repeat; background-position: center; } QPushButton:hover { background-image:url(:xxx.png); }[/code]
Inhibitory 2016-11-24
  • 打赏
  • 举报
回复
QSS 不支持按钮的 hover 切换图片,需要处理鼠标事件实现
bool FramelessWindowCentralWidget::eventFilter(QObject *watched, QEvent *e) {
    // 鼠标进入或离开关闭按钮时设置为不同状态的图标
    if (watched == ui->closeButton) {
        if (e->type() == QEvent::Enter) {
            ui->closeButton->setIcon(QIcon(":/icon/close-hover.png"));
        } else if (e->type() == QEvent::Leave) {
            ui->closeButton->setIcon(QIcon(":/icon/close.png"));
        }
    }

    return QWidget::eventFilter(watched, e);
}
  • 打赏
  • 举报
回复
不要光盯着图标看,把图标和字做在一张图片里,normal时设置普通时的图片,hover时设置另一张,这样在qss里只需设置背景图即可
DreamLife. 2016-11-23
  • 打赏
  • 举报
回复
引用 1 楼 caihuisinx 的回复:
qss完全可以实现你需要的功能
求具体代码,我没有弄出来
DreamLife. 2016-11-23
  • 打赏
  • 举报
回复
引用 5 楼 u010111033 的回复:
QPushButton:hover{ /*鼠标悬停在按钮上时*/ image: url(:/images/loadin.png); }
他要的的Ico的那种格式,这样写,换成ioc文件Qt提示语法问题 pushButton->setStyleSheet("QPushButton:hover{QIcon(:/logo.ico);}"); 抱错
DreamLife. 2016-11-23
  • 打赏
  • 举报
回复
我使用重载实现了你的功能,可以看一下http://blog.csdn.net/z609932088/article/details/53301297 主函数 #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->pushButton->setIcon(QIcon(":/picture.ico")); ui->pushButton->setStyleSheet("QPushButton:hover{QIcon(:/logo.ico);}"); } 重载。h #ifndef M_PUSHBUTON_H #define M_PUSHBUTON_H #include <QPushButton> #include <QEvent> class m_pushbuton : public QPushButton { public: m_pushbuton(QWidget *parent); void enterEvent(QEvent* event); void leaveEvent(QEvent* event); }; #endif // M_PUSHBUTON_H 重载。cpp #include "m_pushbuton.h" m_pushbuton::m_pushbuton(QWidget *parent): QPushButton(parent) { } void m_pushbuton::enterEvent(QEvent *event) { this->setIcon(QIcon(":/logo.ico")); } void m_pushbuton::leaveEvent(QEvent *event) { this->setIcon(QIcon(":/picture.ico")); } 这里我写的比较简单了,你可以在重在函数中加个参数,来改变资源路径
Little柯南 2016-11-23
  • 打赏
  • 举报
回复
QPushButton:hover{ /*鼠标悬停在按钮上时*/ image: url(:/images/loadin.png); }
bestman 2016-11-23
  • 打赏
  • 举报
回复
你直接带icon的图片作为背景,图片要跟按钮一样大,icon可以小
DreamLife. 2016-11-22
  • 打赏
  • 举报
回复
以我目前的能力,还是没有遇到过,不过明天可以到公司研究一下看看
daxuezdj7 2016-11-22
  • 打赏
  • 举报
回复
引用 1 楼 caihuisinx 的回复:
qss完全可以实现你需要的功能
刚看了,没找到相关的。能不能详细说下,挺急的
bestman 2016-11-22
  • 打赏
  • 举报
回复
qss完全可以实现你需要的功能

16,214

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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