Browse Source

Set role names

Girish Ramakrishnan 8 years ago
parent
commit
802c33982b
2 changed files with 37 additions and 2 deletions
  1. 25 2
      qavahiservicebrowsermodel.cpp
  2. 12 0
      qavahiservicebrowsermodel.h

+ 25 - 2
qavahiservicebrowsermodel.cpp

@@ -21,7 +21,7 @@ static void QABDEBUG(const char *fmt, ...)
         va_start(ap, fmt);
         vsnprintf(buf, sizeof(buf), fmt, ap);
         va_end(ap);
-        qDebug("%s", buf);
+        qDebug("QAvahiServiceBrowserModel: %s", buf);
     }
 }
 
@@ -30,6 +30,19 @@ QAvahiServiceBrowserModel::QAvahiServiceBrowserModel(QObject *parent)
       m_browser(0), m_autoResolve(true)
 {
     qRegisterMetaType<QAvahiServiceBrowserModel::Service>();
+
+    QHash<int, QByteArray> hash = QAbstractListModel::roleNames();
+    hash[NameRole] = "name";
+    hash[TypeRole] = "type";
+    hash[DomainRole] = "domain";
+    hash[ProtocolRole] = "protocol";
+    hash[InterfaceRole] = "interface";
+    hash[HostNameRole] = "hostname";
+    hash[AddressRole] = "address";
+    hash[PortRole] = "port";
+    hash[TxtRole] = "txt";
+    setRoleNames(hash);
+
     initialize();
 }
 
@@ -364,7 +377,17 @@ QVariant QAvahiServiceBrowserModel::data(const QModelIndex &index, int role) con
         if (col == 7) return service.port;
         if (col == 8) return service.textRecords.join(",");
         break;
-    default: break;
+    case NameRole: return service.name;
+    case TypeRole: return service.type;
+    case DomainRole: return service.domain;
+    case ProtocolRole: return service.protocol;
+    case InterfaceRole: return service.interface;
+    case HostNameRole: return service.hostName;
+    case AddressRole: return service.address.toString();
+    case PortRole: return service.port;
+    case TxtRole: return service.textRecords.join(",");
+    default:
+        break;
     }
     return QVariant();
 }

+ 12 - 0
qavahiservicebrowsermodel.h

@@ -29,6 +29,18 @@ public:
     QAvahiServiceBrowserModel(QObject *parent = 0);
     ~QAvahiServiceBrowserModel();
 
+    enum CustomRoles {
+        NameRole = Qt::UserRole + 1,
+        TypeRole,
+        DomainRole,
+        ProtocolRole,
+        InterfaceRole,
+        HostNameRole,
+        AddressRole,
+        PortRole,
+        TxtRole
+    };
+
     void setAutoResolve(bool ar) { m_autoResolve = ar; }
     bool autoResolve() const { return m_autoResolve; }