企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] ## 封装弹窗 封装基类 ``` class CustomDialog : public QDialog { public: explicit CustomDialog(QWidget *parent = nullptr) : QDialog(parent) { setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint); setAttribute(Qt::WA_TranslucentBackground); setupUI(); } void setTitle(const QString &title) { m_titleLabel->setText(title); } void setBody(QLayout *bodyWidget) { m_bodyWidget = bodyWidget; m_mainLayout->addLayout(m_bodyWidget); } protected: void paintEvent(QPaintEvent *event) override { QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); // 绘制阴影 QColor shadowColor(0, 0, 0, 10); for (int i = 0; i < 5; i++) { painter.setPen(Qt::NoPen); painter.setBrush(shadowColor); painter.drawRoundedRect(5 + i, 5 + i, width() - 10 - i * 3, height() - 10 - i * 3, 15, 15); shadowColor.setAlpha(shadowColor.alpha() - 1); } // 绘制主体 painter.setPen(Qt::NoPen); painter.setBrush(QColor(255, 255, 255)); painter.drawRoundedRect(10, 10, width() - 20, height() - 20, 15, 15); QDialog::paintEvent(event); } private: QLabel *m_titleLabel; QLayout *m_bodyWidget; QVBoxLayout *m_mainLayout; QPushButton *m_closeButton; void setupUI() { m_mainLayout = new QVBoxLayout(this); m_mainLayout->setContentsMargins(15, 15, 15, 15); m_titleLabel = new QLabel(this); m_titleLabel->setAlignment(Qt::AlignCenter); m_titleLabel->setStyleSheet("font-weight: bold; font-size: 16px; color: #333;"); m_closeButton = new QPushButton("×", this); m_closeButton->setFixedSize(30, 30); m_closeButton->setStyleSheet("QPushButton { border: none; font-size: 20px; color: #666; }" "QPushButton:hover { color: #f00; }"); QHBoxLayout *titleLayout = new QHBoxLayout(); titleLayout->addWidget(m_titleLabel); titleLayout->addWidget(m_closeButton); m_mainLayout->addLayout(titleLayout); connect(m_closeButton, &QPushButton::clicked, this, &QDialog::accept); setFixedSize(400, 300); // 默认大小,可以根据需要调整 } }; ``` 自定义弹窗 ``` class DemoDialog : public CustomDialog{ public: DemoDialog(QWidget *parent = nullptr):CustomDialog(parent) { this->setTitle(QString("adasdad title")) ; this->setBody(this->handleBody()) ; } QLayout * handleBody(){ QVBoxLayout * layout = new QVBoxLayout() ; layout->addWidget(new QLabel("adasdad body")) ; return layout; } }; ``` 使用弹窗 ``` DemoDialog dialog; dialog.exec(); ```