c++ - QT中如何制作带触发器的图片按钮

标签 c++ qt

<分区>

我刚刚开始学习 QT,我真的需要一些帮助。我想用(仅)一个图像(不显示任何边框或轮廓)制作一个按钮,并且我还需要能够在鼠标悬停在图像上和离开图像时更改图像。有帮助吗?

最佳答案

I want to make a button out of (just) an Image (not showing any border or outline), and I also need to be able to change the image when the mouse is hovering over the image and off the image. Any help?

也许最简单的方法是使用 QPushButton:

// the button has to be somehow initialized and maybe added to layout
QPushButton* pMyPushButton = new QPushButton(this);
layout()->AddWidget(pMyPushButton);

// try style it like that
// the concrete example limits the scope to pMyPushButton object 
pMyPushButton->setStyleSheet(
    "QPushButton {"
        "background: transparent;"
        "image: url(res/images/normal_bn_image.png);"
    "}"
    "QPushButton:hover {"
        "image: url(res/images/hover_bn_image.png);"
    "}"
  );

确保提供正确的图像 URL。也许这可以稍微澄清图像资源:Unable to set the background image in Qt Stylesheet

关于c++ - QT中如何制作带触发器的图片按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35658710/

相关文章:

c++ - Qt QLabel 默认文本

python - 通过 python 和 sqlite3 访问替代方案

c++ - 如何使用 QListWidget 打开 .jpg 图像?

qt - QML RowLayout 不会根据内容动态调整大小

c++ - 命名冲突。 C++ 和 OS 库(纯 C)

c++ - boost::interprocess 锁持久性

c++ - C++ 中的模板通用引用无法编译

linux - 使用 Qt Creator 绘制 csv 文件图表

c++ - 如何检查一个 C++ 类是否扩展了另一个类(比如另一个类是否是一个接口(interface))?

C++ 构造函数的乐趣——用自身的拷贝构造 Foo