QML / QWidget & Window icons



  • Hi everyone,

    I'm looking for the QML equivalent of QWidget::SetWindowIcon and I seem unable to find it in the docu:

    Theres the title property but, one for the icon seems to be missing.

    I can set an Icon by calling
    QApplication::setWindowIcon(); inside the main.cpp
    but, thats global and can't be changed, once exec() is called.

    My last Idea(untested so far) would be, to move from QQmlApplicationEngine to a QQuickView, because that one has a setIcon function, but I would rather not go the way over QWidgets.


  • Lifetime Qt Champion

    Hi,

    This interest mailing list thread might be of use.

    It looks like the Window type is missing some APIs.



  • What I would do:

    Create an attached type for the window with an icon property, effectively calling QWindow::setIcon.
    Usage would be like this:

    ApplicationWindow {
        // ...
        WindowHelper.icon: "qrc:/windowIconFile" 
      // ...
    }
    

    reference: http://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html#providing-attached-properties



  • Thanks @SGaist and @GrecKo for your input on this issue.

    I ended up following the tip I found in the mailing list conversation SGaist posted.

    A QML Window is a QQuickWindow which is a subclass of QWindow, so the method you would want to call is QWindow::setIcon(QIcon &). Maybe you can create a subclass of QQuickWindow in C++ ...

    this is also basicaly what GrecKo suggested, I think.

    The fix was surprisingly easy, and I'm surpised that this hasn't been added to the official release yet, the Mailinglist conversation was on Jan 29, 2013!

    the Custom QQuickWindow

    #ifndef QUICKWINDOW_H
    #define QUICKWINDOW_H
    
    #include <QQuickWindow>
    #include <QIcon>
    
    class QuickWindow : public QQuickWindow
    {
        Q_OBJECT
    
        Q_PROPERTY(QString windowIcon READ windowIcon WRITE setWindowIcon NOTIFY windowIconChanged)
    public:
        explicit QuickWindow(QQuickWindow *parent = nullptr) : QQuickWindow(parent)  {}
    
        const inline QString &windowIcon(){return str;}
        inline void setWindowIcon(const QString &str){
             if(str != m_str){
                 m_str = str;
                 setIcon(QIcon(m_str));
                 emit windowIconChanged();
             }
       }
    
    signals:
        void windowIconChanged();
    
    private:
        QString m_str;
    
    };
    
    #endif // QUICKWINDOW_H
    

    main.cpp

    #include "quickwindow.h"
    
    int main(int argc, char *argv[]){
       ....
       qmlRegisterType<QuickWindow>("QuickWindow",1,0, "QuickWindow");
        
       QQmlApplicationEngine engine;
       engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
       ....
    }
    

    main.qml

    import QuickWindow 1.0
    
    QuickWindow{
        ....
        title: qsTr("MyApp")
        windowIcon: ":/Path/to/image.png"
        ....
    }
    

    and done.

    Thanks for the help guys.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.