Windows 中的低质量 QPixmap
Low quality QPixmap in Windows
我正在通过 QSqlTableModel 将 QPixmap 上传到 QTableView。在 Linux 上,图像以正常质量显示,但在 Windows 上,图像质量非常低。有可能以某种方式修复它吗?
Qt 6
我的代码片段:
SqlTableModel::SqlTableModel(QObject *parent, QSqlDatabase db)
: QSqlTableModel(parent, db)
{
int iconheight = QFontMetrics(QApplication::font()).height() * 2 - 4;
m_IconSize = QSize(iconheight, iconheight);
/*...*/
m_ActoinMMIcon = QPixmap(":/resources/img/many_many.svg", "SVG").
scaled(m_IconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
/*... etc ...*/
}
QVariant SqlTableModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::BackgroundRole && isDirty(index)) return QBrush(QColor(Qt::yellow));
auto field = record().field(index.column());
auto tablename = field.tableName();
auto fieldname = field.name();
/*...*/
// TABL_TASKS
if(tablename == TABL_TASKS)
{
if(fieldname == COL_TASKACTTYPE)
{
if(role == Qt::DisplayRole) return QString();
else if(role == Qt::DecorationRole)
{
auto value = QSqlTableModel::data(index, Qt::DisplayRole).toInt();
if(value == Action::ManyFromMany) return m_ActoinMMIcon;
else if(value == Action::OneFromMany) return m_ActoinOMIcon;
else if(value == Action::AsValue) return m_ActoinValueIcon;
else return m_ErrorIcon;
}
else if(role == Qt::SizeHintRole) return m_IconSize;
}
/*...*/
}
/*...*/
return QSqlTableModel::data(index, role);
}
Linux:
Windows:
在 app.exec()
调用之前的 main()
函数中的 Qt 6 之前,我们习惯于设置这些:
// Unfortunately no longer working with Qt 6.
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling)
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
至少它确实帮助解决了 Qt 的一些问题 Windows 图形渲染硬件集成英特尔® 图形等提供了 ANGLE 支持,但对于 Qt 5 来说并不总是一个问题。对于 Qt 6,这也可能是由于 ANGLE正在从支持图形中删除:How to ANGLE in Qt 6 OpenGL
对于这种情况,将 QML 与 ListView/etc 一起使用通常会带来更好的图形体验。小部件渲染取决于 Qt 自己的图形上下文。
另一个考虑因素是适当调整图标大小或更好地使用 1:1 源来显示视口尺寸。
是的,我修好了。如果在 Windows 10 OS 设置中启用了界面缩放,则会检测到问题(在我的例子中是 125%)。目前还不清楚。
问题修复如下:
SqlTableModel::SqlTableModel(QObject *parent, QSqlDatabase db)
: QSqlTableModel(parent, db)
{
auto pixelratio = qApp->primaryScreen()->devicePixelRatio();
auto iconheight = config->GUISize();
m_IconSize = QSize(iconheight, iconheight);
/*...*/
m_ActoinMMIcon = QPixmap(":/resources/img/many_many.svg", "SVG").
scaled(m_IconSize * pixelratio, Qt::KeepAspectRatio, Qt::SmoothTransformation);
m_ActoinMMIcon.setDevicePixelRatio(pixelratio);
/*...*/
}
/* etc */
我正在通过 QSqlTableModel 将 QPixmap 上传到 QTableView。在 Linux 上,图像以正常质量显示,但在 Windows 上,图像质量非常低。有可能以某种方式修复它吗?
Qt 6
我的代码片段:
SqlTableModel::SqlTableModel(QObject *parent, QSqlDatabase db)
: QSqlTableModel(parent, db)
{
int iconheight = QFontMetrics(QApplication::font()).height() * 2 - 4;
m_IconSize = QSize(iconheight, iconheight);
/*...*/
m_ActoinMMIcon = QPixmap(":/resources/img/many_many.svg", "SVG").
scaled(m_IconSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
/*... etc ...*/
}
QVariant SqlTableModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::BackgroundRole && isDirty(index)) return QBrush(QColor(Qt::yellow));
auto field = record().field(index.column());
auto tablename = field.tableName();
auto fieldname = field.name();
/*...*/
// TABL_TASKS
if(tablename == TABL_TASKS)
{
if(fieldname == COL_TASKACTTYPE)
{
if(role == Qt::DisplayRole) return QString();
else if(role == Qt::DecorationRole)
{
auto value = QSqlTableModel::data(index, Qt::DisplayRole).toInt();
if(value == Action::ManyFromMany) return m_ActoinMMIcon;
else if(value == Action::OneFromMany) return m_ActoinOMIcon;
else if(value == Action::AsValue) return m_ActoinValueIcon;
else return m_ErrorIcon;
}
else if(role == Qt::SizeHintRole) return m_IconSize;
}
/*...*/
}
/*...*/
return QSqlTableModel::data(index, role);
}
Linux:
Windows:
在 app.exec()
调用之前的 main()
函数中的 Qt 6 之前,我们习惯于设置这些:
// Unfortunately no longer working with Qt 6.
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling)
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
至少它确实帮助解决了 Qt 的一些问题 Windows 图形渲染硬件集成英特尔® 图形等提供了 ANGLE 支持,但对于 Qt 5 来说并不总是一个问题。对于 Qt 6,这也可能是由于 ANGLE正在从支持图形中删除:How to ANGLE in Qt 6 OpenGL
对于这种情况,将 QML 与 ListView/etc 一起使用通常会带来更好的图形体验。小部件渲染取决于 Qt 自己的图形上下文。
另一个考虑因素是适当调整图标大小或更好地使用 1:1 源来显示视口尺寸。
是的,我修好了。如果在 Windows 10 OS 设置中启用了界面缩放,则会检测到问题(在我的例子中是 125%)。目前还不清楚。 问题修复如下:
SqlTableModel::SqlTableModel(QObject *parent, QSqlDatabase db)
: QSqlTableModel(parent, db)
{
auto pixelratio = qApp->primaryScreen()->devicePixelRatio();
auto iconheight = config->GUISize();
m_IconSize = QSize(iconheight, iconheight);
/*...*/
m_ActoinMMIcon = QPixmap(":/resources/img/many_many.svg", "SVG").
scaled(m_IconSize * pixelratio, Qt::KeepAspectRatio, Qt::SmoothTransformation);
m_ActoinMMIcon.setDevicePixelRatio(pixelratio);
/*...*/
}
/* etc */