gpt4 book ai didi

c++ - Qt:点击时QIcon消失; QIcon::模式不被尊重

转载 作者:行者123 更新时间:2023-11-28 05:13:11 26 4
gpt4 key购买 nike

我有一个 play button 的 png 格式图像我在其中存储为 embedded resource在我的申请中。

<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="icon">
<file>play.png</file>
</qresource>
</RCC>

我已经为 NormalActive 模式创建了一个具有相同源图像集的 QIcon

QIcon play;
play.addFile(":icon/play.png", QSize(), QIcon::Normal);
play.addFile(":icon/play.png", QSize(), QIcon::Active);

据我所知,这应该在未点击时显示图标(Normal 模式),并在点击时继续显示图标(Active 模式)

QIcon::Normal: Display the pixmap when the user is not interacting with the icon, but the functionality represented by the icon is available.

QIcon::Active: Display the pixmap when the functionality represented by the icon is available and the user is interacting with the icon, for example, moving the mouse over it or clicking it.

但是,当我点击它时它消失了(而是显示一个空白框)。

这是未点击的图标

alt text

这里是点击的图标

alt text

最小工作示例:

我创建了一个最小示例来复制我看到的行为

#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>

void initIcons()
{
Q_INIT_RESOURCE(view);
}

int main(int argc, char** argv)
{
QApplication* app = new QApplication(argc, argv);
QMainWindow* window = new QMainWindow();

QMenuBar* menu = new QMenuBar();
window->setMenuBar(menu);

QIcon play;
play.addFile(":icon/play.png", QSize(), QIcon::Normal);
play.addFile(":icon/play.png", QSize(), QIcon::Active);

QAction* action = new QAction(play, "", nullptr);
menu->addAction(action);

window->show();
return app->exec();
}

更新:

我还尝试了 ModeState 的所有组合,但行为没有任何变化:

play.addFile(":icon/play.png", QSize(), QIcon::Normal,   QIcon::On);
play.addFile(":icon/play.png", QSize(), QIcon::Normal, QIcon::Off);
play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::On);
play.addFile(":icon/play.png", QSize(), QIcon::Selected, QIcon::Off);
play.addFile(":icon/play.png", QSize(), QIcon::Active, QIcon::On);
play.addFile(":icon/play.png", QSize(), QIcon::Active, QIcon::Off);

我也试过addPixmap

QIcon play;
play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal, QIcon::On);
play.addPixmap(QPixmap(":icon/play.png"), QIcon::Normal, QIcon::Off);
play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::On);
play.addPixmap(QPixmap(":icon/play.png"), QIcon::Selected, QIcon::Off);
play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active, QIcon::On);
play.addPixmap(QPixmap(":icon/play.png"), QIcon::Active, QIcon::Off);

不幸的是,这些都没有改变行为。

最佳答案

我所理解的问题是 QMenuBar 的风格,我以这种方式修改了您的示例:

#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>


int main(int argc, char** argv)
{
QApplication* app = new QApplication(argc, argv);
QMainWindow* window = new QMainWindow();

QMenuBar* menu = new QMenuBar();
menu->setStyleSheet(R"style(
QMenuBar {
background-color: transparent;
}
QMenuBar::item:selected { /* when selected using mouse or keyboard */
background: #a8a8a8;
}

QMenuBar::item:pressed {
background: #888888;
}
)style");
window->setMenuBar(menu);

QIcon play;
const char path_to_icon[] = "/home/evgeniy/Downloads/icon-play-128.png";
play.addFile(path_to_icon, QSize(), QIcon::Normal);
play.addFile(path_to_icon, QSize(), QIcon::Active);

QAction* action = new QAction(play, "", nullptr);
menu->addAction(action);

window->show();
return app->exec();
}

按下后我可以看到图标。请注意,我的默认样式与您的不同,因此您可能需要修改样式表中的颜色。

关于c++ - Qt:点击时QIcon消失; QIcon::模式不被尊重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43153706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com