Skip to content

Commit

Permalink
完善高分屏显示效果
Browse files Browse the repository at this point in the history
  • Loading branch information
czyt1988 committed Dec 15, 2023
1 parent e3bbaf3 commit bd495cc
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 24 deletions.
2 changes: 2 additions & 0 deletions readme-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ int main(int argc, char* argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
QApplication a(argc, argv);
......
Expand All @@ -697,6 +698,7 @@ int main(int argc, char* argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ int main(int argc, char* argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
QApplication a(argc, argv);
......
Expand All @@ -697,6 +698,7 @@ int main(int argc, char* argv[])
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
Expand Down
2 changes: 0 additions & 2 deletions src/SARibbonBar/SARibbonBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,6 @@ void SARibbonBar::PrivateData::init()
mQuickAccessBar->setIcon(q_ptr->windowIcon());
//
mRightButtonGroup = RibbonSubElementDelegate->craeteButtonGroupWidget(q_ptr);
mRightButtonGroup->setFrameShape(QFrame::NoFrame);
//
setNormalMode();
}
Expand Down Expand Up @@ -1265,7 +1264,6 @@ SARibbonButtonGroupWidget* SARibbonBar::activeRightButtonGroup()
{
if (nullptr == d_ptr->mRightButtonGroup) {
d_ptr->mRightButtonGroup = RibbonSubElementDelegate->craeteButtonGroupWidget(this);
d_ptr->mRightButtonGroup->setFrameShape(QFrame::NoFrame);
}
d_ptr->mRightButtonGroup->show();
return d_ptr->mRightButtonGroup;
Expand Down
1 change: 1 addition & 0 deletions src/SARibbonBar/SARibbonButtonGroupWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ QAction* SARibbonButtonGroupWidget::addAction(QAction* a)
QAction* SARibbonButtonGroupWidget::addAction(const QString& text, const QIcon& icon, QToolButton::ToolButtonPopupMode popMode)
{
QAction* a = new QAction(icon, text, this);

addAction(a);
ButtonTyle* btn = qobject_cast< ButtonTyle* >(d_ptr->mItems.back().widget);
btn->setPopupMode(popMode);
Expand Down
21 changes: 8 additions & 13 deletions src/SARibbonBar/SARibbonColorToolButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ SARibbonColorToolButton::PrivateData::PrivateData(SARibbonColorToolButton* p) :

QPixmap SARibbonColorToolButton::PrivateData::createIconPixmap(const QStyleOptionToolButton& opt, const QSize& iconsize) const
{
if (opt.icon.isNull()) { //没有有图标
if (opt.icon.isNull()) { // 没有有图标
return QPixmap();
}
//有icon,在icon下方加入颜色
// 有icon,在icon下方加入颜色
QIcon::State state = (opt.state & QStyle::State_On) ? QIcon::On : QIcon::Off;
QIcon::Mode mode;
if (!(opt.state & QStyle::State_Enabled)) {
Expand All @@ -42,19 +42,14 @@ QPixmap SARibbonColorToolButton::PrivateData::createIconPixmap(const QStyleOptio
mode = QIcon::Normal;
}
QSize realIconSize = iconsize - QSize(0, c_ribbonbutton_color_height + 1);
realIconSize *= q_ptr->devicePixelRatioF();
QPixmap pixmap = opt.icon.pixmap(q_ptr->window()->windowHandle(), realIconSize, mode, state);
pixmap.setDevicePixelRatio(q_ptr->devicePixelRatioF());
QPixmap res(pixmap.size() + QSize(4, c_ribbonbutton_color_height + 4)); //宽度上,颜色块多出2px
res.setDevicePixelRatio(q_ptr->devicePixelRatioF());
QPixmap res(pixmap.size() + QSize(4, c_ribbonbutton_color_height + 4)); // 宽度上,颜色块多出2px
res.fill(Qt::transparent);
QPainter painter(&res);
int xpixmap = (res.width() - pixmap.width()) / 2;
int ypixmap = (res.height() - c_ribbonbutton_color_height - 2 - pixmap.height()) / 2; //这里要减去2而不是1,这样奇数偶数都不会影响
xpixmap /= q_ptr->devicePixelRatioF();
ypixmap /= q_ptr->devicePixelRatioF();
int w = pixmap.width()/q_ptr->devicePixelRatioF();
int h = pixmap.height()/q_ptr->devicePixelRatioF();
int xpixmap = (res.width() - pixmap.width()) / 2;
int ypixmap = (res.height() - c_ribbonbutton_color_height - 2 - pixmap.height()) / 2; // 这里要减去2而不是1,这样奇数偶数都不会影响
int w = pixmap.width();
int h = pixmap.height();
QRect rpixmap = QRect(xpixmap, ypixmap, w, h);
painter.drawPixmap(rpixmap, pixmap);
QRect colorRect = rpixmap.adjusted(0, h + 1, 0, c_ribbonbutton_color_height + 1);
Expand Down Expand Up @@ -204,7 +199,7 @@ void SARibbonColorToolButton::onButtonClicked(bool checked)
void SARibbonColorToolButton::paintIcon(QPainter& p, const QStyleOptionToolButton& opt, const QRect& iconDrawRect)
{
if (ColorUnderIcon == colorStyle()) {
//有icon
// 有icon
QPixmap pm = d_ptr->createIconPixmap(opt, iconDrawRect.size());
style()->drawItemPixmap(&p, iconDrawRect, Qt::AlignCenter, pm);
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/SARibbonBar/SARibbonCtrlContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SARibbonCtrlContainer::PrivateData
void setContainerWidget(QWidget* w)
{
if (containerWidget) {
//原来有widget
// 原来有widget
QWidget* oldwidget = containerWidget;
takeContainerWidget(oldwidget);
oldwidget->deleteLater();
Expand Down Expand Up @@ -118,8 +118,7 @@ bool SARibbonCtrlContainer::hasContainerWidget() const
void SARibbonCtrlContainer::setIcon(const QIcon& i)
{
d_ptr->icon = i;
QPixmap pixmap = i.pixmap(d_ptr->iconSize * devicePixelRatioF());
pixmap.setDevicePixelRatio(devicePixelRatioF());
QPixmap pixmap = i.pixmap(d_ptr->iconSize);
d_ptr->labelPixmap->setPixmap(pixmap);
}

Expand Down
8 changes: 2 additions & 6 deletions src/SARibbonBar/SARibbonToolButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,13 +622,9 @@ QPixmap SARibbonToolButton::PrivateData::createIconPixmap(const QStyleOptionTool
} else {
mode = QIcon::Normal;
}
//添加高分屏支持
qreal pr = QApplication::primaryScreen()->devicePixelRatio();
// 添加高分屏支持
QSize pxiampSize = iconsize - QSize(2, 2);
pxiampSize *= pr;
QPixmap px = opt.icon.pixmap(pxiampSize, mode, state);
px.setDevicePixelRatio(pr);
return px;
return opt.icon.pixmap(pxiampSize, mode, state);
}

int SARibbonToolButton::PrivateData::getTextAlignment() const
Expand Down
1 change: 1 addition & 0 deletions src/example/MainWindowExample/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ int main(int argc, char* argv[])
// 以下是针对高分屏的设置,有高分屏需求都需要按照下面进行设置
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
#endif
#if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
Expand Down

0 comments on commit bd495cc

Please sign in to comment.