Files
energy_storage/src/qt/MyQUI.cpp

126 lines
5.2 KiB
C++
Raw Normal View History

#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(80, 80, 80,80);border-radius:5px;border:1px solid gray;color:white;font:bold 13px;}";
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: rgb(14, 49, 66); color: #ffffff; border: 1px solid gray; border-radius: 5px; font: bold 13px; }";
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);
}
std::shared_ptr<QGroupBox> MyQUI::GroupBox(QWidget* parent, int x, int y, int w, int h, std::string title)
{
auto groupBox = std::make_shared<QGroupBox>(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<QLabel> key = make_shared<QLabel>(parent);
key->setText(k.c_str());
key->setGeometry(x, y, 80, 26);
shared_ptr<QLineEdit> value = make_shared<QLineEdit>(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<QTableWidget> MyQUI::TableWidget(QWidget* parent, int x, int y, int w, int h)
{
auto table = std::make_shared<QTableWidget>(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<QTableWidget> 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)); }
}