#pragma execution_character_set("utf-8") #include "frmmain.h" #include "ui_frmmain.h" #include "iconhelper.h" #include "quihelper.h" frmMain::frmMain(QWidget *parent) : QDialog(parent), ui(new Ui::frmMain) { ui->setupUi(this); this->initForm(); this->initNav(); QTimer::singleShot(0, this, SLOT(initImage())); } frmMain::~frmMain() { delete ui; } bool frmMain::eventFilter(QObject *obj, QEvent *event) { if (obj == ui->widgetTitle) { if (event->type() == QEvent::MouseButtonDblClick) { on_btnMenu_Max_clicked(); } } return QDialog::eventFilter(obj, event); } void frmMain::initStyle(const QString &normalColor, const QString &darkColor, const QString &textColor) { QStringList qss; //边框 qss.append(QString("QDialog#frmMain{border:1px solid %1;}").arg(normalColor)); //标题栏背景颜色+流线型背景 qss.append(QString("QWidget#widgetTitle{background:%1;}").arg(normalColor)); qss.append("QWidget#widgetTitle>QWidget#widgetMenu>QAbstractButton{border-radius:0px;}"); //菜单按钮 qss.append("QToolButton#btnMenu,QPushButton#btnMenu_Min,QPushButton#btnMenu_Max,QPushButton#btnMenu_Close{"); qss.append("border-radius:3px;color:#F0F0F0;padding:3px;margin:0px;background:none;border-style:none;}"); qss.append("QToolButton#btnMenu:hover,QPushButton#btnMenu_Min:hover,QPushButton#btnMenu_Max:hover{"); qss.append("color:#FFFFFF;margin:1px 1px 2px 1px;background-color:rgba(51,127,209,130);}"); qss.append("QPushButton#btnMenu_Close:hover{color:#FFFFFF;margin:1px 1px 2px 1px;background-color:rgba(238,0,0,128);}"); //主界面顶部标题字体+文字颜色 qss.append("QLabel#labNavTitle{font:23px;font-weight:bold;color:#FFFFFF;}"); this->setPalette(Qt::white); this->setStyleSheet(qss.join("")); //设置导航按钮颜色 QColor normalBgColor = QColor(normalColor); QColor hoverBgColor = darkColor; QColor checkBgColor = darkColor; QColor normalTextColor = QColor(textColor); QColor hoverTextColor = QColor(textColor); QColor checkTextColor = QColor(textColor); for (int i = 0; i < btns.count(); i++) { int icon = icons.at(i); QPixmap iconNormal = IconHelper::getPixmap(normalTextColor.name(), icon, 25, 30, 30); QPixmap iconHover = IconHelper::getPixmap(hoverTextColor.name(), icon, 25, 30, 30); QPixmap iconCheck = IconHelper::getPixmap(checkTextColor.name(), icon, 25, 30, 30); btns.at(i)->setIconNormal(iconNormal); btns.at(i)->setIconHover(iconHover); btns.at(i)->setIconCheck(iconCheck); btns.at(i)->setNormalBgColor(normalBgColor); btns.at(i)->setHoverBgColor(hoverBgColor); btns.at(i)->setCheckBgColor(checkBgColor); btns.at(i)->setNormalTextColor(normalTextColor); btns.at(i)->setHoverTextColor(hoverTextColor); btns.at(i)->setCheckTextColor(checkTextColor); } } void frmMain::initForm() { ui->labLogo->setFixedWidth(120); ui->widgetMenu->setFixedWidth(130); ui->widgetTitle->setFixedHeight(40); //ui->labNavTitle->setText("导航按钮示例"); ui->widgetTitle->installEventFilter(this); //设置无边框 QUIHelper::setFramelessForm(this); this->setWindowTitle(ui->labNavTitle->text()); QList btns = ui->widget->findChildren(); foreach (QPushButton *btn, btns) { connect(btn, SIGNAL(clicked(bool)), this, SLOT(btnClicked())); } } void frmMain::initNav() { //从图形字体获得图片,也可以从资源文件或者路径文件获取 icons << 0xf03e << 0xf03d << 0xf272 << 0xf085; btns << ui->btn1 << ui->btn2 << ui->btn3 << ui->btn4; QFont font; font.setPixelSize(15); font.setBold(true); for (int i = 0; i < btns.count(); i++) { btns.at(i)->setFont(font); btns.at(i)->setPaddingLeft(25); btns.at(i)->setLineSpace(0); btns.at(i)->setShowLine(false); btns.at(i)->setShowTriangle(true); btns.at(i)->setTextAlign(NavButton::TextAlign_Center); btns.at(i)->setTrianglePosition(NavButton::TrianglePosition_Bottom); btns.at(i)->setLinePosition(NavButton::LinePosition_Top); btns.at(i)->setShowIcon(true); btns.at(i)->setIconSpace(10); btns.at(i)->setIconSize(QSize(20, 20)); btns.at(i)->setMinimumWidth(110); connect(btns.at(i), SIGNAL(clicked(bool)), this, SLOT(buttonClick())); } ui->btn1->setChecked(true); ui->btnColor1->click(); } void frmMain::initImage() { //左上角logo QString bgLogo = ":/image/logo.png"; if (QFile(bgLogo).exists()) { QPixmap pix(bgLogo); pix = pix.scaled(ui->labLogo->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); ui->labLogo->setPixmap(pix); } } void frmMain::buttonClick() { NavButton *btn = (NavButton *)sender(); for (int i = 0; i < btns.count(); i++) { btns.at(i)->setChecked(btns.at(i) == btn); } ui->stackedWidget->setCurrentIndex(btns.indexOf(btn)); } void frmMain::btnClicked() { QPushButton *btn = (QPushButton *)sender(); QString text = btn->text(); if (text == "蓝色") { this->initStyle("#3985BF", "#4897D1", "#FFFFFF"); } else if (text == "黑色") { this->initStyle("#2D3E50", "#34495E", "#FFFFFF"); } else if (text == "灰色") { this->initStyle("#7E8C8D", "#95A5A5", "#FFFFFF"); } else if (text == "红色") { this->initStyle("#DB5D5E", "#F65066", "#FFFFFF"); } else if (text == "褐色") { this->initStyle("#1D5973", "#56BEC1", "#FFFFFF"); } else if (text == "绿色") { this->initStyle("#005E36", "#24A562", "#FFFFFF"); } else if (text == "黄色") { this->initStyle("#F39C11", "#E67F22", "#FFFFFF"); } else if (text == "紫色") { this->initStyle("#8A8CB3", "#A87EA6", "#FFFFFF"); } else if (text == "紫红色") { this->initStyle("#8D44AF", "#9A59B5", "#FFFFFF"); } else if (text == "深绿色") { this->initStyle("#009679", "#17A086", "#FFFFFF"); } else if (text == "深蓝色") { this->initStyle("#22A3A9", "#0E99A0", "#FFFFFF"); } else if (text == "咖啡色") { this->initStyle("#614736", "#64391A", "#FFFFFF"); } } void frmMain::on_btnMenu_Min_clicked() { this->showMinimized(); } void frmMain::on_btnMenu_Max_clicked() { static bool max = false; static QRect location = this->geometry(); if (max) { this->setGeometry(location); } else { location = this->geometry(); this->setGeometry(QUIHelper::getScreenRect()); } this->setProperty("canMove", max); max = !max; } void frmMain::on_btnMenu_Close_clicked() { exit(0); }