Publish and Subscribe Example |
Parameter | Description |
---|---|
-server | Starts the Value Space Server. Only one instance should be started with the this parameter. This parameter is only needed if the Value Space will use a layer that has a client-server architecture and a server needs to be started. |
-publisher | Publish values in the Value Space and create a dialog to control those values directly. |
-subscriber | Create a dialog to view Value Space items. |
When acting as a publisher of Value Space content the program creates a PublisherDialog. This dialog contains a widget for setting the base path of the QValueSpacePublisher and widgets for setting the value of the two fixed attributes 'intValue' and 'stringValue'.
We declare three slots which we will use to respond to user input in the dialog.
private slots: void createNewObject(); void intValueChanged(int value); void setStringValue(); void setByteArrayValue();
And connect them to signals from the widgets in the constructor.
connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(createNewObject())); connect(ui->intValue, SIGNAL(valueChanged(int)), this, SLOT(intValueChanged(int))); connect(ui->setStringButton, SIGNAL(clicked()), this, SLOT(setStringValue())); connect(ui->setByteArrayButton, SIGNAL(clicked()), this, SLOT(setByteArrayValue()));
To publish values in the Value Space we first need to create a QValueSpacePublisher. We do this in the createNewObject() slot, which is also used to handle changes in the base path. After the object is constructed we publish the initial values by explicitly calling the other two slots.
void PublisherDialog::createNewObject() { if (publisher) delete publisher; publisher = new QValueSpacePublisher(ui->basePath->text()); intValueChanged(ui->intValue->value()); setStringValue(); setByteArrayValue(); }
We use QValueSpacePublisher::setValue() to set the values in response to dialog input.
void PublisherDialog::intValueChanged(int value) { publisher->setValue("intValue", value); } void PublisherDialog::setStringValue() { publisher->setValue("stringValue", ui->stringValue->text()); } void PublisherDialog::setByteArrayValue() { publisher->setValue("byteArrayValue", ui->byteArrayValue->text().toAscii()); }
As a final step in the constructor we explicitly call the createNewObject() slot to create an initial QValueSpacePublisher.
createNewObject();
When acting as a subscriber of Value Space content the program creates a SubscriberDialog. This dialog contains a widget for setting the base path of the QValueSpaceSubscriber and a table for displaying all of the values directly under base path in the Value Space.
We declare two slots to propagate values from the Value Space to the dialog in response to change notifications from QValueSpaceSubscriber.
private slots: void changeSubscriberPath(); void subscriberChanged();
To read values from the Value Space we first create a QValueSpaceSubscriber and connect to the contentsChanged() signal.
We explicitly call our itemChanged() slot to populate the table with the initial values.
void SubscriberDialog::changeSubscriberPath() { ui->values->clearContents(); if (!subscriber) subscriber = new QValueSpaceSubscriber(ui->basePath->text(), this); else subscriber->setPath(ui->basePath->text()); connect(subscriber, SIGNAL(contentsChanged()), this, SLOT(subscriberChanged())); subscriberChanged(); }
In response to the contentsChanged() signal we update all the values in the table.
void SubscriberDialog::subscriberChanged() { ui->values->clearContents(); QStringList subPaths = subscriber->subPaths(); ui->values->setRowCount(subPaths.count()); for (int i = 0; i < subPaths.count(); ++i) { QVariant v = subscriber->value(subPaths.at(i)); QTableWidgetItem *pathItem = new QTableWidgetItem(subPaths.at(i)); QTableWidgetItem *valueItem = new QTableWidgetItem(v.toString()); QTableWidgetItem *typeItem = new QTableWidgetItem(v.typeName()); ui->values->setItem(i, 0, pathItem); ui->values->setItem(i, 1, valueItem); ui->values->setItem(i, 2, typeItem); } }
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt qtmobility-1.0-tp | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com