zcy 2022-05-03 21:27:00 +08:00
commit 43cd74b18f
2 changed files with 174 additions and 30 deletions

178
Qss.cpp
View File

@ -24,6 +24,13 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QGraphicsDropShadowEffect> #include <QGraphicsDropShadowEffect>
#include <QPainterPath> #include <QPainterPath>
#include <QAction>
#include <QPushButton>
#include <QLineEdit>
#include <QComboBox>
#include <QDateTimeEdit>
#include <QMenu>
#include <QMenuBar>
#define QSSDIALOG_SHADOW_WIDTH 12 #define QSSDIALOG_SHADOW_WIDTH 12
#define QSSDIALOG_BODER_WIDTH 0 #define QSSDIALOG_BODER_WIDTH 0
@ -45,10 +52,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);
@ -180,9 +187,9 @@ void QssTtitleBar::mouseMoveEvent( QMouseEvent * ev )
QPoint movePt = globalPt - m_pressedPos; QPoint movePt = globalPt - m_pressedPos;
parentWidget()->move(movePt); parentWidget()->move(movePt);
QssMainWindow *parent = static_cast<QssMainWindow*>(parentWidget()); QssMainWindow *parent = static_cast<QssMainWindow*>(parentWidget());
if(nullptr != parent){ // if(nullptr != parent){
parent->DetectDpiChange(); // parent->DetectDpiChange();
} // }
return ; return ;
} }
@ -251,7 +258,6 @@ void QssMainWindow::OnMaxOrRestore(bool max)
setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)); setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
this->m_frame->setMinimumSize(0, 0); this->m_frame->setMinimumSize(0, 0);
this->m_frame->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)); this->m_frame->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
QSize sizeHint = parentWidget()->sizeHint();
QDesktopWidget desktop; QDesktopWidget desktop;
QRect rc = desktop.availableGeometry(-1); QRect rc = desktop.availableGeometry(-1);
@ -263,12 +269,23 @@ void QssMainWindow::OnMaxOrRestore(bool max)
this->setFixedHeight(rc.height() - m_titleBar->height()); this->setFixedHeight(rc.height() - m_titleBar->height());
} }
} }
// 实际测试切换分辨率会触发这个slot
void QssMainWindow::DpiChange(qreal dpi) void QssMainWindow::PhisycalDpiChange(qreal dpi)
{ {
qDebug()<<"change dpi of QssMainWindow"<<dpi; qDebug()<<"dpi changed,reason: configuration "<<dpi;
} }
// 实际测试切换分辨率或者修改缩放比率都会触发这个slot
void QssMainWindow::LogicalDpiChange(qreal dpi)
{
qDebug()<<"dpi changed,reason: configuration "<<dpi;
m_dpi_ratio = 96/dpi;
qDebug()<<"factor is "<<m_dpi_ratio;
// auto parent = this->parentWidget();
// parent->setGeometry(parent->geometry().x(),parent->geometry().y(),
// parent->geometry().width()*m_dpi_ratio,
// parent->geometry().height()*m_dpi_ratio);
}
QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* = 0*/,float scale) QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* = 0*/,float scale)
@ -321,9 +338,14 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* =
if(nullptr != mCurrentScreen){ if(nullptr != mCurrentScreen){
connect(mCurrentScreen, connect(mCurrentScreen,
SIGNAL(physicalDotsPerInchChanged(qreal)), SIGNAL(physicalDotsPerInchChanged(qreal)),
this,SLOT(DpiChange(qreal))); this,SLOT(PhisycalDpiChange(qreal)));
connect(mCurrentScreen,
SIGNAL(logicalDotsPerInchChanged(qreal)),
this,SLOT(LogicalDpiChange(qreal)));
} }
rangeObjectList(m_frame,0); // rangeObjectList(m_frame,0);
detectDpi();
return; return;
} }
@ -332,11 +354,38 @@ QssMainWindow::~QssMainWindow()
m_frame->deleteLater(); m_frame->deleteLater();
} }
void QssMainWindow::AdapteDpi()
{
QObjectList child = this->m_frame->children();
int i = 0;
for(int i = 0;i < child.size();i++){
qDebug()<<child.at(i)->objectName() + " "
+ child.at(i)->metaObject()->className();
// QssTtitleBar
if(child.at(i)->objectName() =="qssTitleBar"){
QssTtitleBar *p = dynamic_cast<QssTtitleBar*>(child.at(i));
if(nullptr != p){
qDebug()<< p->sizePolicy();
p->setFixedHeight(p->height()*m_dpi_ratio);
}
}
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;
} }
void QssMainWindow::RangeObject()
{
rangeObjectList(m_frame,0);
}
void QssMainWindow::DetectDpiChange() void QssMainWindow::DetectDpiChange()
{ {
int screenNum = qApp->desktop()->screenNumber(this); int screenNum = qApp->desktop()->screenNumber(this);
@ -382,7 +431,6 @@ enum {
BUTTOMRIGHT = 33 BUTTOMRIGHT = 33
}; };
void QssMainWindow::setCursorShape(int CalPos) void QssMainWindow::setCursorShape(int CalPos)
{ {
Qt::CursorShape cursor; Qt::CursorShape cursor;
@ -432,7 +480,6 @@ void QssMainWindow::showNormal()
void QssMainWindow::setWindowTitle( QString title ) void QssMainWindow::setWindowTitle( QString title )
{ {
//m_frame->setWindowTitle(title);
m_titleBar->setTitle(title); m_titleBar->setTitle(title);
} }
@ -471,8 +518,6 @@ void QssMainWindow::onMouseMoveEvent(QMouseEvent * ev)
setCursorShape(CalCursorPos(ev->pos(),CalCursorCol(ev->pos()))); setCursorShape(CalCursorPos(ev->pos(),CalCursorCol(ev->pos())));
} }
QPoint ptCurrentPos = QCursor::pos(); QPoint ptCurrentPos = QCursor::pos();
QPoint ptMoveSize = ptCurrentPos - m_ptViewMousePos;
QRect rtTempGeometry = this->geometry();
QRect rtCentralGeo = this->centralWidget()->geometry(); QRect rtCentralGeo = this->centralWidget()->geometry();
QRect rtMainWindow = this->geometry(); QRect rtMainWindow = this->geometry();
setCursor(Qt::ArrowCursor); setCursor(Qt::ArrowCursor);
@ -580,6 +625,109 @@ void QssMainWindow::dpiScaleChildren()
} }
void QssMainWindow::detectDpi()
{
int logicdpi = logicalDpiX();
m_dpi_ratio = this->logicalDpiX() / 96.0;
}
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() =="QssTtitleBar"){
QssTtitleBar *p = dynamic_cast<QssTtitleBar*>(child.at(i));
if(nullptr != p){
p->setMinimumSize(p->width()*m_dpi_ratio,
p->height()*m_dpi_ratio);
}
}
if(child.at(i)->metaObject()->className() =="QMenuBar"){
QMenuBar *p = dynamic_cast<QMenuBar*>(child.at(i));
if(nullptr != p){
p->setMinimumSize(p->width()*m_dpi_ratio,
p->height()*m_dpi_ratio);
}
continue;
}
if(child.at(i)->metaObject()->className() =="QDateTimeEdit"){
QDateTimeEdit *p = dynamic_cast<QDateTimeEdit*>(child.at(i));
if(nullptr != p){
p->setMinimumSize(p->minimumHeight()*m_dpi_ratio,
p->minimumHeight()*m_dpi_ratio);
}
continue;
}
if(strcmp(child.at(i)->metaObject()->className(),"QLabel") == 0){
QLabel *p = dynamic_cast<QLabel*>(child.at(i));
if(nullptr != p){
qDebug()<< "parent is "
<< p->parent()->metaObject()->className();
if(p->parent() != nullptr){
QWidget *wparent = dynamic_cast<QWidget*> (p->parent());
if(nullptr != wparent){
if(nullptr != wparent->layout()){
qDebug()<< "index of label in layout is "
<<wparent->layout()->indexOf(p);
}
}
}
QFont pf = p->font();
pf.setPointSize(pf.pointSize()*this->m_dpi_ratio);
p->setFont(pf);
p->setMinimumSize(p->minimumHeight()*m_dpi_ratio,
p->minimumHeight()*m_dpi_ratio);
}
continue;
}
if(strcmp(child.at(i)->metaObject()->className(),"QComboBox") == 0){
QComboBox *p = dynamic_cast<QComboBox*>(child.at(i));
p->setMinimumSize(p->width()*m_dpi_ratio,
p->height()*m_dpi_ratio);
continue;
}
if(strcmp(child.at(i)->metaObject()->className(),"QPushButton") == 0){
QPushButton *p = dynamic_cast<QPushButton*>(child.at(i));
qDebug()<<tmp + child.at(i)->objectName() + " " +
child.at(i)->metaObject()->className() << p->size();
if(nullptr != p){
p->setFixedHeight(p->height()*m_dpi_ratio);
p->setMinimumWidth(p->width()*m_dpi_ratio);
}
continue;
}
if(strcmp(child.at(i)->metaObject()->className(),"QLineEdit") == 0){
QLineEdit *p = dynamic_cast<QLineEdit*>(child.at(i));
if(nullptr != p){
p->setFixedHeight(p->height()*this->m_dpi_ratio);
QFont pf = p->font();
}
continue;
}
if(child.at(i)->children().size() > 0){
this->rangeObjectList(child.at(i),indent + 1);
}
}
}
void QssMainWindow::showEvent(QShowEvent *ev) void QssMainWindow::showEvent(QShowEvent *ev)
{ {

24
Qss.h
View File

@ -26,7 +26,9 @@
class QPushButton; class QPushButton;
class QLabel; class QLabel;
class QMouseEvent; class QMouseEvent;
#define STANDARD_DPI 96.0 #define STANDARD_DPI 96.0
#ifndef WM_DPICHANGED #ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02e0 #define WM_DPICHANGED 0x02e0
#endif #endif
@ -108,19 +110,7 @@ class QssEventFilter
{ {
public: public:
virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *) Q_DECL_OVERRIDE
{/*
MSG* pMsg = reinterpret_cast<MSG*>(message);
if(nullptr != pMsg){
switch (pMsg->message)
{ {
case WM_DPICHANGED:
{
qDebug()<<"DPI CHANGED";
}
}
}*/
// TODO: filter out or modify msg struct here
return false; return false;
} }
}; };
@ -131,15 +121,18 @@ class QssMainWindow : public QMainWindow
public slots: public slots:
void OnMaxOrRestore(bool max); void OnMaxOrRestore(bool max);
void DpiChange(qreal); void PhisycalDpiChange(qreal);
void LogicalDpiChange(qreal);
public: public:
typedef enum{ typedef enum{
EVENT_MOVE = 523, EVENT_MOVE = 523,
} 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 DetectDpiChange(); void DetectDpiChange();
void ShowMask(); void ShowMask();
void HideMask(); void HideMask();
@ -163,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;