Icon Dialog
-
Hello,
Is it possible to set a Icon to a QML Dialog ?
http://doc.qt.io/qt-5/qml-qtquick-dialogs-dialog.html
It seems that there is nothing in documentation for it :(
On windows, RC_ICONS give only a Icon to the main windowsThank in advance
-
@didu
I don't think this is possible in QtQuick. According to the documentation, the RC_ICONS feature is specific to both MS Windows and the main application window. There, it is not implemented via a component property (which I would consider more natural) but via something I would rather consider a QMakeFile hack. It also seems to rely on the Windows-specific .ico format for icons. Also note that showing icons in dialog titlebars is a feature that may not be common, enabled or available at all on some other platforms. So this feature shouldn't really play a significant role in app(lication) design decisions.That said, it should somehow be possible to achieve what you want via the lower level C++ side of the Qt world, using QStyleOptionTitleBar::icon and more general QIcon means. But likely not (yet?) via pure QML / QtQuick / JavaScript.
-
@didu If you are loading the QML from C++ side then you can use setWindowIcon. It will set icon on the title bar for main window as well as all its child windows.
QGuiApplication app(argc, argv); app.setWindowIcon(QIcon("icon.png")); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
Dialog
invoked frommain.qml
will have that icon too. -
@p3c0
You are right it does. But it is the same behaviour as RC_ICONS ( http://doc.qt.io/qt-4.8/appicon.html ).
main.qml
andDialog
"child" have the icon.But the
Dialog
in my app, that does not have a Icon, is a "child" of aStackView
loaded bymain.qml
-
@p3c0 Here is a "simple" test code
The ProfileNameDialog has no Icon but the main.qml has :'(
(I made test with and without "app.setWindowIcon")
.pro... RC_ICONS += "logo.ico" ...
main.cpp
#include <QApplication> #include <QQmlApplicationEngine> #include <QIcon> int main(int argc, char *argv[]) { QApplication app(argc, argv); app.setWindowIcon(QIcon("logo.ico")); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); return app.exec(); }
main.qml
import QtQuick 2.4 import QtQuick.Controls 1.3 import QtQuick.Window 2.2 import QtQuick.Dialogs 1.2 ApplicationWindow { title: qsTr("Hello World") width: 640 height: 480 visible: true Component { id: mainId MyMain { } } StackView { id: stack anchors.fill: parent } Component.onCompleted: { stack.push(mainId) } }
MyMain.qml
import QtQuick 2.0 import QtQuick.Controls 1.3 Rectangle { width: 100 height: 62 Button { text: "Add" ProfileNameDialog { id: profileInputId } onClicked: profileInputId.visible = true } }
ProfileNameDialog.qml
import QtQuick 2.0 import QtQuick.Controls 1.2 import QtQuick.Layouts 1.1 import QtQuick.Dialogs 1.2 Dialog { id: profileInputId title: "Profile name" visible: false standardButtons: StandardButton.Ok | StandardButton.Cancel TextField { id: profileNameId; placeholderText: "Profile name" width: parent ? parent.width : implicitWidth } onAccepted: visible = false onRejected: visible = false; }
-
@didu Icon specified through
RC_ICONS
will not be available at runtime in the code. It is used to set the icon for the executable which is created after compiling.
In your case you need to store the icon in Qt's Resource System by creating a.qrc
file. After that it will be accessible from the code. Eg:app.setWindowIcon(QIcon("qrc:/images/logo.ico"));
Another quick way to test it ls loading the
ico
file from a physical location. Eg:app.setWindowIcon(QIcon("D:/MyIcons/logo.ico"));
The above should work too.
But a more practical approach is to use the Resource files. More info here. -
@p3c0 Thanks you, you are right I had to use the Resource files.
Now with the same test code I have provided:
If I open the Dialog the first time, there is not Icon.
I close the Dialog.
I open a second time the Dialog, the Icon set withapp.setWindowIcon
is shown.Have you already face such problem ?
-
@p3c0 said in Icon Dialog:
@didu Icon specified through
RC_ICONS
will not be available at runtime in the code. It is used to set the icon for the executable which is created after compiling.
In your case you need to store the icon in Qt's Resource System by creating a.qrc
file. After that it will be accessible from the code. Eg:app.setWindowIcon(QIcon("qrc:/images/logo.ico"));
Another quick way to test it ls loading the
ico
file from a physical location. Eg:app.setWindowIcon(QIcon("D:/MyIcons/logo.ico"));
The above should work too.
But a more practical approach is to use the Resource files. More info here.I was facing a similar issue with the icon being loaded successfully from its physical location but not from the resource file. So I tried
app.setWindowIcon(QIcon(":/Images/IconFile.ico"));
where "qrc" is removed which worked seamlessly.