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 <QGraphicsDropShadowEffect>
#include <QPainterPath>
#include <QAction>
#include <QPushButton>
#include <QLineEdit>
#include <QComboBox>
#include <QDateTimeEdit>
#include <QMenu>
#include <QMenuBar>
#define QSSDIALOG_SHADOW_WIDTH 12
#define QSSDIALOG_BODER_WIDTH 0
@ -45,10 +52,10 @@ static void rangeObjectList(QObject*obj,int indent){
QssTtitleBar::QssTtitleBar(QWidget *parent ,
QTitleBar_Type type/* = QTitleBar_Type_Window*/)
: QWidget(parent),
m_Main(nullptr),
m_maxOrRestore(false),
m_pressed(false),
m_type(type),
m_Main(nullptr)
m_type(type)
{
setObjectName("qssTitleBar");
m_closeBtn = new QPushButton(this);
@ -180,9 +187,9 @@ void QssTtitleBar::mouseMoveEvent( QMouseEvent * ev )
QPoint movePt = globalPt - m_pressedPos;
parentWidget()->move(movePt);
QssMainWindow *parent = static_cast<QssMainWindow*>(parentWidget());
if(nullptr != parent){
parent->DetectDpiChange();
}
// if(nullptr != parent){
// parent->DetectDpiChange();
// }
return ;
}
@ -251,7 +258,6 @@ void QssMainWindow::OnMaxOrRestore(bool max)
setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
this->m_frame->setMinimumSize(0, 0);
this->m_frame->setMaximumSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX));
QSize sizeHint = parentWidget()->sizeHint();
QDesktopWidget desktop;
QRect rc = desktop.availableGeometry(-1);
@ -263,12 +269,23 @@ void QssMainWindow::OnMaxOrRestore(bool max)
this->setFixedHeight(rc.height() - m_titleBar->height());
}
}
void QssMainWindow::DpiChange(qreal dpi)
// 实际测试切换分辨率会触发这个slot
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)
@ -321,9 +338,14 @@ QssMainWindow::QssMainWindow(QWidget *parent/* = 0*/, Qt::WindowFlags flags/* =
if(nullptr != mCurrentScreen){
connect(mCurrentScreen,
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;
}
@ -332,11 +354,38 @@ QssMainWindow::~QssMainWindow()
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()
{
return m_titleBar;
}
void QssMainWindow::RangeObject()
{
rangeObjectList(m_frame,0);
}
void QssMainWindow::DetectDpiChange()
{
int screenNum = qApp->desktop()->screenNumber(this);
@ -382,7 +431,6 @@ enum {
BUTTOMRIGHT = 33
};
void QssMainWindow::setCursorShape(int CalPos)
{
Qt::CursorShape cursor;
@ -432,7 +480,6 @@ void QssMainWindow::showNormal()
void QssMainWindow::setWindowTitle( QString title )
{
//m_frame->setWindowTitle(title);
m_titleBar->setTitle(title);
}
@ -471,8 +518,6 @@ void QssMainWindow::onMouseMoveEvent(QMouseEvent * ev)
setCursorShape(CalCursorPos(ev->pos(),CalCursorCol(ev->pos())));
}
QPoint ptCurrentPos = QCursor::pos();
QPoint ptMoveSize = ptCurrentPos - m_ptViewMousePos;
QRect rtTempGeometry = this->geometry();
QRect rtCentralGeo = this->centralWidget()->geometry();
QRect rtMainWindow = this->geometry();
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)
{

24
Qss.h
View File

@ -26,7 +26,9 @@
class QPushButton;
class QLabel;
class QMouseEvent;
#define STANDARD_DPI 96.0
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02e0
#endif
@ -108,19 +110,7 @@ class QssEventFilter
{
public:
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;
}
};
@ -131,15 +121,18 @@ class QssMainWindow : public QMainWindow
public slots:
void OnMaxOrRestore(bool max);
void DpiChange(qreal);
void PhisycalDpiChange(qreal);
void LogicalDpiChange(qreal);
public:
typedef enum{
EVENT_MOVE = 523,
} EVENT_CUSTOM;
QssMainWindow(QWidget *parent = 0, Qt::WindowFlags flags = 0,float scale = 1);
virtual ~QssMainWindow();
void AdapteDpi();
QWidget *TitleBar();
void RangeObject();
void DetectDpiChange();
void ShowMask();
void HideMask();
@ -163,6 +156,9 @@ private:
void onMousePressEvent(QMouseEvent * ev);
void onMouseReleaseEvent(QMouseEvent * ev);
void dpiScaleChildren();
void detectDpi();
void rangeObjectList(QObject*obj,int indent);
protected:
QFrame* m_frame;
QRect mFrameRect;