KDNSSD 5.109.0
Public Types | Public Member Functions | List of all members
KDNSSD::ServiceModel

Model for list of Zeroconf services. More...

#include <KDNSSD/ServiceModel>

Public Types

enum  AdditionalRoles { ServicePtrRole = 0xA06519DE }
 The additional data roles provided by this model. More...
 
enum  ModelColumns { ServiceName = 0 , Host = 1 , Port = 2 }
 The default columns for this model. More...
 

Public Member Functions

 ServiceModel (ServiceBrowser *browser, QObject *parent=nullptr)
 Creates a model for the given service browser and starts browsing for services.
 
int columnCount (const QModelIndex &parent=QModelIndex()) const override
 
QVariant data (const QModelIndex &index, int role=Qt::DisplayRole) const override
 
virtual bool hasIndex (int row, int column, const QModelIndex &parent) const
 
QVariant headerData (int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
 
QModelIndex index (int row, int column, const QModelIndex &parent=QModelIndex()) const override
 
QModelIndex parent (const QModelIndex &index) const override
 
int rowCount (const QModelIndex &parent=QModelIndex()) const override
 

Detailed Description

Model for list of Zeroconf services.

This class provides a Qt Model for ServiceBrowser to allow easy integration of service discovery into a GUI. For example, to show the HTTP servers published on the local network, you can do:

KDNSSD::ServiceModel *serviceModel = new ServiceModel(
new KDNSSD::ServiceBrowser("_http._tcp")
);
QComboBox *serviceCombo = new QComboBox();
serviceCombo->setModel(serviceModel);
Browses for network services advertised over DNS-SD.
Definition servicebrowser.h:55
Model for list of Zeroconf services.
Definition servicemodel.h:54

After the user makes a selection, the application typically needs to get a pointer to the selected service in order to get the host name and port. A RemoteService::Ptr can be obtained from a QModelIndex using:

void onSelected(const QModelIndex &selection) {
KDNSSD::RemoteService::Ptr service =
.value<KDNSSD::RemoteService::Ptr>();
}
@ ServicePtrRole
gets a RemoteService::Ptr for the service
Definition servicemodel.h:60
Since
4.1
Author
Jakub Stachowski

Member Enumeration Documentation

◆ AdditionalRoles

The additional data roles provided by this model.

Enumerator
ServicePtrRole 

gets a RemoteService::Ptr for the service

◆ ModelColumns

The default columns for this model.

If service browser is not set to resolve automatically, then the model will only ever have one column (the service name).

Constructor & Destructor Documentation

◆ ServiceModel()

KDNSSD::ServiceModel::ServiceModel ( ServiceBrowser browser,
QObject *  parent = nullptr 
)
explicit

Creates a model for the given service browser and starts browsing for services.

The model takes ownership of the browser, so there is no need to delete it afterwards.

You should not call ServiceBrowser::startBrowse() on browser before passing it to ServiceModel.

Member Function Documentation

◆ columnCount()

int KDNSSD::ServiceModel::columnCount ( const QModelIndex &  parent = QModelIndex()) const
override
Reimplemented from superclass.

◆ data()

QVariant KDNSSD::ServiceModel::data ( const QModelIndex &  index,
int  role = Qt::DisplayRole 
) const
override
Reimplemented from superclass.

◆ hasIndex()

virtual bool KDNSSD::ServiceModel::hasIndex ( int  row,
int  column,
const QModelIndex &  parent 
) const
virtual
Reimplemented from superclass.

◆ headerData()

QVariant KDNSSD::ServiceModel::headerData ( int  section,
Qt::Orientation  orientation,
int  role = Qt::DisplayRole 
) const
override
Reimplemented from superclass.

◆ index()

QModelIndex KDNSSD::ServiceModel::index ( int  row,
int  column,
const QModelIndex &  parent = QModelIndex() 
) const
override
Reimplemented from superclass.

◆ parent()

QModelIndex KDNSSD::ServiceModel::parent ( const QModelIndex &  index) const
override
Reimplemented from superclass.

◆ rowCount()

int KDNSSD::ServiceModel::rowCount ( const QModelIndex &  parent = QModelIndex()) const
override
Reimplemented from superclass.