From bdd09484f44bf7353f8f591882832f0f25118f9b Mon Sep 17 00:00:00 2001 From: Alekseev Date: Fri, 4 Jul 2025 02:57:55 +0700 Subject: [PATCH] added stateLabel --- Inc/mainwindow.h | 4 ++++ Src/mainwindow.cpp | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/Inc/mainwindow.h b/Inc/mainwindow.h index 0de4794..a3e971c 100644 --- a/Inc/mainwindow.h +++ b/Inc/mainwindow.h @@ -67,6 +67,10 @@ private: int m_connectTimeout; int m_pollInterval; QTimer* m_pollTimer; + QLabel* m_statusLabel; + int m_requestCounter; + int m_responseCounter; + void updateStatusBar(); }; #endif // MAINWINDOW_H diff --git a/Src/mainwindow.cpp b/Src/mainwindow.cpp index ef204d1..0aeb250 100644 --- a/Src/mainwindow.cpp +++ b/Src/mainwindow.cpp @@ -27,7 +27,8 @@ void ColoredSquare::paintEvent(QPaintEvent *) { } Mainwindows::Mainwindows(QWidget *parent) - : QWidget(parent), m_modbusClient(nullptr), m_connected(false) { + : QWidget(parent), m_modbusClient(nullptr), m_connected(false), + m_requestCounter(0), m_responseCounter(0) { // Устанавливаем кодировку для всего приложения QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); @@ -137,12 +138,16 @@ void Mainwindows::onStateChanged(QModbusDevice::State state) { if (state == QModbusDevice::ConnectedState) { m_connected = true; m_connectButton->setText("Disconnect"); + m_requestCounter = 0; + m_responseCounter = 0; + updateStatusBar(); readRegisters(); m_pollTimer->start(); // Start polling when connected } else if (state == QModbusDevice::UnconnectedState) { m_connected = false; m_connectButton->setText("Connect"); m_pollTimer->stop(); // Stop polling when disconnected + updateStatusBar(); } } @@ -174,6 +179,10 @@ void Mainwindows::onPollTimer() { void Mainwindows::readRegisters() { if (!m_connected) return; + // Увеличиваем счетчик запросов + m_requestCounter += m_indicatorCount + m_buttonCount; + updateStatusBar(); + // Read indicator registers for (int i = 0; i < m_indicatorCount; ++i) { if (i < m_indicatorRegisters.size()) { @@ -207,7 +216,10 @@ void Mainwindows::onReadReady() { auto reply = qobject_cast(sender()); if (!reply) return; + // Увеличиваем счетчик ответов при успешном чтении if (reply->error() == QModbusDevice::NoError) { + m_responseCounter++; + updateStatusBar(); const QModbusDataUnit unit = reply->result(); int address = unit.startAddress(); int value = unit.value(0); @@ -242,6 +254,11 @@ void Mainwindows::onReadReady() { void Mainwindows::createUIElements() { auto* mainLayout = new QVBoxLayout(this); + // Добавляем статусную строку + m_statusLabel = new QLabel(this); + m_statusLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken); + updateStatusBar(); + // Add connect button at the top m_connectButton = new QPushButton("Connect", this); connect(m_connectButton, &QPushButton::clicked, this, &Mainwindows::onConnectButtonClicked); @@ -319,14 +336,26 @@ void Mainwindows::createUIElements() { m_table->setColumnWidth(3, 100); // Status text column mainLayout->addWidget(m_table); + mainLayout->addWidget(m_statusLabel); mainLayout->setContentsMargins(0, 0, 0, 0); - // Устанавливаем фиксированный размер окна на основе размера таблицы - int totalWidth = m_table->horizontalHeader()->length() + 2; // +2 для рамки - int totalHeight = m_table->verticalHeader()->length() + m_connectButton->height() + 2; + // Обновляем расчет размера окна с учетом статусной строки + int totalWidth = m_table->horizontalHeader()->length() + 20; + int totalHeight = m_table->verticalHeader()->length() + + m_connectButton->height() + + m_statusLabel->sizeHint().height() + 20; setFixedSize(totalWidth, totalHeight); } +void Mainwindows::updateStatusBar() { + QString status = QString("IP: %1 Port: %2 | Requests: %3 Responses: %4") + .arg(m_ipAddress) + .arg(m_port) + .arg(m_requestCounter) + .arg(m_responseCounter); + m_statusLabel->setText(status); +} + Mainwindows::~Mainwindows() { if (m_pollTimer) { m_pollTimer->stop();