added stateLabel
This commit is contained in:
@@ -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<QModbusReply *>(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();
|
||||
|
||||
Reference in New Issue
Block a user