Make the Main Window example use high-DPI toolbar icons
The Main Window example generates icons programmatically for its toolbars. However, these icons are shown with low resolution in a high-DPI display because the application is not enabling high-DPI pixmap support. Change-Id: Id763b707105d02f63162fff2efeb607eb5b59ed1 Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
parent
f8b164e1c3
commit
ac8011c161
@ -175,6 +175,8 @@ static ParseCommandLineArgumentsResult
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
|
||||
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
|
||||
QApplication app(argc, argv);
|
||||
MainWindow::CustomSizeHintMap customSizeHints;
|
||||
switch (parseCustomSizeHints(QCoreApplication::arguments(), &customSizeHints)) {
|
||||
|
@ -62,10 +62,10 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &color)
|
||||
static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &color, qreal pixelRatio)
|
||||
{
|
||||
int w = iconSize.width();
|
||||
int h = iconSize.height();
|
||||
int w = qRound(iconSize.width() * pixelRatio);
|
||||
int h = qRound(iconSize.height() * pixelRatio);
|
||||
|
||||
QImage image(w, h, QImage::Format_ARGB32_Premultiplied);
|
||||
image.fill(0);
|
||||
@ -75,11 +75,13 @@ static QPixmap genIcon(const QSize &iconSize, const QString &, const QColor &col
|
||||
extern void render_qt_text(QPainter *, int, int, const QColor &);
|
||||
render_qt_text(&p, w, h, color);
|
||||
|
||||
return QPixmap::fromImage(image, Qt::DiffuseDither | Qt::DiffuseAlphaDither);
|
||||
QPixmap pm = QPixmap::fromImage(image, Qt::DiffuseDither | Qt::DiffuseAlphaDither);
|
||||
pm.setDevicePixelRatio(pixelRatio);
|
||||
return pm;
|
||||
}
|
||||
|
||||
static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color)
|
||||
{ return genIcon(iconSize, QString::number(number), color); }
|
||||
static QPixmap genIcon(const QSize &iconSize, int number, const QColor &color, qreal pixelRatio)
|
||||
{ return genIcon(iconSize, QString::number(number), color, pixelRatio); }
|
||||
|
||||
ToolBar::ToolBar(const QString &title, QWidget *parent)
|
||||
: QToolBar(parent)
|
||||
@ -91,22 +93,23 @@ ToolBar::ToolBar(const QString &title, QWidget *parent)
|
||||
|
||||
setIconSize(QSize(32, 32));
|
||||
|
||||
qreal dpr = devicePixelRatioF();
|
||||
menu = new QMenu("One", this);
|
||||
menu->setIcon(genIcon(iconSize(), 1, Qt::black));
|
||||
menu->addAction(genIcon(iconSize(), "A", Qt::blue), "A");
|
||||
menu->addAction(genIcon(iconSize(), "B", Qt::blue), "B");
|
||||
menu->addAction(genIcon(iconSize(), "C", Qt::blue), "C");
|
||||
menu->setIcon(genIcon(iconSize(), 1, Qt::black, dpr));
|
||||
menu->addAction(genIcon(iconSize(), "A", Qt::blue, dpr), "A");
|
||||
menu->addAction(genIcon(iconSize(), "B", Qt::blue, dpr), "B");
|
||||
menu->addAction(genIcon(iconSize(), "C", Qt::blue, dpr), "C");
|
||||
addAction(menu->menuAction());
|
||||
|
||||
QAction *two = addAction(genIcon(iconSize(), 2, Qt::white), "Two");
|
||||
QAction *two = addAction(genIcon(iconSize(), 2, Qt::white, dpr), "Two");
|
||||
QFont boldFont;
|
||||
boldFont.setBold(true);
|
||||
two->setFont(boldFont);
|
||||
|
||||
addAction(genIcon(iconSize(), 3, Qt::red), "Three");
|
||||
addAction(genIcon(iconSize(), 4, Qt::green), "Four");
|
||||
addAction(genIcon(iconSize(), 5, Qt::blue), "Five");
|
||||
addAction(genIcon(iconSize(), 6, Qt::yellow), "Six");
|
||||
addAction(genIcon(iconSize(), 3, Qt::red, dpr), "Three");
|
||||
addAction(genIcon(iconSize(), 4, Qt::green, dpr), "Four");
|
||||
addAction(genIcon(iconSize(), 5, Qt::blue, dpr), "Five");
|
||||
addAction(genIcon(iconSize(), 6, Qt::yellow, dpr), "Six");
|
||||
orderAction = new QAction(this);
|
||||
orderAction->setText(tr("Order Items in Tool Bar"));
|
||||
connect(orderAction, &QAction::triggered, this, &ToolBar::order);
|
||||
|
Loading…
Reference in New Issue
Block a user