提问人:Jabu 提问时间:9/16/2023 更新时间:9/16/2023 访问量:28
高 DPI 导致像素图缩放错误
High DPI cause wrong scaling of pixmaps
问:
我在 Windows 6.5 上使用 Qt 10,我的应用程序上的像素图根据当前的 DPI 进行不同的缩放。
最小可重现的示例:
Windows 缩放 100%
规模 125%
规模 150%
class Label : public QLabel
{
Q_OBJECT
public:
QIcon icon;
Label(QWidget* parent = nullptr) : QLabel(parent){}
void paintEvent(QPaintEvent* event) override
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
icon.paint(&painter, rect(), Qt::AlignCenter, QIcon::Normal, QIcon::On);
}
};
inline QPixmap setRoundness(QPixmap pixmap, int roundness)
{
QPainterPath path;
qDebug() << pixmap.width() << pixmap.height();
// Round the pixmap by 50%
path.addRoundedRect(QRectF(0, 0, pixmap.width(), pixmap.height()), pixmap.width() / 2, pixmap.height() / 2);
QPixmap roundedPixmap = QPixmap(pixmap.size());
roundedPixmap.fill(Qt::transparent);
QPainter painter(&roundedPixmap);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::SmoothPixmapTransform);
painter.fillPath(path, Qt::black);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.drawPixmap(0, 0, pixmap);
pixmap.save("test_1.png");
roundedPixmap.save("test_2.png");
return roundedPixmap;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr) : QMainWindow(parent), ui(new Ui::MainWindowClass())
{
ui->setupUi(this);
setStyleSheet("#centralWidget { background-color: rgba(80, 80, 80, 80); }");
QIcon icon = QIcon("C:/Users/Katia/test.png"); // test.png => https://i.imgur.com/cZfWHkW.png
QPixmap roundedPixmap = setRoundness(icon.pixmap(QSize(55, 55)), 50);
Label* label = new Label;
label->icon = roundedPixmap;
QLabel* label_2 = new QLabel;
label_2->setPixmap(roundedPixmap);
QWidget* widget = new QWidget;
QHBoxLayout* hLayout = new QHBoxLayout(widget);
hLayout->addWidget(label);
hLayout->addWidget(label_2);
hLayout->setContentsMargins(0, 0, 0, 0);
hLayout->setSpacing(30);
setCentralWidget(widget);
setContentsMargins(32, 32, 32, 32);
return;
}
}
这是QPixmap中的问题,而不是QPaintEvent中的问题,因为当我保存时,这就是我得到的:roundedPixmap.save("test_2.png");
(DPI 150%)
(DPI100%)
我该如何解决这个dpi问题?
答: 暂无答案
评论