Qt Simulator Manual 1.2.0

Adding Views

You can write plugins to add views that contain your own controls to Qt Simulator. The plugins display the controls and act as servers that accept connections from the simulated applications. You must also write a target library or application that receives data from the controls.

Writing Qt Simulator Plugins

Write a simple plugin as described in How to Create Qt Plugins.

The plugin class has a single function that returns all views to show in Qt Simulator, as illustrated by the following code snippet:

 class ExamplePlugin : public QObject, public PluginInterface
 {
     Q_OBJECT
     Q_INTERFACES(PluginInterface)

 public:
     QList<ToolBoxPage *> pages(QWidget *);
 };

The following code snippet illustrates how to return a view created from the ExamplePage class:

 QList<ToolBoxPage *> ExamplePlugin::pages(QWidget *parent)
 {
     return QList<ToolBoxPage *>() << new ExamplePage(parent);
 }

 Q_EXPORT_PLUGIN2(example_plugin, ExamplePlugin)

Creating Views

The implementation of a Qt Simulator view must contain all the visible widgets. The following code snippet implements a view that contains an editable QLineEdit widget:

 class ExamplePage : public ToolBoxPage
 {
     Q_OBJECT

 public:
     explicit ExamplePage(QWidget *parent = 0);
     virtual ~ExamplePage();

     void writeSettings(QSettings &) const;
     void readSettings(QSettings &);

     QIcon icon() const;

 public slots:
     void textEdited(const QString &);
     void showText(const QString &);

 private slots:
     void newConnection(Simulator::ConnectionWorker *);
     void disconnected();

 private:
     Simulator::Connection *mConnection;
     QLineEdit *target;
     QList<Simulator::ConnectionWorker *> mWorkers;
 };

The constructor sets up a QWidget that contains the contents to display. The connection server is set up using the Connection class:

 ExamplePage::ExamplePage(QWidget *parent)
     : ToolBoxPage(parent)
     , mConnection(0)
 {
     QList<OptionsItem *> optionsList;
     target = new QLineEdit(this);
     optionsList << new OptionsItem(tr("Example control"), target);
     setOptions(optionsList);

     setTitle(tr("Example Page"));

     mConnection = new Connection(Connection::Server, "example", Version(1,0,0,0), this);

     connect(target, SIGNAL(textEdited ( const QString &)), this, SLOT(textEdited(const QString &)));
     connect(mConnection, SIGNAL(newConnection(Simulator::ConnectionWorker *)), this, SLOT(newConnection(Simulator::ConnectionWorker *)));
 }

The following code snippet is an example of a slot that is called when the text in QLineEdit changes. The slot notifies the connected clients about the change. If multiple clients are connected, all of them have to be notified:

 void ExamplePage::textEdited(const QString &text)
 {
     foreach (ConnectionWorker *worker, mWorkers)
         worker->call("showText", text);
 }

Reimplement the following functions to store persistent values in Qt Simulator configuration. Make sure you call the parent functions as early as possible, because ToolBoxPage needs to save its window state:

 void ExamplePage::writeSettings(QSettings &settings) const
 {
     ToolBoxPage::writeSettings(settings);

     settings.beginGroup(title());
     settings.setValue("myValue", 5);
     settings.endGroup();
 }

 void ExamplePage::readSettings(QSettings &settings)
 {
     ToolBoxPage::readSettings(settings);

     settings.beginGroup(title());
     if (settings.contains("myValue"))
         int myValue = settings.value("myValue").toInt();
     settings.endGroup();
 }

The server uses the following two slots to manage the connected clients by connecting and disconnecting them as necessary:

 void ExamplePage::newConnection(ConnectionWorker *worker)
 {
     connect(worker, SIGNAL(disconnected()), this, SLOT(disconnected()));
     mWorkers.append(worker);
     worker->addReceiver(this);
 }

 void ExamplePage::disconnected()
 {
     ConnectionWorker *worker = qobject_cast<ConnectionWorker *>(sender());
     worker->deleteLater();
     mWorkers.removeAll(worker);
 }

Qt Simulator displays icons that you can select to open views. To display an icon for your view, implement the function QIcon icon() const and make sure that it returns a valid icon with the size of 32x32 pixels. If you do not implement the function, the name of the page is used instead. You can set the page name using void setName(const QString &). The name is mainly used in the Menu.

Writing Client Applications

The example application shows a single QLineEdit.

The following class implements a standard QWidget:

 class ExampleApp : public QWidget
 {
     Q_OBJECT

 public:
     explicit ExampleApp(QWidget *parent = 0);
     virtual ~ExampleApp();

 public slots:
     void showText(const QString &);
     void textEdited(const QString &);

 private:
     QLineEdit *mLineEdit;
     Simulator::Connection *mConnection;
     Simulator::ConnectionWorker *mWorker;
 };

The only thing that is special about the constructor is that it creates a connection to the server. The resulting ConnectionWorker has to be saved:

 ExampleApp::ExampleApp(QWidget *parent)
     : QWidget(parent)
     , mConnection(new Connection(Connection::Client, "example", Version(1,0,0,0)))
 {
     mWorker = mConnection->connectToServer();
     mWorker->addReceiver(this);

     QHBoxLayout *layout = new QHBoxLayout(this);
     mLineEdit = new QLineEdit(this);
     layout->addWidget(mLineEdit);
     setLayout(layout);
     connect(mLineEdit, SIGNAL(textEdited ( const QString &)), this, SLOT(textEdited(const QString &)));
 }

The showText function is called remotely when the text in the control widget changes, and the text is updated locally:

 void ExampleApp::showText(const QString &text)
 {
   mLineEdit->setText(text);
   qWarning() << "showText:" << text;
 }

When the text is edited locally, a remote call is made to update the control widget page:

 void ExampleApp::textEdited(const QString &text)
 {
       mWorker->call("showText", text);
 }
X

Thank you for giving your feedback.

Make sure it is related to this specific page. For more general bugs and requests, please use the Qt Bug Tracker.

[0]; s.parentNode.insertBefore(ga, s); })();