#include "MyQUI.h" static const std::string QSS_GROUP = "QGroupBox { border: 1px solid gray; margin-top: 8px; border-radius: 5px;}" "QGroupBox::title { subcontrol-origin: margin; subcontrol-position: top left; left:10px; margin-left: 0px; padding:0 1px; }"; static const std::string QSS_BTN = "QPushButton {background:rgba(180,180,180,50);border-radius:2px;border:1px solid gray;color:white;}" "QPushButton:hover {background-color:rgb(32,164,128);}" "QPushButton:pressed {border-width:2px 0 0 2px;border-style:inset;}"; static const std::string QSS_BTN_ACTIVE = "QPushButton {background:rgba(80, 80, 80,80);border-radius:5px;border:1px solid green;color:green;font:bold 15px;}"; static const std::string QSS_BTN_MENU = "QPushButton {background:rgba(19, 35, 71,200);color:white;border-radius:5px;border:2px solid rgb(10,120,215);font:bold 18px;}" "QPushButton:hover {background-color:rgb(32,164,128);}" "QPushButton:pressed {border-width:3px 0 0 3px;border-style:inset;}" "QPushButton:disabled {color:rgb(150,150,150);}"; static const std::string QSS_BTN_MENU_ACTIVE = "QPushButton {background:rgba(32,164,128, 200);color:white;border-radius:5px;border:2px solid rgb(10,120,215);font:bold 18px;}" "QPushButton:hover {background-color:rgb(32,164,128);}" "QPushButton:pressed {border-width:3px 0 0 3px;border-style:inset;}" "QPushButton:disabled {color:rgb(150,150,150);}"; static const std::string QSS_BTN_TAB = "QPushButton {background:rgba(80,80,80,100);color:white;border-radius:3px;border:1px solid rgb(10,120,215);}" "QPushButton:hover {background-color:rgba(80,80,80,200);}"; //"QPushButton:pressed {border-width:3px 0 0 3px;border-style:inset;}" //"QPushButton:disabled {color:rgb(150,150,150);}"; static const std::string QSS_LINE = "QLineEdit { background-color:rgba(180,180,180,36); color:#ffffff; border:1px solid gray; border-radius:3px; font:bold 13px; }"; //14, 49, 66 static const std::string QSS_TABLE = // 表格整体样式 "QTableWidget {" " background-color: transparent;" // 背景色 " gridline-color: #C0C0C0;" // 网格线颜色 " border: 1px solid gray;" // 边框 " color: white;" // 文字颜色 "}" // 表头样式 "QHeaderView { background-color: rgba(150,150,150,50); }" "QHeaderView::section {" " background-color: rgba(120,120,120,0);" // 表头背景 " padding: 4px;" // 内边距 " border: 1px solid #505050;" // 边框 " min-height: 25px;" // 最小高度 "}" // 单元格样式 "QTableWidget::item {" " padding-left: 5px;" " border-bottom: 1px solid gray;" // 底部边框 "}" // 选中状态 "QTableWidget::item:selected {" " background-color: rgba(184, 214, 255, 50);" // 选中背景色 " color: rgb(220,220,220);" // 选中文字颜色 "}"; static const std::string QSS_BTN_COMBOX = "QComboBox { background-color: transparent; border: 1px solid rgb(18, 251, 255)}" "QListView::item { background-color: rgba(80,80,80,200); color: white; padding-left: 10px; }"; MyWidget::MyWidget(QWidget* parent) : QWidget(parent) { // 可以在这里设置样式表,也可以在其他地方设置 setObjectName("MyWidget"); setStyleSheet("#MyWidget {background-color:rgba(120,120,120,80); border-radius:5px;}"); // 确保自动填充背景 setAutoFillBackground(true); } shared_ptr MyQUI::Button(QWidget* parent, int x, int y, int w, int h, std::string title) { auto btn = std::make_shared(title.c_str(), parent); btn->setGeometry(x, y, w, h); btn->setStyleSheet(QSS_BTN.c_str()); return btn; } std::shared_ptr MyQUI::GroupBox(QWidget* parent, int x, int y, int w, int h, std::string title) { auto groupBox = std::make_shared(title.c_str(), parent); groupBox->setGeometry(x, y, w, h); groupBox->setStyleSheet(QSS_GROUP.c_str()); return groupBox; } MyPairLabelLine MyQUI::PairLine(QWidget* parent, int x, int y, string k, string v, bool readonly/* = true*/) { shared_ptr key = make_shared(parent); key->setText(k.c_str()); key->setGeometry(x, y, 80, 26); shared_ptr value = make_shared(parent); value->setText(v.c_str()); value->setGeometry(x+80, y, 260, 26); value->setStyleSheet(QSS_LINE.c_str()); value->setReadOnly(readonly); return {key, value}; } std::shared_ptr MyQUI::TableWidget(QWidget* parent, int x, int y, int w, int h) { auto table = std::make_shared(parent); table->setGeometry(x, y, w, h); table->setStyleSheet(QSS_TABLE.c_str()); table->horizontalHeader()->setStretchLastSection(true); // 最后一列占满 table->verticalHeader()->setVisible(false); // 不显示垂直表头 table->setEditTriggers(QAbstractItemView::NoEditTriggers); // 单元格不可编辑 table->setSelectionMode(QAbstractItemView::SingleSelection); // 设置为单选模式 table->setSelectionBehavior(QAbstractItemView::SelectRows); // 设置为整行选中 table->horizontalHeader()->setFixedHeight(50); table->horizontalHeader()->setDefaultSectionSize(60); return table; } void MyQUI::setTableCell(std::shared_ptr table, int row, int col, std::string text, std::string style /*= ""*/) { if (row >= table->rowCount()) { table->insertRow(row); } auto item = table->item(row, col); if (!item) { item = new QTableWidgetItem(); table->setItem(row, col, item); } item->setText(text.c_str()); if (style == "OK") { item->setForeground(QBrush(Qt::green)); } else if (style == "ERR") { item->setForeground(QBrush(Qt::red)); } else if (style == "WARN") { item->setForeground(QBrush(Qt::cyan)); } }