Discussion:
How are High DPI Icons supposed to work?
(too old to reply)
Julius Bullinger
2015-09-25 11:19:18 UTC
Permalink
Dear List,

is there any clear and concise How To for High DPI icons/pixmaps? I gathered some information from several Wiki pages, Blog posts and documentation pages [1-7], but no luck.

My MNWE (minimal not working example):

// from main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setAttribute(Qt::AA_UseHighDpiPixmaps);

MainWindow w;
w.show();

return a.exec();
}


// from mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent)

{



QPixmap pixmap(":/icons/48x48/icon.png");

QPixmap pixmap2x(":/icons/48x48/***@2x.png");

pixmap2x.setDevicePixelRatio(2);



QLabel* pbLabel = new QLabel();

pbLabel->setText(tr("PushButton:"));



QPushButton* pushButton = new QPushButton();

pushButton->setIcon(pixmap2x);

pushButton->setIconSize(QSize(48, 48));



QVBoxLayout* layout = new QVBoxLayout();

layout->addWidget(pbLabel);

layout->addWidget(pushButton);



QWidget* centralWidget = new QWidget();

centralWidget->setLayout(layout);

setCentralWidget(centralWidget);

}

What am I missing?

Best regards,
Julius

[1]: http://blog.qt.io/blog/2013/04/25/retina-display-support-for-mac-os-ios-and-x11/
[2]: http://wiki.qt.io/Icons_In_Qt_Quick_Controls
[3]: http://doc.qt.io/qt-5/scalability.html
[4]: http://doc.qt.io/qt-5/qicon.html#pixmap
[5]: http://doc.qt.io/qt-5/highdpi.html
[6]: http://www.kdab.com/pixels-trust-scalable-uis-qml-part-2/
[7]: https://bugreports.qt.io/browse/QTBUG-44486
Hamish Moffatt
2015-09-28 00:43:20 UTC
Permalink
Post by Julius Bullinger
Dear List,
is there any clear and concise How To for High DPI icons/pixmaps? I
gathered some information from several Wiki pages, Blog posts and
documentation pages [1-7], but no luck.
Your code looks ok. You didn't say what the problem was?
Post by Julius Bullinger
pixmap2x.setDevicePixelRatio(2);
You don't need to set the DPR manually, QPixmap will set it when it sees
the @2x in the filename.


Hamish
Julius Bullinger
2015-09-28 07:27:49 UTC
Permalink
Post by Hamish Moffatt
Your code looks ok. You didn't say what the problem was?
You're right, I'm sorry for that. As you can see in the attached screenshot, the @2x icon is not actually used in a high DPI/DPR application:

QPushButton* pbCustom = new QPushButton();
pbCustom->setText(tr("QPushButton w/ pixmap"));
pbCustom->setIcon(pixmap);
pbCustom->setIconSize(QSize(48, 48));
// ...

QPushButton* pbCustom2x = new QPushButton();
pbCustom2x->setText(tr("QPushButton w/ pixmap2x"));
pbCustom2x->setIcon(pixmap2x);
pbCustom2x->setIconSize(QSize(48, 48));
// ...

This is with QT_DEVICE_PIXEL_RATIO set to both '2' and 'auto'.
Post by Hamish Moffatt
Post by Julius Bullinger
pixmap2x.setDevicePixelRatio(2);
That's what I figured out after sending my mail. Thanks anyway!

Best regards,
Julius
Hamish Moffatt
2015-09-29 06:10:57 UTC
Permalink
Post by Julius Bullinger
Post by Hamish Moffatt
Your code looks ok. You didn't say what the problem was?
I'm still a bit confused, as it looks like the @2x images were used in
that screenshot. What should be different?
Post by Julius Bullinger
This is with QT_DEVICE_PIXEL_RATIO set to both '2' and 'auto'.
I've only worked with this stuff on Mac, and there there's no need to
set any variables like that.


Hamish
Julius Bullinger
2015-09-29 06:27:03 UTC
Permalink
Post by Hamish Moffatt
Your code looks ok. You didn't say what the problem was?
They are only used where I explicitly requested the @2x icon. Maybe I misunderstand the whole thing, but I was under the impression that the ***@2x.png file should be used transparently if icon.png is requested in a High DPI case.

So, I expected _both_ lower buttons to use the same ***@2x.png, even though it's only explicitly set in one of those.

Otherwise, what would be the advantage of using @2x icons if I had to manually distinguish between high and low DPR anyway?

Best regards,
Julius
Hamish Moffatt
2015-09-29 06:49:39 UTC
Permalink
Post by Hamish Moffatt
Your code looks ok. You didn't say what the problem was?
Oh - QIcon does do that, but QPixmap does not.


Hamish
Thiago Macieira
2015-09-29 06:32:11 UTC
Permalink
Post by Hamish Moffatt
Post by Julius Bullinger
This is with QT_DEVICE_PIXEL_RATIO set to both '2' and 'auto'.
I've only worked with this stuff on Mac, and there there's no need to
set any variables like that.
And QT_DEVICE_PIXEL_RATIO is deprecated -- it will print a qWarning in Qt 5.6
now. Please don't set it.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Julius Bullinger
2015-09-29 08:19:43 UTC
Permalink
Post by Hamish Moffatt
Oh - QIcon does do that, but QPixmap does not.
Ah, so I misunderstood the documentation. Switching to QIcon loads the correct @2x icon. Thank you very much!

Best Regards,
Julius
Julius Bullinger
2015-09-29 08:20:38 UTC
Permalink
And QT_DEVICE_PIXEL_RATIO is deprecated -- it will print a qWarning in Qt 5.6 now. Please don't set it.
I know about the upcoming changes in 5.6, but AFAIK this the only option in 5.5 right now.
But please enlighten me if there's a better way working with Qt 5.5.
Adam Light
2015-09-29 12:55:01 UTC
Permalink
On Tue, Sep 29, 2015 at 1:20 AM, Julius Bullinger <
Post by Thiago Macieira
And QT_DEVICE_PIXEL_RATIO is deprecated -- it will print a qWarning in
Qt 5.6 now. Please don't set it.
I know about the upcoming changes in 5.6, but AFAIK this the only option in 5.5 right now.
But please enlighten me if there's a better way working with Qt 5.5.
On Macintosh, it's not necessary to set QT_DEVICE_PIXEL_RATIO when using Qt
5.5, as the correct resource is used (in buttons at least, but not in some
other widgets that display icons). However, AFAIK, using
QT_DEVICE_PIXEL_RATIO is required to get this to work at all on Windows
with Qt 5.5.

Adam
Thiago Macieira
2015-09-29 15:42:44 UTC
Permalink
Post by Adam Light
On Tue, Sep 29, 2015 at 1:20 AM, Julius Bullinger <
Post by Thiago Macieira
And QT_DEVICE_PIXEL_RATIO is deprecated -- it will print a qWarning in
Qt 5.6 now. Please don't set it.
I know about the upcoming changes in 5.6, but AFAIK this the only option
in 5.5 right now.
But please enlighten me if there's a better way working with Qt 5.5.
On Macintosh, it's not necessary to set QT_DEVICE_PIXEL_RATIO when using Qt
5.5, as the correct resource is used (in buttons at least, but not in some
other widgets that display icons). However, AFAIK, using
QT_DEVICE_PIXEL_RATIO is required to get this to work at all on Windows
with Qt 5.5.
We had a discussion about that yesterday on IRC and concluded the windows
platform plugin must enable it automatically. Relying on the environment
variable to be set is wrong.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Philippe
2015-09-29 15:49:29 UTC
Permalink
Well, an application should be able to enable or disable HDPI support at
Qt startup, programatically.

Philippe

On Tue, 29 Sep 2015 08:42:44 -0700
Post by Thiago Macieira
Post by Adam Light
On Tue, Sep 29, 2015 at 1:20 AM, Julius Bullinger <
Post by Thiago Macieira
And QT_DEVICE_PIXEL_RATIO is deprecated -- it will print a qWarning in
Qt 5.6 now. Please don't set it.
I know about the upcoming changes in 5.6, but AFAIK this the only option
in 5.5 right now.
But please enlighten me if there's a better way working with Qt 5.5.
On Macintosh, it's not necessary to set QT_DEVICE_PIXEL_RATIO when using Qt
5.5, as the correct resource is used (in buttons at least, but not in some
other widgets that display icons). However, AFAIK, using
QT_DEVICE_PIXEL_RATIO is required to get this to work at all on Windows
with Qt 5.5.
We had a discussion about that yesterday on IRC and concluded the windows
platform plugin must enable it automatically. Relying on the environment
variable to be set is wrong.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
_______________________________________________
Interest mailing list
http://lists.qt-project.org/mailman/listinfo/interest
Thiago Macieira
2015-09-29 16:04:51 UTC
Permalink
Post by Philippe
Well, an application should be able to enable or disable HDPI support at
Qt startup, programatically.
Agreed. But that means an API, not an environment variable.

Qt Creator did the same thing: it set the variable via qputenv in its main
function. The result? That got inherited by all applications it ran. Worse, as
soon as you began running Qt 5.6 applications, they all began producing
qWarnings if you ran them inside Qt Creator.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel Open Source Technology Center
Continue reading on narkive:
Loading...