Write values dialog preparations
This commit is contained in:
47
changevaluesdialog.cpp
Normal file
47
changevaluesdialog.cpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include "changevaluesdialog.h"
|
||||||
|
#include "ui_changevaluesdialog.h"
|
||||||
|
|
||||||
|
// Qt includes
|
||||||
|
#include <QDebug>
|
||||||
|
#include <QModbusTcpClient>
|
||||||
|
|
||||||
|
ChangeValuesDialog::ChangeValuesDialog(QModbusTcpClient &modbus, QWidget *parent) :
|
||||||
|
QDialog{parent},
|
||||||
|
m_ui{std::make_unique<Ui::ChangeValuesDialog>()},
|
||||||
|
m_modbus{modbus}
|
||||||
|
{
|
||||||
|
m_ui->setupUi(this);
|
||||||
|
|
||||||
|
qDebug() << "called";
|
||||||
|
|
||||||
|
m_ui->tableView->setModel(&m_model);
|
||||||
|
|
||||||
|
m_ui->spinBoxFirstRegister->setValue(m_model.firstRegister());
|
||||||
|
m_ui->spinBoxCount->setValue(m_model.count());
|
||||||
|
|
||||||
|
connect(m_ui->spinBoxFirstRegister, &QSpinBox::valueChanged, &m_model, &ChangeValuesModel::setFirstRegister);
|
||||||
|
connect(m_ui->spinBoxCount, &QSpinBox::valueChanged, &m_model, &ChangeValuesModel::setCount);
|
||||||
|
|
||||||
|
connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &ChangeValuesDialog::sendRequest);
|
||||||
|
|
||||||
|
|
||||||
|
{
|
||||||
|
const auto addItem = [&](const auto &text, const auto &value){
|
||||||
|
m_ui->comboBoxType->addItem(text, QVariant::fromValue<QModbusDataUnit::RegisterType>(value));
|
||||||
|
};
|
||||||
|
addItem(tr("Discrete Inputs"), QModbusDataUnit::DiscreteInputs);
|
||||||
|
addItem(tr("Coils"), QModbusDataUnit::Coils);
|
||||||
|
addItem(tr("Input Registers"), QModbusDataUnit::InputRegisters);
|
||||||
|
addItem(tr("Holding Registers"), QModbusDataUnit::HoldingRegisters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ChangeValuesDialog::~ChangeValuesDialog()
|
||||||
|
{
|
||||||
|
qDebug() << "called";
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChangeValuesDialog::sendRequest()
|
||||||
|
{
|
||||||
|
accept();
|
||||||
|
}
|
31
changevaluesdialog.h
Normal file
31
changevaluesdialog.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// system includes
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
// Qt includes
|
||||||
|
#include <QDialog>
|
||||||
|
|
||||||
|
// local includes
|
||||||
|
#include "changevaluesmodel.h"
|
||||||
|
|
||||||
|
// forward declares
|
||||||
|
class QModbusTcpClient;
|
||||||
|
namespace Ui {class ChangeValuesDialog; }
|
||||||
|
|
||||||
|
class ChangeValuesDialog : public QDialog
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ChangeValuesDialog(QModbusTcpClient &modbus, QWidget *parent = nullptr);
|
||||||
|
~ChangeValuesDialog() override;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void sendRequest();
|
||||||
|
|
||||||
|
private:
|
||||||
|
const std::unique_ptr<Ui::ChangeValuesDialog> m_ui;
|
||||||
|
QModbusTcpClient &m_modbus;
|
||||||
|
ChangeValuesModel m_model;
|
||||||
|
};
|
93
changevaluesdialog.ui
Normal file
93
changevaluesdialog.ui
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>ChangeValuesDialog</class>
|
||||||
|
<widget class="QDialog" name="ChangeValuesDialog">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>400</width>
|
||||||
|
<height>300</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>Change values</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout" stretch="0,1,0">
|
||||||
|
<item>
|
||||||
|
<layout class="QFormLayout" name="formLayout">
|
||||||
|
<item row="0" column="0">
|
||||||
|
<widget class="QLabel" name="label">
|
||||||
|
<property name="text">
|
||||||
|
<string>TextLabel</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="0" column="1">
|
||||||
|
<widget class="QComboBox" name="comboBoxType"/>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="0">
|
||||||
|
<widget class="QLabel" name="labelFirstRegister">
|
||||||
|
<property name="text">
|
||||||
|
<string>First reister:</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="1" column="1">
|
||||||
|
<widget class="QSpinBox" name="spinBoxFirstRegister">
|
||||||
|
<property name="maximum">
|
||||||
|
<number>9999</number>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="0">
|
||||||
|
<widget class="QLabel" name="labelCount">
|
||||||
|
<property name="text">
|
||||||
|
<string>Count:</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item row="2" column="1">
|
||||||
|
<widget class="QSpinBox" name="spinBoxCount">
|
||||||
|
<property name="maximum">
|
||||||
|
<number>9999</number>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QTableView" name="tableView"/>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QDialogButtonBox" name="buttonBox">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="standardButtons">
|
||||||
|
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<resources/>
|
||||||
|
<connections>
|
||||||
|
<connection>
|
||||||
|
<sender>buttonBox</sender>
|
||||||
|
<signal>rejected()</signal>
|
||||||
|
<receiver>ChangeValuesDialog</receiver>
|
||||||
|
<slot>reject()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>316</x>
|
||||||
|
<y>260</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>286</x>
|
||||||
|
<y>274</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
</connections>
|
||||||
|
</ui>
|
134
changevaluesmodel.cpp
Normal file
134
changevaluesmodel.cpp
Normal file
@ -0,0 +1,134 @@
|
|||||||
|
#include "changevaluesmodel.h"
|
||||||
|
|
||||||
|
// Qt includes
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
ChangeValuesModel::ChangeValuesModel(QObject *parent) :
|
||||||
|
QAbstractTableModel{parent}
|
||||||
|
{
|
||||||
|
m_registers.resize(m_count);
|
||||||
|
qDebug() << "size =" << m_registers.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
ChangeValuesModel::~ChangeValuesModel()
|
||||||
|
{
|
||||||
|
qDebug() << "called";
|
||||||
|
}
|
||||||
|
|
||||||
|
QModelIndex ChangeValuesModel::index(int row, int column, const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
Q_ASSERT(!parent.isValid());
|
||||||
|
return createIndex(row, column);
|
||||||
|
}
|
||||||
|
|
||||||
|
QModelIndex ChangeValuesModel::parent(const QModelIndex &child) const
|
||||||
|
{
|
||||||
|
Q_UNUSED(child)
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
int ChangeValuesModel::rowCount(const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
Q_ASSERT(!parent.isValid());
|
||||||
|
return m_count;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ChangeValuesModel::columnCount(const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
Q_ASSERT(!parent.isValid());
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant ChangeValuesModel::data(const QModelIndex &index, int role) const
|
||||||
|
{
|
||||||
|
Q_ASSERT(index.isValid());
|
||||||
|
|
||||||
|
switch (role)
|
||||||
|
{
|
||||||
|
case Qt::DisplayRole:
|
||||||
|
return QString::number(m_registers.at(m_firstRegister + index.row()));
|
||||||
|
case Qt::EditRole:
|
||||||
|
return m_registers.at(m_firstRegister + index.row());
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant ChangeValuesModel::headerData(int section, Qt::Orientation orientation, int role) const
|
||||||
|
{
|
||||||
|
switch (orientation)
|
||||||
|
{
|
||||||
|
case Qt::Horizontal:
|
||||||
|
Q_ASSERT(section == 0);
|
||||||
|
|
||||||
|
switch (role)
|
||||||
|
{
|
||||||
|
case Qt::DisplayRole:
|
||||||
|
case Qt::EditRole:
|
||||||
|
switch (section)
|
||||||
|
{
|
||||||
|
case 0: return tr("Value");
|
||||||
|
}
|
||||||
|
__builtin_unreachable();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case Qt::Vertical:
|
||||||
|
|
||||||
|
switch (role)
|
||||||
|
{
|
||||||
|
case Qt::DisplayRole: return QString::number(m_firstRegister + section);
|
||||||
|
case Qt::EditRole: return m_firstRegister + section;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Qt::ItemFlags ChangeValuesModel::flags(const QModelIndex &index) const
|
||||||
|
{
|
||||||
|
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ChangeValuesModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||||
|
{
|
||||||
|
qDebug() << "setData" << index << value << Qt::ItemDataRole(role);
|
||||||
|
|
||||||
|
if (!value.canConvert<uint16_t>())
|
||||||
|
{
|
||||||
|
qCritical() << "wrong type";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (role)
|
||||||
|
{
|
||||||
|
case Qt::EditRole:
|
||||||
|
m_registers[index.row() + m_firstRegister] = value.value<uint16_t>();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChangeValuesModel::setFirstRegister(uint16_t firstRegister)
|
||||||
|
{
|
||||||
|
beginResetModel();
|
||||||
|
m_firstRegister = firstRegister;
|
||||||
|
if (m_registers.size() < m_firstRegister + m_count)
|
||||||
|
m_registers.resize(m_firstRegister + m_count);
|
||||||
|
endResetModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChangeValuesModel::setCount(uint16_t count)
|
||||||
|
{
|
||||||
|
beginResetModel();
|
||||||
|
m_count = count;
|
||||||
|
if (m_registers.size() < m_firstRegister + m_count)
|
||||||
|
m_registers.resize(m_firstRegister + m_count);
|
||||||
|
endResetModel();
|
||||||
|
}
|
34
changevaluesmodel.h
Normal file
34
changevaluesmodel.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Qt includes
|
||||||
|
#include <QAbstractTableModel>
|
||||||
|
#include <QVector>
|
||||||
|
|
||||||
|
class ChangeValuesModel : public QAbstractTableModel
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit ChangeValuesModel(QObject *parent = nullptr);
|
||||||
|
~ChangeValuesModel() override;
|
||||||
|
|
||||||
|
uint16_t firstRegister() const { return m_firstRegister; }
|
||||||
|
uint16_t count() const { return m_count; }
|
||||||
|
|
||||||
|
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
|
||||||
|
QModelIndex parent(const QModelIndex &child) const override;
|
||||||
|
int rowCount(const QModelIndex &parent) const override;
|
||||||
|
int columnCount(const QModelIndex &parent) const override;
|
||||||
|
QVariant data(const QModelIndex &index, int role) const override;
|
||||||
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
|
||||||
|
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||||
|
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void setFirstRegister(uint16_t firstRegister);
|
||||||
|
void setCount(uint16_t count);
|
||||||
|
|
||||||
|
private:
|
||||||
|
uint16_t m_firstRegister{};
|
||||||
|
uint16_t m_count{100};
|
||||||
|
|
||||||
|
QVector<uint16_t> m_registers;
|
||||||
|
};
|
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
// local includes
|
// local includes
|
||||||
#include "modbustablemodel.h"
|
#include "modbustablemodel.h"
|
||||||
|
#include "changevaluesdialog.h"
|
||||||
|
|
||||||
// utilities
|
// utilities
|
||||||
namespace {
|
namespace {
|
||||||
@ -60,6 +61,7 @@ MainWindow::MainWindow(QWidget *parent) :
|
|||||||
|
|
||||||
connect(m_ui->pushButtonConnect, &QAbstractButton::pressed, this, &MainWindow::connectPressed);
|
connect(m_ui->pushButtonConnect, &QAbstractButton::pressed, this, &MainWindow::connectPressed);
|
||||||
connect(m_ui->pushButtonRequest, &QAbstractButton::pressed, this, &MainWindow::requestPressed);
|
connect(m_ui->pushButtonRequest, &QAbstractButton::pressed, this, &MainWindow::requestPressed);
|
||||||
|
connect(m_ui->pushButtonWrite, &QAbstractButton::pressed, this, &MainWindow::writePressed);
|
||||||
|
|
||||||
m_ui->tableView->setModel(m_model.get());
|
m_ui->tableView->setModel(m_model.get());
|
||||||
}
|
}
|
||||||
@ -156,6 +158,13 @@ void MainWindow::requestPressed()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::writePressed()
|
||||||
|
{
|
||||||
|
ChangeValuesDialog dialog{*m_modbus, this};
|
||||||
|
dialog.exec();
|
||||||
|
qDebug() << "called";
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::modbusErrorOccured(int error)
|
void MainWindow::modbusErrorOccured(int error)
|
||||||
{
|
{
|
||||||
const auto typedError = QModbusDevice::Error(error);
|
const auto typedError = QModbusDevice::Error(error);
|
||||||
|
@ -24,6 +24,7 @@ public:
|
|||||||
private slots:
|
private slots:
|
||||||
void connectPressed();
|
void connectPressed();
|
||||||
void requestPressed();
|
void requestPressed();
|
||||||
|
void writePressed();
|
||||||
|
|
||||||
void modbusErrorOccured(int error);
|
void modbusErrorOccured(int error);
|
||||||
void modbusStateChanged(int state);
|
void modbusStateChanged(int state);
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
<item>
|
<item>
|
||||||
<widget class="QLineEdit" name="lineEditServer">
|
<widget class="QLineEdit" name="lineEditServer">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>192.168.0.75</string>
|
<string>192.168.8.116</string>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
@ -287,6 +287,26 @@
|
|||||||
</property>
|
</property>
|
||||||
</spacer>
|
</spacer>
|
||||||
</item>
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="pushButtonWrite">
|
||||||
|
<property name="text">
|
||||||
|
<string>Write values</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<spacer name="horizontalSpacer_4">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sizeHint" stdset="0">
|
||||||
|
<size>
|
||||||
|
<width>40</width>
|
||||||
|
<height>20</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
</spacer>
|
||||||
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLabel" name="labelRequestStatus">
|
<widget class="QLabel" name="labelRequestStatus">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
|
@ -25,6 +25,7 @@ QModelIndex ModbusTableModel::index(int row, int column, const QModelIndex &pare
|
|||||||
|
|
||||||
QModelIndex ModbusTableModel::parent(const QModelIndex &child) const
|
QModelIndex ModbusTableModel::parent(const QModelIndex &child) const
|
||||||
{
|
{
|
||||||
|
Q_UNUSED(child)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,12 +5,17 @@ CONFIG += c++17
|
|||||||
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
|
DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000
|
||||||
|
|
||||||
SOURCES += main.cpp \
|
SOURCES += main.cpp \
|
||||||
|
changevaluesdialog.cpp \
|
||||||
|
changevaluesmodel.cpp \
|
||||||
mainwindow.cpp \
|
mainwindow.cpp \
|
||||||
modbustablemodel.cpp
|
modbustablemodel.cpp
|
||||||
|
|
||||||
FORMS += \
|
FORMS += \
|
||||||
|
changevaluesdialog.ui \
|
||||||
mainwindow.ui
|
mainwindow.ui
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
|
changevaluesdialog.h \
|
||||||
|
changevaluesmodel.h \
|
||||||
mainwindow.h \
|
mainwindow.h \
|
||||||
modbustablemodel.h
|
modbustablemodel.h
|
||||||
|
Reference in New Issue
Block a user