added stateLabel

This commit is contained in:
2025-07-04 02:57:55 +07:00
parent 96954c6527
commit bdd09484f4
2 changed files with 37 additions and 4 deletions

View File

@@ -67,6 +67,10 @@ private:
int m_connectTimeout; int m_connectTimeout;
int m_pollInterval; int m_pollInterval;
QTimer* m_pollTimer; QTimer* m_pollTimer;
QLabel* m_statusLabel;
int m_requestCounter;
int m_responseCounter;
void updateStatusBar();
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View File

@@ -27,7 +27,8 @@ void ColoredSquare::paintEvent(QPaintEvent *) {
} }
Mainwindows::Mainwindows(QWidget *parent) 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")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
@@ -137,12 +138,16 @@ void Mainwindows::onStateChanged(QModbusDevice::State state) {
if (state == QModbusDevice::ConnectedState) { if (state == QModbusDevice::ConnectedState) {
m_connected = true; m_connected = true;
m_connectButton->setText("Disconnect"); m_connectButton->setText("Disconnect");
m_requestCounter = 0;
m_responseCounter = 0;
updateStatusBar();
readRegisters(); readRegisters();
m_pollTimer->start(); // Start polling when connected m_pollTimer->start(); // Start polling when connected
} else if (state == QModbusDevice::UnconnectedState) { } else if (state == QModbusDevice::UnconnectedState) {
m_connected = false; m_connected = false;
m_connectButton->setText("Connect"); m_connectButton->setText("Connect");
m_pollTimer->stop(); // Stop polling when disconnected m_pollTimer->stop(); // Stop polling when disconnected
updateStatusBar();
} }
} }
@@ -174,6 +179,10 @@ void Mainwindows::onPollTimer() {
void Mainwindows::readRegisters() { void Mainwindows::readRegisters() {
if (!m_connected) return; if (!m_connected) return;
// Увеличиваем счетчик запросов
m_requestCounter += m_indicatorCount + m_buttonCount;
updateStatusBar();
// Read indicator registers // Read indicator registers
for (int i = 0; i < m_indicatorCount; ++i) { for (int i = 0; i < m_indicatorCount; ++i) {
if (i < m_indicatorRegisters.size()) { if (i < m_indicatorRegisters.size()) {
@@ -207,7 +216,10 @@ void Mainwindows::onReadReady() {
auto reply = qobject_cast<QModbusReply *>(sender()); auto reply = qobject_cast<QModbusReply *>(sender());
if (!reply) return; if (!reply) return;
// Увеличиваем счетчик ответов при успешном чтении
if (reply->error() == QModbusDevice::NoError) { if (reply->error() == QModbusDevice::NoError) {
m_responseCounter++;
updateStatusBar();
const QModbusDataUnit unit = reply->result(); const QModbusDataUnit unit = reply->result();
int address = unit.startAddress(); int address = unit.startAddress();
int value = unit.value(0); int value = unit.value(0);
@@ -242,6 +254,11 @@ void Mainwindows::onReadReady() {
void Mainwindows::createUIElements() { void Mainwindows::createUIElements() {
auto* mainLayout = new QVBoxLayout(this); auto* mainLayout = new QVBoxLayout(this);
// Добавляем статусную строку
m_statusLabel = new QLabel(this);
m_statusLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);
updateStatusBar();
// Add connect button at the top // Add connect button at the top
m_connectButton = new QPushButton("Connect", this); m_connectButton = new QPushButton("Connect", this);
connect(m_connectButton, &QPushButton::clicked, this, &Mainwindows::onConnectButtonClicked); connect(m_connectButton, &QPushButton::clicked, this, &Mainwindows::onConnectButtonClicked);
@@ -319,14 +336,26 @@ void Mainwindows::createUIElements() {
m_table->setColumnWidth(3, 100); // Status text column m_table->setColumnWidth(3, 100); // Status text column
mainLayout->addWidget(m_table); mainLayout->addWidget(m_table);
mainLayout->addWidget(m_statusLabel);
mainLayout->setContentsMargins(0, 0, 0, 0); mainLayout->setContentsMargins(0, 0, 0, 0);
// Устанавливаем фиксированный размер окна на основе размера таблицы // Обновляем расчет размера окна с учетом статусной строки
int totalWidth = m_table->horizontalHeader()->length() + 2; // +2 для рамки int totalWidth = m_table->horizontalHeader()->length() + 20;
int totalHeight = m_table->verticalHeader()->length() + m_connectButton->height() + 2; int totalHeight = m_table->verticalHeader()->length() +
m_connectButton->height() +
m_statusLabel->sizeHint().height() + 20;
setFixedSize(totalWidth, totalHeight); 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() { Mainwindows::~Mainwindows() {
if (m_pollTimer) { if (m_pollTimer) {
m_pollTimer->stop(); m_pollTimer->stop();