[QT] QSetting 값을 QML UI로 Display 하는 방법
2021. 8. 30. 14:14ㆍ카테고리 없음
반응형
GwEvccSettingView 생성 시, QML의 UI 값을 변경하는 예제 코드
QTimer 없이 생성자에서 바로 emit을 호출하면, QML 쪽 Slot이 초기화 되기 전에 emit 함수가 호출 되기 때문에 UI에 값이 표시 되지 않는다.
이런 이유로, QTimer의 singleshot을 이용하여 문제를 해결 함.
#ifndef GWEVCCSETTING_H
#define GWEVCCSETTING_H
#include <QObject>
#include <QSettings>
#define EV_EMULATOR_SETTING_FILE_PATH "./EV_PNC_Emulator_Setting.ini"
class GwEvccSettingView : public QObject
{
Q_OBJECT
public:
explicit GwEvccSettingView(QObject *parent = nullptr);
void UpdateSettingView();
signals:
void callSettingInfoUpdate(const QString key, const QVariant value);
private:
QSettings m_evccAppSetting;
};
#endif // GWEVCCSETTING_H
#include "gwevccsettingview.h"
#include <QFile>
#include <QDebug>
#include <QTimer>
GwEvccSettingView::GwEvccSettingView(QObject *parent) : QObject(parent),
m_evccAppSetting(EV_EMULATOR_SETTING_FILE_PATH,QSettings::IniFormat)
{
if( !QFile::exists(EV_EMULATOR_SETTING_FILE_PATH) )
{
// Set Default Configuration
m_evccAppSetting.setValue("SERVER IP","192.168.1.222");
m_evccAppSetting.setValue("SERVER PORT",5000);
}
QTimer::singleShot(1000, this, &GwEvccSettingView::UpdateSettingView);
}
void GwEvccSettingView::UpdateSettingView()
{
QStringList childKeys = m_evccAppSetting.childKeys();
foreach (const QString &childKey, childKeys) {
emit callSettingInfoUpdate(childKey, m_evccAppSetting.value(childKey));
}
}
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
Page {
width: 480
height: 320
property alias button_saveConfig: button_saveConfig
Connections {
target: evccSettingView
onCallSettingInfoUpdate: {
if (key == "SERVER IP") {
textField_apiServerIp.text = value
} else if (key == "SERVER PORT") {
textField_apiServerPort.text = value
}
}
}
ColumnLayout {
anchors.fill: parent
GridLayout {
Layout.preferredHeight: 108
Layout.preferredWidth: 441
layoutDirection: Qt.LeftToRight
flow: GridLayout.LeftToRight
rows: 3
columns: 3
Text {
id: element
color: "#ffffff"
text: qsTr("API Server IP")
Layout.preferredHeight: 57
Layout.preferredWidth: 147
font.bold: true
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
Layout.fillHeight: true
Layout.fillWidth: true
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
font.pixelSize: 15
}
TextField {
id: textField_apiServerIp
text: qsTr("")
Layout.preferredHeight: 43
Layout.preferredWidth: 175
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.fillHeight: true
Layout.fillWidth: true
Layout.rowSpan: 1
}
Button {
id: button_saveConfig
text: qsTr("Save")
Layout.rowSpan: 2
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredHeight: 48
Layout.preferredWidth: 108
}
Text {
id: element1
color: "#ffffff"
text: qsTr("API Server Port")
Layout.preferredHeight: 53
Layout.preferredWidth: 138
font.bold: true
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.fillWidth: true
Layout.fillHeight: true
font.pixelSize: 15
}
TextField {
id: textField_apiServerPort
text: qsTr("")
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.preferredHeight: 43
Layout.preferredWidth: 175
Layout.fillHeight: true
Layout.fillWidth: true
Layout.rowSpan: 1
}
}
RowLayout {
Layout.preferredHeight: 65
Layout.preferredWidth: 441
Button {
id: button_serverReconnect
text: qsTr("SERVER-RECONNECT")
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredHeight: 65
Layout.preferredWidth: 197
}
Button {
id: button_serialReconnect
text: qsTr("Serial-Reconnect")
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.fillHeight: true
Layout.fillWidth: true
Layout.preferredHeight: 65
Layout.preferredWidth: 216
}
}
TextArea {
id: textArea
text: qsTr("Config Info")
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Layout.preferredHeight: 80
Layout.preferredWidth: 438
}
}
}
반응형