添加手动dpi适配功能

qt6
zcy 2022-04-09 21:02:25 +08:00
parent d3139ba090
commit 7e465c3ab4
2 changed files with 63 additions and 5 deletions

63
Qss.cpp
View File

@ -24,6 +24,7 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QGraphicsDropShadowEffect> #include <QGraphicsDropShadowEffect>
#include <QPainterPath> #include <QPainterPath>
#include <QAction>
#define QSSDIALOG_SHADOW_WIDTH 12 #define QSSDIALOG_SHADOW_WIDTH 12
#define QSSDIALOG_BODER_WIDTH 0 #define QSSDIALOG_BODER_WIDTH 0
@ -45,10 +46,10 @@ static void rangeObjectList(QObject*obj,int indent){
QssTtitleBar::QssTtitleBar(QWidget *parent , QssTtitleBar::QssTtitleBar(QWidget *parent ,
QTitleBar_Type type/* = QTitleBar_Type_Window*/) QTitleBar_Type type/* = QTitleBar_Type_Window*/)
: QWidget(parent), : QWidget(parent),
m_Main(nullptr),
m_maxOrRestore(false), m_maxOrRestore(false),
m_pressed(false), m_pressed(false),
m_type(type), m_type(type)
m_Main(nullptr)
{ {
setObjectName("qssTitleBar"); setObjectName("qssTitleBar");
m_closeBtn = new QPushButton(this); m_closeBtn = new QPushButton(this);
@ -333,7 +334,8 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* =
SIGNAL(logicalDotsPerInchChanged(qreal)), SIGNAL(logicalDotsPerInchChanged(qreal)),
this,SLOT(LogicalDpiChange(qreal))); this,SLOT(LogicalDpiChange(qreal)));
} }
rangeObjectList(m_frame,0); // rangeObjectList(m_frame,0);
detectDpi();
return; return;
} }
@ -342,6 +344,20 @@ QssMainWindow::~QssMainWindow()
m_frame->deleteLater(); m_frame->deleteLater();
} }
void QssMainWindow::AdapteDpi()
{
QObjectList child = this->children();
int i = 0;
for(int i = 0;i < child.size();i++){
qDebug()<<child.at(i)->objectName() + " "
+ child.at(i)->metaObject()->className();
if(child.at(i)->children().size() > 0){
this->rangeObjectList(child.at(i),i + 1);
}
}
}
QWidget *QssMainWindow::TitleBar() QWidget *QssMainWindow::TitleBar()
{ {
return m_titleBar; return m_titleBar;
@ -397,7 +413,6 @@ enum {
BUTTOMRIGHT = 33 BUTTOMRIGHT = 33
}; };
void QssMainWindow::setCursorShape(int CalPos) void QssMainWindow::setCursorShape(int CalPos)
{ {
Qt::CursorShape cursor; Qt::CursorShape cursor;
@ -592,6 +607,46 @@ void QssMainWindow::dpiScaleChildren()
} }
void QssMainWindow::detectDpi()
{
m_dpi_ratio = this->logicalDpiX() / 96;
}
void QssMainWindow::rangeObjectList(QObject *obj, int indent)
{
QObjectList child = obj->children();
QString tmp("");
for(int i = 0;i < indent;i++)
tmp +=" ";
for(int i = 0;i < child.size();i++){
qDebug()<<tmp + child.at(i)->objectName() + " " +
child.at(i)->metaObject()->className();
if(child.at(i)->metaObject()->className() =="QAction"){
QAction *p = dynamic_cast<QAction*>(child.at(i));
if(nullptr != p){
}
}
if(strcmp(child.at(i)->metaObject()->className(),"QLabel") == 0){
QLabel *p = dynamic_cast<QLabel*>(child.at(i));
if(nullptr != p){
p->setFixedHeight(p->height()*this->m_dpi_ratio);
}
}
if(child.at(i)->metaObject()->className() =="QLabel"){
QLabel *p = dynamic_cast<QLabel*>(child.at(i));
if(nullptr != p){
p->setFixedHeight(p->height()*this->m_dpi_ratio);
}
}
if(child.at(i)->children().size() > 0){
this->rangeObjectList(child.at(i),indent + 1);
}
}
}
void QssMainWindow::showEvent(QShowEvent *ev) void QssMainWindow::showEvent(QShowEvent *ev)
{ {

5
Qss.h
View File

@ -130,7 +130,7 @@ public:
} EVENT_CUSTOM; } EVENT_CUSTOM;
QssMainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0,float scale = 1); QssMainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0,float scale = 1);
virtual ~QssMainWindow(); virtual ~QssMainWindow();
void AdapteDpi();
QWidget *TitleBar(); QWidget *TitleBar();
void RangeObject(); void RangeObject();
void DetectDpiChange(); void DetectDpiChange();
@ -156,6 +156,9 @@ private:
void onMousePressEvent(QMouseEvent * ev); void onMousePressEvent(QMouseEvent * ev);
void onMouseReleaseEvent(QMouseEvent * ev); void onMouseReleaseEvent(QMouseEvent * ev);
void dpiScaleChildren(); void dpiScaleChildren();
void detectDpi();
void rangeObjectList(QObject*obj,int indent);
protected: protected:
QFrame* m_frame; QFrame* m_frame;
QRect mFrameRect; QRect mFrameRect;