Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Help Understanding Correct Design Pattern for Settings Class
Qt 6.11 is out! See what's new in the release blog

Help Understanding Correct Design Pattern for Settings Class

Scheduled Pinned Locked Moved Unsolved C++ Gurus
1 Posts 1 Posters 356 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mcmatt90
    wrote on last edited by
    #1

    I'm hoping this is the correct part of the forum to post this question. Please let me know if it isn't.

    I'm struggling to come up with correct design pattern. I’m using a qml listview (SettingBroswer.qml) to hold different settings. Below is a list oof all the members of the settings. I currently have it as a struct that contains all possible variables. However, I feel like this isn't the proper "C++" way of doing it.

    I wanted to make a settings Class and then use the Qt's AbstracListmodels to feed the settings to my SettingBrowser QML item. The number of settings on each page is static so the user isn't able to add settings but depending on certain user privileges a setting is enabled or disabled. Some setting pages contain a swipeView and each new page index will have a different list of settings.

    Simple derived classes would cause issues such as object slicing(from my understanding). I've been looking through design patterns and am a bit overwhelmed. Any suggestions or advice would be greatly appreciated!

    Below is my current implemenation (summarized)

    struct settingComponent
    {
    Settings settingVariable;
    QString type;
    QString label;
    int value;
    int minValue;
    int maxValue;
    int settingID;
    QStringList optionsList;
    bool settingEnabled;

    };
    

    The settings are then appended to a QVector -> QVector <settingComponent> mSettingList;

    settingComponent mSetting1 {settingType(mSetting1),settingLabel(mSetting1),settingValue(mSetting1),
    minimumValue(mSetting1),maximumValue(mSetting1),settingID(mSetting1), optionsForSetting(mSetting1),settingEnabled(mSetting1)}; (issue here: depending on the setting it would return null values ie an int setting doesn't have an optionsForsetting list)

    mSettingList.append(mSetting1);
    mSettingList.append(mSetting2);
    mSettingList.append(mSetting3);
    ...

    All the different setting members : QString settingLabel, int settingId, int settingValue, int maxValue, int minValue, QStringList optionsForSetting, bool settingEnabled, QString settingType, QString settingString

    Different Settings:
    -Toggle Switch (settingLabel, settingValue (0 or 1), settingId, settingEnabled, settingType=”switch”)
    -ComboBox (settingType=”combo”, settingLabel, settingValue (index of selection from QStringList), optionsForSetting, settingID)
    -Spinbox(settingLabel, settingValue, settingType=”int”, maxValue, minValue, settingID)
    -TextInput (settingLabel, settingId, , settingType=”lineedit”, settingString)
    -Timer (settingLabel, settingID, settingType=”time”, setttingValue (time in seconds) )

    Below is my QML SettingBrowser item, added for more clarity.

    //SettingBrowser.qml

    Item {
        id: root
    
        property string name
        property var settingBrowserModel 
        property int parent_popupheight: parent
        property int parent_popupwidth: parent
        property bool seperator: false
    
        property bool darkMode: presenter.darkMode
        property color backgroundcolor: darkMode ? "#131313" : Variables.white
        property color textcolor: darkMode ?  Variables.white : Variables.anthraciteGrey
        property color headerBackgroundColor: darkMode ? "#2C2C2C" : Variables.ultraLightGrey2
        property bool settingSaved: true
        property string intUnit: ""
        property int settingLabelWidth: parent.width *.4
        property int settingComponentWidth: parent.width * .6
    
    
        signal saveClicked();
        signal sendPUDOValue(int id,int idvalue);
        signal sendTSValue(int id, int idvalue);
        signal sendFeatureValue(int id, int idvalue);
        signal sendTimerValue(int id, int idvalue);
        signal sendHMITestTransferValue(int id, int idvalue);
        signal sendMobileTestTransferValue(int id, int idvalue);
        signal sendEthernetTestTransferValue(int id, int idvalue);
    
    
        FontLoader{id: opensansregular; source: "qrc:/fonts/OpenSans-Regular.ttf";}
        FontLoader{id: opensanssemibold; source: "qrc:/fonts/OpenSans-SemiBold.ttf";}
    
    
    
        Rectangle
        {
    
            id: title
            height: parent.height/6
            anchors.top: parent.top
            anchors.left: parent.left
            anchors.right: parent.right
            color: backgroundcolor
    
            Text
            {
                id: titletext1
                anchors.left: parent.left
                width: parent.width
                height: parent.height
                fontSizeMode: Text.Fit;
                text: name
                font.pixelSize: 20
                color: textcolor
                verticalAlignment: Text.AlignVCenter
                anchors.leftMargin: parent.width/64
                font.family: opensanssemibold.name
                textFormat : Text.AutoText
            }
        }
    
            ListView {
                id: settinglistView
                width: parent.width
                z:-1
    
                anchors.top: title.bottom
                anchors.bottom: parent.bottom
                anchors.horizontalCenter: parent.horizontalCenter
                boundsBehavior: Flickable.StopAtBounds
                        ScrollBar.vertical: ScrollBar {
                            id: settinglistScrollbar
                            policy: ScrollBar.AlwaysOn
                            active: true
                            interactive: true
    
                        }
    
                cacheBuffer: 150
                model: settingBrowserModel
    
    
    
                delegate: Component {
                    id: settingComponentDelegate
    
    
    
                    Rectangle{
                        id: settingDelegate
                        //z: -index
                        width: settinglistView.width
                        height: settinglistView.height/3
                        color: backgroundcolor
                        property int componentID: model.settingID
    
                        Text {
                            id: settingname
                            anchors.left: parent.left
                            anchors.leftMargin: parent.width/32
                            text: model.label
                            wrapMode: Text.WordWrap
                            color: textcolor
                            minimumPixelSize: 12
                            font.pixelSize: 20
                            font.family: opensansregular.name
                            height: parent.height
                            width: settingLabelWidth
                            verticalAlignment: TextInput.AlignVCenter
                            textFormat : Text.AutoText
                            fontSizeMode: Text.Fit
                        }
                        Rectangle{
                            id:horzSeperator
                            height: 2
                            anchors.left: parent.left
                            anchors.right: parent.right
                            anchors.bottom: parent.bottom
                            visible: seperator
                            color: Variables.ultraLightGrey2
                        }
                        Loader {
                            id: mainLoader
                            width: settingComponentWidth //parent.width * .6
                            height:  parent.height
                            anchors.right: parent.right
                            anchors.verticalCenter: parent.verticalCenter
                            anchors.rightMargin: parent.width / 16
                            source: switch(model.type) {
                                    case "combo": return "ComboPopUp.qml"
                                    case "int": return "CustomSpinBox.qml"
                                    case "switch": return "ToggleSwitch.qml"
                                    case "time": return "TimerPopUp.qml" //"TimerPopUp.qml"
                                    case "save": return "SaveButton.qml"
                                    }
                            asynchronous: true
                        }
                    }
    
                }
    
    
    
            }
    
    
    }
    
    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved