From 705dc359b0345866f300ff43c81cd130e3bcb8d5 Mon Sep 17 00:00:00 2001 From: talksik Date: Thu, 8 Feb 2024 14:40:23 -0800 Subject: [PATCH] showing items after 10 or so items in the model data --- Main.qml | 42 ++++++++++++++++++++---------------------- NetworkScanner.cpp | 11 +++++++---- main.cpp | 8 +++----- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/Main.qml b/Main.qml index 7e749b9..cb77073 100644 --- a/Main.qml +++ b/Main.qml @@ -1,5 +1,6 @@ import QtQuick import QtQuick.Controls +import QtQuick.Layouts Window { id: window @@ -8,34 +9,31 @@ Window { visible: true title: "WIFI Picker" - property var model - signal scanNetworks() - Button { - id: scanbutton - text: "Scan for networks" - onClicked: window.scanNetworks() - } + Button { + id: scanbutton + text: "Scan for networks" + onClicked: window.scanNetworks() + } - ListView { - id: listview - model: window.model - width: 100 - height: 200 - // model: ListModel { - // ListElement { name: "Item 1"; value: 10 } - // ListElement { name: "Item 2"; value: 20 } - // ListElement { name: "Item 3"; value: 30 } - // } + ListView { + id: listview + model: networksModel + anchors.fill: parent + // model: ListModel { + // ListElement { name: "Item 1"; value: 10 } + // ListElement { name: "Item 2"; value: 20 } + // ListElement { name: "Item 3"; value: 30 } + // } - delegate: Item { - height: 20 - required property string name + delegate: Item { + height: 20 + required property string name - Text { + Text { text: parent.name + } } } - } } diff --git a/NetworkScanner.cpp b/NetworkScanner.cpp index f4dac2e..6d8cb61 100644 --- a/NetworkScanner.cpp +++ b/NetworkScanner.cpp @@ -18,9 +18,6 @@ NetworksModel::NetworksModel(QObject *parent) : QAbstractListModel(parent) { - QList networks; - networks.append(Network("Gita test")); - this->m_networks = networks; } int NetworksModel::rowCount(const QModelIndex &parent) const @@ -47,12 +44,18 @@ QVariant NetworksModel::data(const QModelIndex &index, int role) const void NetworksModel::scan() { + beginInsertRows(QModelIndex(), this->rowCount(), this->rowCount()); + QList networks = this->m_networkScanner.scanForNetworks(); for (const Network& network: networks) { qDebug() << "Got network: " << network.name() << Qt::endl; + this->m_networks << network; } - this->m_networks = networks; + + qDebug() << "now have " << this->m_networks.size() << Qt::endl; + + endInsertRows(); } QHash NetworksModel::roleNames() const diff --git a/main.cpp b/main.cpp index 8ad8207..19a8638 100644 --- a/main.cpp +++ b/main.cpp @@ -4,12 +4,14 @@ #include #include #include +#include int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); NetworksModel model; + model.scan(); qDebug() << "now has " << model.rowCount() << Qt::endl; QQmlApplicationEngine engine; @@ -20,9 +22,9 @@ int main(int argc, char *argv[]) &app, []() { QCoreApplication::exit(-1); }, Qt::QueuedConnection); + engine.rootContext()->setContextProperty("networksModel", &model); engine.load(url); - QObject *rootObject = engine.rootObjects().first(); if (!rootObject) { @@ -30,11 +32,7 @@ int main(int argc, char *argv[]) return -1; } - engine.setInitialProperties({{"model", QVariant::fromValue(&model)}}); - rootObject->setProperty("model", QVariant::fromValue(&model)); - QObject::connect(rootObject, SIGNAL(scanNetworks()), &model, SLOT(scan())); - return app.exec(); }