ubuntuusers.de

Dateien in Combobuttor einlesen

Status: Ungelöst | Ubuntu-Version: Ubuntu Touch 22.04 (Jammy Jellyfish)
Antworten |

dl8aax

Avatar von dl8aax

Anmeldungsdatum:
6. Mai 2016

Beiträge: 108

Wohnort: bei Göttingen

Moin,

ich versuche gerade ne app zu erstellen. Ich möchte die Namen der Datenbanken in ein ComboButton einlesen um so eine Datenbank auszuwählen bzw. neu zu erstellen

mein bisheriger Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

import QtQuick 2.7
import Lomiri.Components 1.3
import QtQuick.Layouts 1.3
import Qt.labs.folderlistmodel 2.1
import QtQuick.LocalStorage 2.0
import Qt.labs.platform 1.1
import Qt.labs.settings 1.0


Page {
    id: settingsPage

    property string currentDb: ""

    // --- Ersatz für UbuntuColors ---
    QtObject {
        id: ubuntucolors
        property color orange: "#E95420"
        property color lightGreen: "#68B723"
        property color suruGrey: "#AEA79F"
        property color darkGrey: "#333333"
    }
Settings {
        id: appSettings
         property string mycallsign: "n0call"
         property string myqth: "Kronos"
          property string currentDb: ""
          property string dbPfad:  "File://.local/share/portablelog.dl8aax/Databases/"
          
        } 
    Flickable {
        anchors.fill: parent
        contentHeight: columnContent.height + units.gu(4)
        clip: true

        Column {
            id: columnContent
            width: parent.width
            spacing: units.gu(2)
            anchors.margins: units.gu(2)

            Label {
                text: i18n.tr("Einstellungen")
                color: ubuntucolors.orange
                font.bold: true
                wrapMode: Text.WordWrap
            }

            TextField {
                id: callField
                placeholderText: "My Call"
                width: units.gu(13)
                text:  appSettings.mycallsign
                onTextChanged: appSettings.mycallsign = text
            }

            TextField {
                id: nameField
                placeholderText: "My Name"
                width: units.gu(13)
            }

            Rectangle {
                width: parent.width
                height: 3
                color: ubuntucolors.lightGreen
                opacity: 0.6
            }

          ComboButton {
    id: dbCombo
    text: currentDb === "" ? i18n.tr("Datenbank wählen") : currentDb
    expandedHeight: units.gu(25)

    model: ListModel { id: dbModel }

    delegate: ListItem {
        width: parent.width
        Label {
            text: name
            anchors.verticalCenter: parent.verticalCenter
        }

        onClicked: {
            if (name === i18n.tr("Neue Datenbank anlegen...")) {
                newDbOverlay.visible = true
            } else {
                appSettings.currentDb = name
                dbCombo.text = name
                currentDb = name
                dbCombo.expanded = false
                openDatabase(name)
            }
        }
    }

    Component.onCompleted: {
        console.log("📁 Lade Datenbanken aus:", dbFolder.folder, " ->", dbFolder.count, "Dateien")
        if (dbFolder.count > 0)
            loadDatabases()
    }
}
        }
    }

    Rectangle {
        id: newDbOverlay
        anchors.fill: parent
        color: "#000000AA"
        visible: false
        z: 100

        Rectangle {
            width: parent.width * 0.8
            height: units.gu(20)
            anchors.centerIn: parent
            radius: units.gu(1)
            color: ubuntucolors.suruGrey
            border.color: ubuntucolors.orange
            border.width: 2

            Column {
                anchors.fill: parent
                anchors.margins: units.gu(2)
                spacing: units.gu(2)
                Label {
                    text: i18n.tr("Neue Datenbank anlegen")
                    color: ubuntucolors.orange
                    font.bold: true
                }
                TextField {
                    id: newDbName
                    placeholderText: i18n.tr("z.B. mylog.sqlite")
                }
                Row {
                    spacing: units.gu(2)
                    Button {
                        text: i18n.tr("Erstellen")
                        color: ubuntucolors.orange
                        onClicked: {
                            if (newDbName.text.length > 0) {
                               
                                settingsPage.createDatabase(newDbName.text)
                                loadDatabases()
                                dbCombo.text = newDbName.text
                                dbCombo.selectedDatabase = newDbName.text
                            }
                            newDbOverlay.visible = false
                        }
                    }

                    Button {
                        text: i18n.tr("Abbrechen")
                        onClicked: newDbOverlay.visible = false
                    }
                }
            }
        }
    }

    // --- Datenbankfunktionen ---
    function loadDatabases() {
        dbModel.clear()
        dbModel.append({ "name": i18n.tr("Neue Datenbank anlegen...") })

        console.log("📁 Lade Datenbanken aus:", dbFolder.folder, " ->", dbFolder.count, "Dateien")

        for (var i = 0; i < dbFolder.count; i++) {
            var fn = dbFolder.get(i, "fileName")
            var displayName = fn.replace(".sqlite", "")
            dbModel.append({ "name": displayName })
        }

        if (currentDb !== "") {
            dbCombo.text = currentDb.replace(".sqlite", "")
            dbCombo.selectedDatabase = currentDb
        }
    }

    function openDatabase(fileName) {
        currentDb = fileName
        var db = LocalStorage.openDatabaseSync(fileName, "1.0", "PortableLog", 1000000)
        db.transaction(function(tx) {
            tx.executeSql(
                'CREATE TABLE IF NOT EXISTS log (' +
                'id INTEGER PRIMARY KEY AUTOINCREMENT, ' +
                'call TEXT, name TEXT, time TEXT, qrg TEXT, ' +
                'rst_sent TEXT, rst_rcvd TEXT, mode TEXT, ' +
                'reference TEXT, qth TEXT, qsl INTEGER)'
            )
        })
        console.log("✅ DB geöffnet / initialisiert:", fileName)
    }

    function createDatabase(name) {
        openDatabase(name)
    }

   FolderListModel {
    id: dbFolder
    folder: "file://" + StandardPaths.writableLocation(StandardPaths.AppDataLocation) + "/Databases"
    nameFilters: ["*.sqlite"]
    showDirs: false

    onCountChanged: {
        if (count > 0)
            settingsPage.loadDatabases()
    }

    Component.onCompleted: {
        console.log("🔍 Datenbankpfad:", folder)
        if (count > 0)
            settingsPage.loadDatabases()
    }
}
}

Lg Dirk

Antworten |