#include #include #include #include #include #include #include #include class fancyWarnWindow :public QDialog { public: QLabel* detailLabel; enum type { Warning, tip }; QPushButton* closeBtn = new QPushButton(this); fancyButtonText* continueBtn = new fancyButtonText(120,60,"继续","继续"); fancyButtonText* cancelBtn = new fancyButtonText(120, 60, "取消", "取消"); fancyWarnWindow(type typeIn,QString title,QString detail,bool Show) { QPalette paletteWhite, paletteRed, paletteGreen; paletteWhite.setColor(QPalette::WindowText, Qt::white); paletteRed.setColor(QPalette::WindowText, Qt::red); paletteGreen.setColor(QPalette::WindowText, Qt::green); QFont font("微软雅黑", 16, QFont::Bold); QFont font2("微软雅黑", 18, QFont::Bold); QFont font3("微软雅黑", 12, QFont::Bold); this->setObjectName("warnDialog"); this->setStyleSheet(QString("QDialog#warnDialog {" "background-color: rgb(%1);" "border: 1px solid black;" "border-radius: 1px;}").arg(sColor_blue)); this->setWindowFlags(Qt::FramelessWindowHint); // 设置无边框的样式 this->setFixedSize(QSize(480, 320)); QGridLayout* layout = new QGridLayout(this); layout->setContentsMargins(0, 0, 0, 0); QWidget* menuBarWidget = new QWidget();//第一层QWidget menuBarWidget->setStyleSheet(QString("QWidget { background-color: black;" "border: 0px solid red;" // 显式设置边框 "border-radius: 0px;" // 设置边框半径为0,确保没有圆角 "}")); menuBarWidget->setFixedHeight(35); menuBarWidget->setFixedWidth(480); QHBoxLayout* menuBarLayout = new QHBoxLayout(menuBarWidget); menuBarLayout->setContentsMargins(0, 0, 0, 0); QHBoxLayout* menuBarLayoutLeft = new QHBoxLayout(); menuBarLayoutLeft->setContentsMargins(0, 0, 0, 0); menuBarLayoutLeft->setAlignment(Qt::AlignLeft); QHBoxLayout* menuBarLayoutRight = new QHBoxLayout(); menuBarLayoutRight->setContentsMargins(0, 0, 0, 0); menuBarLayoutRight->setAlignment(Qt::AlignRight); menuBarLayout->addLayout(menuBarLayoutLeft); menuBarLayout->addLayout(menuBarLayoutRight); QLabel* warnLabel = new QLabel(); warnLabel->setAlignment(Qt::AlignLeft); warnLabel->setFont(font); switch (typeIn) { case Warning: warnLabel->setText("警告"); warnLabel->setPalette(paletteRed); break; case tip: warnLabel->setText("提示"); warnLabel->setPalette(paletteGreen); break; } menuBarLayoutLeft->addWidget(warnLabel); closeBtn->setIcon(QPixmap(":/topBar/icon/close.png")); // 设置图标 closeBtn->setFixedHeight(35); closeBtn->setStyleSheet(QString("QPushButton { background-color: transparent;" "border: 0px solid red;" // 显式设置边框 "border-radius: 0px;" // 设置边框半径为0,确保没有圆角 "}" "QPushButton:focus {" " outline: none;" "}" "QPushButton:hover { background-color: rgb(%1); }").arg(sColor_Red)); // 设置透明背景和悬停时的背景色 closeBtn->setFixedHeight(35); closeBtn->setFixedWidth(50); menuBarLayoutRight->addWidget(closeBtn); QObject::connect(closeBtn, &QPushButton::clicked, this, &QDialog::close); layout->addWidget(menuBarWidget, 0, 0); QLabel* titleLabel = new QLabel(title); titleLabel->setFont(font2); titleLabel->setPalette(paletteWhite); titleLabel->setAlignment(Qt::AlignCenter); titleLabel->setWordWrap(true); layout->addWidget(titleLabel, 1, 0); detailLabel = new QLabel(detail); detailLabel->setFont(font3); detailLabel->setPalette(paletteWhite); detailLabel->setAlignment(Qt::AlignCenter); detailLabel->setWordWrap(true); layout->addWidget(detailLabel, 2, 0); QHBoxLayout* funcBtnLayout = new QHBoxLayout(); funcBtnLayout->addWidget(continueBtn, 3, 0); funcBtnLayout->addWidget(cancelBtn, 3, 0); layout->addLayout(funcBtnLayout, 3, 0); QObject::connect(continueBtn, &QPushButton::clicked, this, &QDialog::close); QObject::connect(cancelBtn, &QPushButton::clicked, this, &QDialog::close); this->setWindowFlags(this->windowFlags() | Qt::WindowStaysOnTopHint); if (Show == true) this->show(); else this->hide(); } void setDetailText(QString text) { detailLabel->setText(text); } void disableHideAfterClicked() { disconnect(continueBtn, &QPushButton::clicked, this, &QDialog::close); } private: bool isMoving = false; //窗口移动 #pragma region 窗口移动变量 QPoint startPos; QRect startFrameGeometry; QTime lastWindowMoveTime; int freshDelay = 1000 / windowMoveFreshRate; bool setNormalAndMove = false;//判断是否从最大化开始移动 #pragma endregion protected: void mousePressEvent(QMouseEvent* event) override { if (event->pos().y() > 35) return; if (event->button() == Qt::LeftButton) { startPos = event->globalPos(); startFrameGeometry = frameGeometry(); isMoving = true; lastWindowMoveTime.start(); // 按下时开始计时 } QDialog::mousePressEvent(event); } void mouseMoveEvent(QMouseEvent* event) override { if (event->buttons() == Qt::LeftButton && isMoving) { QPoint pos = event->globalPos(); if (std::max(std::abs(pos.x() - startPos.x()), std::abs(pos.y() - startPos.y())) > 10) if (lastWindowMoveTime.elapsed() > freshDelay)//每秒刷新100次 { lastWindowMoveTime.restart(); move(startFrameGeometry.topLeft() + (event->globalPos() - startPos)); } } QDialog::mouseMoveEvent(event); } void mouseReleaseEvent(QMouseEvent* event) override { if (event->button() == Qt::LeftButton) { isMoving = false; } QDialog::mouseReleaseEvent(event); } }; #pragma once