From a8ebb5f89b011659f525075a783450990a6ee20a Mon Sep 17 00:00:00 2001 From: likehomedream Date: Tue, 5 Sep 2023 14:44:53 +0800 Subject: [PATCH] fix update showwidget radius --- src/globalthemefeature.cpp | 48 ++++++++++++++++++++++++-------------- src/globalthemefeature.h | 5 ++++ src/globalthemewidget.cpp | 4 ++++ src/mainwindow.h | 3 --- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/globalthemefeature.cpp b/src/globalthemefeature.cpp index c47321b..6d74095 100644 --- a/src/globalthemefeature.cpp +++ b/src/globalthemefeature.cpp @@ -47,15 +47,7 @@ void GlobalImageWidget::updateButtonColor(const QColor& color) { void GlobalImageWidget::updateWidgetBackgroundColor(const QColor& color) { - if (!m_showwidget) { - qDebug() << "m_showwidget is null"; - return; - } else { - QPalette palette = m_showwidget->palette(); - palette.setColor(QPalette::Background, color); - m_showwidget->setAutoFillBackground(true); - m_showwidget->setPalette(palette); - } + m_showwidget->setBackgroundColor(color); } @@ -85,6 +77,14 @@ void GlobalImageWidget::updateOverlayImage(const QString& overlayImagePath) { } } +void GlobalImageWidget::updatescale() +{ + graphicsView->resetTransform(); + QPointF centerPoint(1500, 600); + graphicsView->centerOn(centerPoint); + graphicsView->scale(1, 1); +} + GlobalImageWidget::GlobalImageWidget(QWidget *parent, const QString& coverFilePath) : QWidget(parent), m_coverFilePath(coverFilePath) { @@ -165,6 +165,8 @@ Globalthemefeature::Globalthemefeature(QWidget *parent, const QString& coverFile RoundedWidget::RoundedWidget(QWidget *parent) : QWidget(parent), m_radius(0) { + setAttribute(Qt::WA_TranslucentBackground); + setRadius(6); } void RoundedWidget::setRadius(int radius) @@ -172,18 +174,30 @@ void RoundedWidget::setRadius(int radius) m_radius = radius; } +void RoundedWidget::setBackgroundColor(const QColor &color) +{ + backgroundColor = color; + update(); +} + void RoundedWidget::paintEvent(QPaintEvent *event) { - Q_UNUSED(event) QPainter painter(this); - painter.setRenderHint(QPainter::Antialiasing); - painter.setPen(Qt::black); - painter.setBrush(Qt::NoBrush); // 设置填充为无色 + painter.setRenderHint(QPainter::Antialiasing, true); // 开启抗锯齿 - const QRectF rect = contentsRect().adjusted(1, 1, -1, -1); - QPainterPath path; - path.addRoundedRect(rect, m_radius, m_radius); + QRectF rect = contentsRect().adjusted(borderWidth, borderWidth, -borderWidth, -borderWidth); + int r = std::min(rect.width(), rect.height()) / 2; // 计算圆角半径 + int actualRadius = std::min(m_radius, r); // 取实际圆角半径和计算半径的较小值 + + // 绘制背景 + painter.setPen(Qt::NoPen); + painter.setBrush(backgroundColor); + painter.drawRoundedRect(rect, actualRadius, actualRadius); + + // 绘制边框 +// painter.setPen(QPen(borderColor, borderWidth)); +// painter.setBrush(Qt::NoBrush); +// painter.drawRoundedRect(rect, actualRadius, actualRadius); - painter.drawPath(path); } diff --git a/src/globalthemefeature.h b/src/globalthemefeature.h index 16e5d5d..d4cce84 100644 --- a/src/globalthemefeature.h +++ b/src/globalthemefeature.h @@ -22,12 +22,16 @@ public: RoundedWidget(QWidget *parent = nullptr); void setRadius(int radius); + void setBackgroundColor(const QColor& color); protected: void paintEvent(QPaintEvent *event) override; private: int m_radius; + QColor backgroundColor = Qt::white; // 背景颜色 + QColor borderColor = Qt::white; // 边框颜色 + int borderWidth = 1; // 边框宽度 }; class CustomGraphicsView : public QGraphicsView { @@ -46,6 +50,7 @@ public: void updateWidgetBackgroundColor(const QColor& color); void update(const QString& coverFilePath); void updateOverlayImage(const QString& overlayImagePath); + void updatescale(); private: CustomGraphicsView* graphicsView; QString m_coverFilePath; diff --git a/src/globalthemewidget.cpp b/src/globalthemewidget.cpp index 49505f7..b14ede6 100644 --- a/src/globalthemewidget.cpp +++ b/src/globalthemewidget.cpp @@ -169,11 +169,13 @@ void GlobalThemeWidget::initExteriorWidget() m_preview->updateWidgetBackgroundColor(selectedColor); m_preview->updateOverlayImage(":/resource/background/panel-light.png"); + m_preview->updatescale(); }else if(index == 1){ QColor selectedColor = combobox->itemData(index).value(); m_preview->updateWidgetBackgroundColor(selectedColor); m_preview->updateOverlayImage(":/resource/background/panel-dark.png"); + m_preview->updatescale(); } }); @@ -211,6 +213,7 @@ void GlobalThemeWidget::initFilletWidget() QObject::connect(filletslider, &QSlider::valueChanged, [=](int value) { showButton->setText(QString::number(value)+"px"); + m_preview->updatescale(); m_preview->updateWidgetRadius(value); }); @@ -248,6 +251,7 @@ void GlobalThemeWidget::AccentColorWidget() qDebug() << "Selected color:" << selectedColor; qDebug() << "RGB values:" << selectedColor.red() << selectedColor.green() << selectedColor.blue(); + m_preview->updatescale(); m_preview->updateButtonColor(selectedColor); }); diff --git a/src/mainwindow.h b/src/mainwindow.h index 81e8994..458671a 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -35,9 +35,6 @@ class MainWindow : public QMainWindow public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); -// void mousePressEvent(QMouseEvent *event); -// void mouseMoveEvent(QMouseEvent *event); -// void mouseReleaseEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent * event);