#include #include #include #include #include #include #include class plcValueViewer :public QWidget{ public: QFont font1 = QFont("微软雅黑", 12, QFont::Bold); QFont font2 = QFont("微软雅黑", 10, QFont::Bold); QFont font3 = QFont("微软雅黑", 6, QFont::Bold); plcValueViewer(const QString& groupNameIN, const std::vector>& viewerNameAndAddress, QMap>& plcViwerLineedits) :groupName(groupNameIN){ QLabel* groupNameLabel = new QLabel(groupNameIN); groupNameLabel->setFont(font1); groupNameLabel->setStyleSheet("color: white;"); //groupNameLabel->setPalette(palette); layout->addWidget(groupNameLabel); for (auto& data : viewerNameAndAddress) { plcViwerLineedits[data.second].push_back(addViewer(data.first)); } widget->setMaximumHeight(800); } QLabel* addViewer(const QString& viewerName) { //QPalette palette; //palette.setColor(QPalette::WindowText, Qt::white); map.insert(groupName + viewerName ,new QLabel("*")); auto label = map[groupName + viewerName]; label->setFont(font2); label->setStyleSheet("color: white;"); //label->setPalette(palette); auto textLabel = new QLabel(viewerName); textLabel->setFont(font3); textLabel->setStyleSheet("color: white;"); //textLabel->setPalette(palette); QVBoxLayout* viewerLayout = new QVBoxLayout(); viewerLayout->addWidget(label,Qt::AlignCenter); viewerLayout->addWidget(textLabel, Qt::AlignCenter); layout->addLayout(viewerLayout); return label; } QString groupName = ""; QWidget* widget = new QWidget(); QHBoxLayout* layout = new QHBoxLayout(widget); QMap map; }; class runCommandWidget : public QDialog { Q_OBJECT public: explicit runCommandWidget(QWidget* parent = nullptr) : QDialog(parent) { this->setWindowTitle(TR("Execute Command")); this->setObjectName("runCommandWidget"); this->setStyleSheet(QString("QDialog#runCommandWidget {background-color: rgb(%1);border: 2px solid #000000;border-radius: 6px;}").arg(sColor_Blue)); btnWidgets = new QListWidget(this); btnWidgets->setStyleSheet("background: transparent;"); // 使用 QListView 包裹 plcValueViewer plcValueViewerList = new QListView(this); plcValueViewerList->setStyleSheet("background: transparent;"); plcValueViewerList->setEditTriggers(QAbstractItemView::NoEditTriggers); plcValueViewerList->setSelectionMode(QAbstractItemView::NoSelection); plcValueViewerList->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel); plcValueViewerList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); plcValueViewerList->setResizeMode(QListView::Adjust); plcValueViewerModel = new QStandardItemModel(plcValueViewerList); plcValueViewerList->setModel(plcValueViewerModel); QGridLayout* layout = new QGridLayout(this); layout->addWidget(btnWidgets, 0, 0); layout->addWidget(plcValueViewerList, 0, 1); this->setFixedSize(sizeX, sizeY); } signals: void excuteCommandPreset(QString uuid); public slots: void addBtn(const QString& btnName, const QString& tip, const QString& uuid) { QListWidgetItem* item = new QListWidgetItem(btnWidgets); btnWidgets->addItem(item); fancyButtonText* funcBtn = new fancyButtonText(btnName,0,0, tip); btnWidgets->setItemWidget(item, funcBtn); connect(funcBtn, &fancyButtonText::clicked, this, [this, uuid]() { QMessageBox::StandardButton reply; reply = QMessageBox::question( this, // parent TR("Confirm"), // title TR("Are you sure you want to execute this action?"), // content QMessageBox::Yes | QMessageBox::No // buttons ); if (reply == QMessageBox::Yes) { emit excuteCommandPreset(uuid); } }); } void addPlcValueViewer(QString groupName, std::vector> viewerNameAndAddress) { auto viewer = new plcValueViewer(groupName, viewerNameAndAddress, plcViwerLineedits); // 添加到 QListView 中 QStandardItem* item = new QStandardItem(); // 根据内部 widget 的 sizeHint 设置行高 item->setSizeHint(viewer->widget->sizeHint()); plcValueViewerModel->appendRow(item); QModelIndex index = plcValueViewerModel->index(plcValueViewerModel->rowCount() - 1, 0); plcValueViewerList->setIndexWidget(index, viewer->widget); } void refreshPlcValueViewer(int address, QString value) { for (auto& lineedit : plcViwerLineedits[address]) { lineedit->setText(value); } } protected: //重写showEvent,使窗口显示在屏幕中心 void showEvent(QShowEvent* event) override { QWidget::showEvent(event); //获取屏幕的几何信息 QScreen* screen = QGuiApplication::primaryScreen(); QRect screenGeometry = screen->geometry(); int x = (screenGeometry.width() - this->width()) / 2; int y = (screenGeometry.height() - this->height()) / 2; this->move(x, y); } private: int sizeX = 1000, sizeY = 720; QListWidget* btnWidgets; // 用 QListView + 模型 替代原来的垂直布局 QListView* plcValueViewerList; QStandardItemModel* plcValueViewerModel; QMap>plcViwerLineedits; }; #pragma once