implementations for abstract list model and trying to get signals and slots updating list data
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!DOCTYPE QtCreatorProject>
|
<!DOCTYPE QtCreatorProject>
|
||||||
<!-- Written by QtCreator 12.0.2, 2024-02-07T14:21:41. -->
|
<!-- Written by QtCreator 12.0.2, 2024-02-08T09:05:49. -->
|
||||||
<qtcreator>
|
<qtcreator>
|
||||||
<data>
|
<data>
|
||||||
<variable>EnvironmentId</variable>
|
<variable>EnvironmentId</variable>
|
||||||
|
|||||||
@@ -1,18 +1,32 @@
|
|||||||
import QtQuick
|
import QtQuick
|
||||||
import QtQuick.Controls
|
import QtQuick.Controls
|
||||||
|
|
||||||
Window {
|
Rectangle {
|
||||||
id: window
|
id: window
|
||||||
width: 640
|
width: 640
|
||||||
height: 480
|
height: 480
|
||||||
visible: true
|
visible: true
|
||||||
title: qsTr("WIFI picker")
|
|
||||||
|
required property var networksModel
|
||||||
|
|
||||||
signal scanNetworks()
|
signal scanNetworks()
|
||||||
|
|
||||||
Button {
|
Button {
|
||||||
id: scanButton
|
id: scanbutton
|
||||||
text: "Scan for networks"
|
text: "Scan for networks"
|
||||||
onClicked: window.scanNetworks()
|
onClicked: window.scanNetworks()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ListView {
|
||||||
|
id: listview
|
||||||
|
objectName: "listview"
|
||||||
|
model: parent.networksModel
|
||||||
|
delegate: Rectangle {
|
||||||
|
required property string name
|
||||||
|
|
||||||
|
Text {
|
||||||
|
text: parent.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+58
-1
@@ -1,7 +1,64 @@
|
|||||||
#include "NetworkScanner.h"
|
#include "NetworkScanner.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <QAbstractListModel>
|
||||||
|
|
||||||
void NetworkScanner::scan()
|
QList<Network> NetworkScanner::scanForNetworks()
|
||||||
{
|
{
|
||||||
std::cout << "Called the C++ Network scanner class" << std::endl;
|
std::cout << "Called the C++ Network scanner class" << std::endl;
|
||||||
|
|
||||||
|
QList<Network> networks;
|
||||||
|
|
||||||
|
Network example1 = Network("Gita 2.4Ghz");
|
||||||
|
networks.append(example1);
|
||||||
|
|
||||||
|
return networks;
|
||||||
|
}
|
||||||
|
|
||||||
|
NetworksModel::NetworksModel(QObject *parent)
|
||||||
|
:
|
||||||
|
QAbstractListModel(parent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int NetworksModel::rowCount(const QModelIndex &parent) const
|
||||||
|
{
|
||||||
|
Q_UNUSED(parent);
|
||||||
|
return this->m_networks.count();
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant NetworksModel::data(const QModelIndex &index, int role) const
|
||||||
|
{
|
||||||
|
if (index.row() < 9 || index.row() >= this->m_networks.count())
|
||||||
|
{
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
const Network& network = this->m_networks.at(index.row());
|
||||||
|
if (role == NameRole)
|
||||||
|
{
|
||||||
|
return network.name();
|
||||||
|
}
|
||||||
|
|
||||||
|
return QVariant();
|
||||||
|
}
|
||||||
|
|
||||||
|
void NetworksModel::scan()
|
||||||
|
{
|
||||||
|
QList<Network> networks = this->m_networkScanner.scanForNetworks();
|
||||||
|
for (const Network& network: networks)
|
||||||
|
{
|
||||||
|
qDebug() << "Got network: " << network.name() << Qt::endl;
|
||||||
|
}
|
||||||
|
this->m_networks = networks;
|
||||||
|
}
|
||||||
|
|
||||||
|
Network::Network(const QString &name)
|
||||||
|
: m_name(name)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QString Network::name() const
|
||||||
|
{
|
||||||
|
return this->m_name;
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-12
@@ -4,24 +4,42 @@
|
|||||||
#endif // NETWORKSCANNER_H
|
#endif // NETWORKSCANNER_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QAbstractListModel>
|
||||||
|
|
||||||
// class Network
|
class Network
|
||||||
// {
|
{
|
||||||
// public:
|
public:
|
||||||
// enum NetworkStatus {
|
Network(const QString &name);
|
||||||
// Connected = 0,
|
|
||||||
// Available
|
|
||||||
// };
|
|
||||||
|
|
||||||
// Network(const QString &name, const NetworkStatus &status);
|
QString name() const;
|
||||||
|
private:
|
||||||
// QString name;
|
QString m_name;
|
||||||
// NetworkStatus status;
|
};
|
||||||
// };
|
|
||||||
|
|
||||||
class NetworkScanner : public QObject
|
class NetworkScanner : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
QList<Network> scanForNetworks();
|
||||||
|
};
|
||||||
|
|
||||||
|
class NetworksModel : public QAbstractListModel
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
enum NetworkRoles
|
||||||
|
{
|
||||||
|
NameRole = Qt::UserRole + 1
|
||||||
|
};
|
||||||
|
|
||||||
|
NetworksModel(QObject *parent = nullptr);
|
||||||
|
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
||||||
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void scan();
|
void scan();
|
||||||
|
|
||||||
|
private:
|
||||||
|
QList<Network> m_networks;
|
||||||
|
NetworkScanner m_networkScanner;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -9,16 +9,23 @@ int main(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
QGuiApplication app(argc, argv);
|
QGuiApplication app(argc, argv);
|
||||||
|
|
||||||
|
NetworksModel model;
|
||||||
|
|
||||||
QQuickView view;
|
QQuickView view;
|
||||||
view.setResizeMode(QQuickView::SizeRootObjectToView);
|
view.setResizeMode(QQuickView::SizeRootObjectToView);
|
||||||
//view.setInitialProperties({{"model", QVariant::fromValue(&model)}});
|
view.setInitialProperties({{"networksModel", QVariant::fromValue(&model)}});
|
||||||
view.setSource(QUrl("qrc:/qt/qml/abstractitemmodel/view.qml"));
|
view.setSource(QUrl("qrc:/wifipicker/Main.qml"));
|
||||||
view.show();
|
view.show();
|
||||||
|
|
||||||
QQuickItem *rootObject = view.rootObject();
|
if (!view.rootObject())
|
||||||
// QQuickWindow *window = qobject_cast<QQuickWindow*>(rootObject);
|
{
|
||||||
NetworkScanner networkScanner;
|
qDebug() << "No root object found" << Qt::endl;
|
||||||
QObject::connect(rootObject, SIGNAL(scanNetworks()), &networkScanner, SLOT(scan()));
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
QObject *rootObject = view.rootObject();
|
||||||
|
QObject::connect(rootObject, SIGNAL(scanNetworks()), &model, SLOT(scan()));
|
||||||
|
|
||||||
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user