NetworkModel.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "NetworkModel.h"
  2. #include <QNetworkConfigurationManager>
  3. #include <QDebug>
  4. NetworkModel::NetworkModel(QObject *parent)
  5. : QAbstractListModel(parent)
  6. , m_manager(new QNetworkConfigurationManager(this))
  7. {
  8. QHash<int, QByteArray> role_names;
  9. role_names.insert(RoleName, "name");
  10. role_names.insert(RoleDescription, "description");
  11. setRoleNames(role_names);
  12. scan();
  13. }
  14. NetworkModel::~NetworkModel()
  15. {
  16. }
  17. void NetworkModel::scan()
  18. {
  19. m_configurations = m_manager->allConfigurations();
  20. reset();
  21. }
  22. int NetworkModel::rowCount(const QModelIndex &parent) const
  23. {
  24. return m_configurations.count();
  25. }
  26. QVariant NetworkModel::data(const QModelIndex &index, int role) const
  27. {
  28. if (!index.isValid()) {
  29. return QVariant();
  30. }
  31. QNetworkConfiguration configuration(m_configurations.at(index.row()));
  32. switch (role) {
  33. case RoleName:
  34. return configuration.name();
  35. case RoleDescription:
  36. return configuration.bearerTypeName();
  37. default:
  38. break;
  39. }
  40. return QVariant();
  41. }
  42. QNetworkConfiguration NetworkModel::configurationForRow(int row)
  43. {
  44. return m_configurations.at(row);
  45. }