fix update showwidget radius

This commit is contained in:
likehomedream 2023-09-05 14:44:53 +08:00
parent 5040cc5eb7
commit a8ebb5f89b
4 changed files with 40 additions and 20 deletions

View File

@ -47,15 +47,7 @@ void GlobalImageWidget::updateButtonColor(const QColor& color) {
void GlobalImageWidget::updateWidgetBackgroundColor(const QColor& color) void GlobalImageWidget::updateWidgetBackgroundColor(const QColor& color)
{ {
if (!m_showwidget) { m_showwidget->setBackgroundColor(color);
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);
}
} }
@ -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) GlobalImageWidget::GlobalImageWidget(QWidget *parent, const QString& coverFilePath)
: QWidget(parent), m_coverFilePath(coverFilePath) { : QWidget(parent), m_coverFilePath(coverFilePath) {
@ -165,6 +165,8 @@ Globalthemefeature::Globalthemefeature(QWidget *parent, const QString& coverFile
RoundedWidget::RoundedWidget(QWidget *parent) RoundedWidget::RoundedWidget(QWidget *parent)
: QWidget(parent), m_radius(0) : QWidget(parent), m_radius(0)
{ {
setAttribute(Qt::WA_TranslucentBackground);
setRadius(6);
} }
void RoundedWidget::setRadius(int radius) void RoundedWidget::setRadius(int radius)
@ -172,18 +174,30 @@ void RoundedWidget::setRadius(int radius)
m_radius = radius; m_radius = radius;
} }
void RoundedWidget::setBackgroundColor(const QColor &color)
{
backgroundColor = color;
update();
}
void RoundedWidget::paintEvent(QPaintEvent *event) void RoundedWidget::paintEvent(QPaintEvent *event)
{ {
Q_UNUSED(event)
QPainter painter(this); QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing); painter.setRenderHint(QPainter::Antialiasing, true); // 开启抗锯齿
painter.setPen(Qt::black);
painter.setBrush(Qt::NoBrush); // 设置填充为无色
const QRectF rect = contentsRect().adjusted(1, 1, -1, -1); QRectF rect = contentsRect().adjusted(borderWidth, borderWidth, -borderWidth, -borderWidth);
QPainterPath path; int r = std::min(rect.width(), rect.height()) / 2; // 计算圆角半径
path.addRoundedRect(rect, m_radius, m_radius); 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);
} }

View File

@ -22,12 +22,16 @@ public:
RoundedWidget(QWidget *parent = nullptr); RoundedWidget(QWidget *parent = nullptr);
void setRadius(int radius); void setRadius(int radius);
void setBackgroundColor(const QColor& color);
protected: protected:
void paintEvent(QPaintEvent *event) override; void paintEvent(QPaintEvent *event) override;
private: private:
int m_radius; int m_radius;
QColor backgroundColor = Qt::white; // 背景颜色
QColor borderColor = Qt::white; // 边框颜色
int borderWidth = 1; // 边框宽度
}; };
class CustomGraphicsView : public QGraphicsView { class CustomGraphicsView : public QGraphicsView {
@ -46,6 +50,7 @@ public:
void updateWidgetBackgroundColor(const QColor& color); void updateWidgetBackgroundColor(const QColor& color);
void update(const QString& coverFilePath); void update(const QString& coverFilePath);
void updateOverlayImage(const QString& overlayImagePath); void updateOverlayImage(const QString& overlayImagePath);
void updatescale();
private: private:
CustomGraphicsView* graphicsView; CustomGraphicsView* graphicsView;
QString m_coverFilePath; QString m_coverFilePath;

View File

@ -169,11 +169,13 @@ void GlobalThemeWidget::initExteriorWidget()
m_preview->updateWidgetBackgroundColor(selectedColor); m_preview->updateWidgetBackgroundColor(selectedColor);
m_preview->updateOverlayImage(":/resource/background/panel-light.png"); m_preview->updateOverlayImage(":/resource/background/panel-light.png");
m_preview->updatescale();
}else if(index == 1){ }else if(index == 1){
QColor selectedColor = combobox->itemData(index).value<QColor>(); QColor selectedColor = combobox->itemData(index).value<QColor>();
m_preview->updateWidgetBackgroundColor(selectedColor); m_preview->updateWidgetBackgroundColor(selectedColor);
m_preview->updateOverlayImage(":/resource/background/panel-dark.png"); 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) { QObject::connect(filletslider, &QSlider::valueChanged, [=](int value) {
showButton->setText(QString::number(value)+"px"); showButton->setText(QString::number(value)+"px");
m_preview->updatescale();
m_preview->updateWidgetRadius(value); m_preview->updateWidgetRadius(value);
}); });
@ -248,6 +251,7 @@ void GlobalThemeWidget::AccentColorWidget()
qDebug() << "Selected color:" << selectedColor; qDebug() << "Selected color:" << selectedColor;
qDebug() << "RGB values:" << selectedColor.red() << selectedColor.green() << selectedColor.blue(); qDebug() << "RGB values:" << selectedColor.red() << selectedColor.green() << selectedColor.blue();
m_preview->updatescale();
m_preview->updateButtonColor(selectedColor); m_preview->updateButtonColor(selectedColor);
}); });

View File

@ -35,9 +35,6 @@ class MainWindow : public QMainWindow
public: public:
MainWindow(QWidget *parent = nullptr); MainWindow(QWidget *parent = nullptr);
~MainWindow(); ~MainWindow();
// void mousePressEvent(QMouseEvent *event);
// void mouseMoveEvent(QMouseEvent *event);
// void mouseReleaseEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent * event); void mouseMoveEvent(QMouseEvent * event);