aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier.tilloy@canonical.com>2012-10-09 15:52:06 +0200
committerOlivier Tilloy <olivier.tilloy@canonical.com>2012-10-09 15:52:06 +0200
commit69538ac36e10b514576bcb61d413ebac45b63f0d (patch)
tree53421411892f01739e95aac43852feeaae4ddf65 /examples
parent35dfc73fc832d83bd1ef0caa583fbdbe0d6a397b (diff)
downloadqmenumodel-69538ac36e10b514576bcb61d413ebac45b63f0d.tar.gz
qmenumodel-69538ac36e10b514576bcb61d413ebac45b63f0d.tar.bz2
qmenumodel-69538ac36e10b514576bcb61d413ebac45b63f0d.zip
Add a C++ example that demonstrates the use of libqmenumodel.
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt20
-rw-r--r--examples/README16
-rw-r--r--examples/info-menumodel.cpp87
3 files changed, 121 insertions, 2 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
new file mode 100644
index 0000000..028c6ef
--- /dev/null
+++ b/examples/CMakeLists.txt
@@ -0,0 +1,20 @@
+# Standalone CMakeLists.txt to generate the info-menumodel executable.
+# This is not built as part of libqmenumodel, it has to be built separately
+# and it assumes libqmenumodel-dev is installed on the system.
+
+cmake_minimum_required(VERSION 2.8.9)
+
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+set(CMAKE_AUTOMOC ON)
+
+find_package(Qt5Core REQUIRED)
+
+include(FindPkgConfig)
+pkg_check_modules(QMENUMODEL REQUIRED qmenumodel)
+
+add_executable(info-menumodel info-menumodel.cpp)
+
+qt5_use_modules(info-menumodel Core)
+include_directories(${QMENUMODEL_INCLUDE_DIRS})
+target_link_libraries(info-menumodel ${QMENUMODEL_LDFLAGS})
+
diff --git a/examples/README b/examples/README
index 1ecac0e..ab683f3 100644
--- a/examples/README
+++ b/examples/README
@@ -1,11 +1,23 @@
This directory contains examples that demonstrate how to use the QMenuModel
-QML binding in applications.
+binding in Qt and QML applications.
+
To export an example menu model on the bus, run 'exportmenu.py'.
+
To render this menu in a QML application, run 'render-menumodel.qml' in
qmlscene. You will need to inform qmlscene of the location of the QMenuModel
plugin if it’s not installed system-wide, e.g.:
- qmlscene -I libqmenumodel examples/render-menumodel.qml
+ $ qmlscene -I libqmenumodel examples/render-menumodel.qml
+
+
+To compile a C++ executable that uses libqmenumodel to monitor and print
+information about the exported menu, run `cmake` and then `make` from this
+directory. This assumes that libqmenumodel-dev is installed on the system
+(it won’t work with a local copy). Then, run the executable generated:
+
+ $ cmake .
+ $ make
+ $ ./info-menumodel
diff --git a/examples/info-menumodel.cpp b/examples/info-menumodel.cpp
new file mode 100644
index 0000000..d370731
--- /dev/null
+++ b/examples/info-menumodel.cpp
@@ -0,0 +1,87 @@
+/*
+ * Example executable that links against libqmenumodel to monitor
+ * and print information about a menu model exported on D-Bus.
+ */
+
+// QMenuModel
+#include "qmenumodel/qdbusmenumodel.h"
+
+// stdlib
+#include <iostream>
+
+// Qt
+#include <QtCore/QCoreApplication>
+#include <QtCore/QObject>
+
+
+#define BUS_NAME "com.canonical.testmenu"
+#define BUS_OBJECT_PATH "/com/canonical/testmenu"
+
+
+class MenuModelMonitor : public QDBusMenuModel
+{
+ Q_OBJECT
+
+public:
+ MenuModelMonitor(QObject* parent=0)
+ : QDBusMenuModel(parent)
+ {
+ setProperty("busType", DBusEnums::SessionBus);
+ setProperty("busName", BUS_NAME);
+ setProperty("objectPath", BUS_OBJECT_PATH);
+ QObject::connect(this, SIGNAL(statusChanged(DBusEnums::ConnectionStatus)),
+ SLOT(onStatusChanged(DBusEnums::ConnectionStatus)));
+ QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
+ SLOT(onModelChanged()));
+ QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
+ SLOT(onModelChanged()));
+ std::cout << "Monitoring menu model "
+ << property("objectPath").toString().toUtf8().constData()
+ << " on the well-known name "
+ << property("busName").toString().toUtf8().constData()
+ << std::endl;
+ }
+
+private Q_SLOTS:
+ void onStatusChanged(DBusEnums::ConnectionStatus status)
+ {
+ std::cout << "Status of menu model changed to " << status << std::endl;
+ if (status == DBusEnums::Connected) {
+ printModel(this);
+ }
+ }
+
+ void onModelChanged()
+ {
+ printModel(this);
+ }
+
+private:
+ void printModel(QMenuModel* model, int indent=0)
+ {
+ int count = model->rowCount();
+ for (int i = 0; i < count; ++i) {
+ QModelIndex index = model->index(i, 0);
+ QString label = model->data(index, QMenuModel::Label).toString();
+ QVariant submenu = model->data(index, QMenuModel::LinkSubMenu);
+ for (int j = 0; j < indent * 2; ++j) std::cout << " ";
+ std::cout << " > " << label.toUtf8().constData() << std::endl;
+ if (submenu.isValid()) {
+ printModel(qobject_cast<QMenuModel*>(submenu.value<QObject*>()), indent + 1);
+ }
+ }
+ }
+};
+
+#include "info-menumodel.moc"
+
+
+int main(int argc, char** argv)
+{
+ MenuModelMonitor monitor;
+ monitor.start();
+ int result = QCoreApplication(argc, argv).exec();
+ monitor.stop();
+ return result;
+}
+