KDNSSD::ServiceModel Class

class KDNSSD::ServiceModel

Model for list of Zeroconf services. More...

Header: #include <KDNSSD/ServiceModel>
CMake: find_package(KF6 REQUIRED COMPONENTS DNSSD)
target_link_libraries(mytarget PRIVATE KF6::DNSSD)
Inherits: QAbstractItemModel

Public Types

enum AdditionalRoles { ServicePtrRole }
enum ModelColumns { ServiceName, Host, Port }

Public Functions

ServiceModel(KDNSSD::ServiceBrowser *browser, QObject *parent = nullptr)
virtual bool hasIndex(int row, int column, const QModelIndex &parent) const

Detailed Description

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);

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 =
         selection.data(KDNSSD::ServiceModel::ServicePtrRole)
                  .value<KDNSSD::RemoteService::Ptr>();
 }

Member Type Documentation

enum ServiceModel::AdditionalRoles

The additional data roles provided by this model.

ConstantValueDescription
KDNSSD::ServiceModel::ServicePtrRole0x7E6519DEGets a RemoteService::Ptr for the service.

enum ServiceModel::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).

ConstantValueDescription
KDNSSD::ServiceModel::ServiceName0The name of this service.
KDNSSD::ServiceModel::Host1The hostname or IP address the service is hosted on.
KDNSSD::ServiceModel::Port2The TCP or UDP port the service is hosted on.

Member Function Documentation

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

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

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

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

[virtual] bool ServiceModel::hasIndex(int row, int column, const QModelIndex &parent) const