Qicon未在Qtoolbar中显示

发布于 2025-01-30 22:50:24 字数 1521 浏览 5 评论 0 原文

我是QT的初学者,目前正在阅读以下内容:

当我在qtoolbar中声明qactions时,qpixmap对象(变成Qicons)并未显示:

没有图标,只有文本

但是,当我在没有工具栏的情况下声明qmenu时,qpixmap映像实际上显示了。

我正在使用QT6;在Fedora工作;我的编译器上没有警告。

simple_menu.hpp

#ifndef SIMPLE_MENU_HPP
#define SIMPLE_MENU_HPP

#include <QMainWindow>
#include <QApplication>

class SimpleMenu : public QMainWindow
{
    public:
    SimpleMenu(QWidget *parent = nullptr);
};

#endif

simple_menu.cpp

#include "simple_menu.hpp"

#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QIcon>
#include <QAction>

SimpleMenu::SimpleMenu(QWidget *parent)
    : QMainWindow(parent) 
{
    QPixmap newpix("new.png");
    QPixmap openpix("open.png");

    QToolBar *toolbar = addToolBar("main toolbar");
    toolbar->addAction(QIcon(newpix), "New File");
    toolbar->addAction(QIcon(openpix), "Open File");
}

main.cpp

#include "simple_menu.hpp"

int main(int argc, char *argv[]) 
{
    QApplication app(argc, argv);

    SimpleMenu window;

    window.resize(350, 250);
    window.setWindowTitle("Tuto Toolbar");
    window.show();

    return app.exec();
}

I'm a beginner in Qt, currently reading this : https://zetcode.com/gui/qt5/menusandtoolbars/

When I declare QActions in a QToolBar, the QPixmap objects (turned into QIcons) are not showing :

No icons, only text

However, the QPixmap images are actually showing when I declare a QMenu without the toolbar.

I am using Qt6 ; working on Fedora ; no warning shown on my compiler.

simple_menu.hpp

#ifndef SIMPLE_MENU_HPP
#define SIMPLE_MENU_HPP

#include <QMainWindow>
#include <QApplication>

class SimpleMenu : public QMainWindow
{
    public:
    SimpleMenu(QWidget *parent = nullptr);
};

#endif

simple_menu.cpp

#include "simple_menu.hpp"

#include <QMenu>
#include <QMenuBar>
#include <QToolBar>
#include <QIcon>
#include <QAction>

SimpleMenu::SimpleMenu(QWidget *parent)
    : QMainWindow(parent) 
{
    QPixmap newpix("new.png");
    QPixmap openpix("open.png");

    QToolBar *toolbar = addToolBar("main toolbar");
    toolbar->addAction(QIcon(newpix), "New File");
    toolbar->addAction(QIcon(openpix), "Open File");
}

main.cpp

#include "simple_menu.hpp"

int main(int argc, char *argv[]) 
{
    QApplication app(argc, argv);

    SimpleMenu window;

    window.resize(350, 250);
    window.setWindowTitle("Tuto Toolbar");
    window.show();

    return app.exec();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

屋檐 2025-02-06 22:50:24

也许系统找不到您的图片。请尝试将完整的文件路径(如:)放置。

QPixmap newpix("c:\mydoc\pictures\new.png");

要检查图片是否已加载,您可以做一些不同的事情:

   QPixmap newpix;
   qDebug() << newpix.load("c:\mydoc\pictures\new.png"); 

在加载失败的情况下,方法返回对还是错。

无论如何,如果要将图标嵌入最终EXE文件中,请检查 qt Resources System

这是一个方便的系统,可以实现您想做的事情,并避免处理本地驱动器上的存储路径。

祝你好运 !

Maybe the system cannot find your pictures. Please try to put the full file path (like :).

QPixmap newpix("c:\mydoc\pictures\new.png");

To check if the pictures are loaded, you can do something a bit different :

   QPixmap newpix;
   qDebug() << newpix.load("c:\mydoc\pictures\new.png"); 

the load method returns true or false in case of loading fail.

Anyway, if you want to embed your icons in the final EXE file, check the Qt resource system

It's a convenient system to achieve what you want to do, and to avoid dealing with storage paths on your local drives.

Good luck !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文