MatthieuHAMEL
MatthieuHAMEL

Reputation: 21

QIcon not showing in QToolBar

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();
}

Upvotes: 1

Views: 338

Answers (1)

Yoruk
Yoruk

Reputation: 103

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 !

Upvotes: 0

Related Questions