Reputation: 387
I am uploading QPixmap to QTableView via QSqlTableModel. On Linux, images are displayed in normal quality, but on Windows, the image quality is very low. Is it possible to fix it somehow?
Qt 6
fragment of my code:
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);
}
Upvotes: 0
Views: 327
Reputation: 387
Yes, I fixed it. The problem is detected if the interface scaling is enabled in the Windows 10 OS settings (in my case it is 125%). It wasn't immediately clear. The problem is fixed as follows:
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 */
Upvotes: 1
Reputation: 8718
Before Qt 6 in the main()
function before the app.exec()
call we used to setting these:
// Unfortunately no longer working with Qt 6.
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling)
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
At least it did help with some problematic for Qt Windows graphics rendering hardware Integrated Intel® Graphics etc. given ANGLE support though not always a problem for Qt 5. With Qt 6 this may also due to ANGLE being removed from supporting graphics: How to ANGLE in Qt 6 OpenGL
Also using QML with ListView/etc for such case usually makes much better graphics experience. Widgets rendering depends on Qt's own graphics context.
One more consideration is proper sizing of icons or better use 1:1 source to viewport dimensions.
Upvotes: 1