How to start video-suite in MeeGo / Nokia N9 from Qt code? - qt

I am having problems with launching Nokia's own video player from my application that I just don't seem to be able to solve.
My first attempt included calling
Qt.openUrlExternally(url)
from QML and that seemed to do the trick just fine, except that it opened the browser every time and used it instead of the video-suite (native player).
Next I tried cuteTube -approach where I start new process like this:
QStringList args;
args << url;
QProcess *player = new QProcess();
connect(player, SIGNAL(finished(int, QProcess::ExitStatus)), player, SLOT(deleteLater()));
player->start("/usr/bin/video-suite", args);
That worked, except that it required video-suite to be closed upon calling player->start, otherwise it did nothing.
My third attempt involved starting the video-suite via QDBus, but that didn't work any better:
QList<QVariant> args;
QStringList urls;
urls << url;
args.append(urls);
QDBusMessage message = QDBusMessage::createMethodCall(
"com.nokia.VideoSuite",
"/",
"com.nokia.maemo.meegotouch.VideoSuiteInterface",
"play");
message.setArguments(args);
message.setAutoStartService(true);
QDBusConnection bus = QDBusConnection::sessionBus();
if (bus.isConnected()) {
bus.send(message);
} else {
qDebug() << "Error, QDBus is not connected";
}
The problem with this is that it requires video-suite to be up and running - autoStartService parameter didn't help either. If video-suite isn't running already, the call opens it just fine but, alas, no video starts to play.
Eventually I tried using also VideoSuiteInterface, but even having the program compile with it seemed to be difficult. When I eventually managed to compile and link all relevant libraries, the results didn't differ from option 3 above.
So, is there a way to use either VideoSuiteInterface directly or via DBus so that it would start video playback regardless of the current state of the application?

The solution was actually simpler than I really thought initially; the VideoSuiteInterface -approach worked after all. All it took was to use it properly. Here are the full sources should anyone want to try it themselves.
player.h:
#ifndef PLAYER_H
#define PLAYER_H
#include <QObject>
#include <maemo-meegotouch-interfaces/videosuiteinterface.h>
class Player : public QObject {
Q_OBJECT
private:
VideoSuiteInterface* videosuite;
public:
Player(QObject *parent = 0);
Q_INVOKABLE void play(QString url);
};
#endif // PLAYER_H
player.cpp:
#include "player.h"
#include <QObject>
#include <QStringList>
#include <QtDeclarative>
Player::Player(QObject *parent) : QObject(parent) {}
void Player::play(QString url) {
QList<QVariant> args;
QStringList urls;
urls << url;
args.append(urls);
videosuite = new VideoSuiteInterface();
videosuite->play(urls);
}
In addition you may want to connect some signals to make the UI more responsive, but basically that should do the trick.
Finally, you need to remember to add following to your .pro file and you are good to go:
CONFIG += videosuiteinterface-maemo-meegotouch

Related

QApplication recover from segmentation fault

I want to be able to recover from a Segmentation Fault in MyApplication by catching the SIGSEGV and restarting QApplication. So for testing purposes I'm injecting a segmentation fault in my code.
The issue is that the signal handler that catches the SIGSEGV is getting a non-stop stream of SIGSEGVs. At first I thought it was the while loop in my main but it still happens even though I comment out the while loop. So my questions are simple: Is it even possible to recover from a Segmentation Fault in Qt? Why am I getting rolling SIGSEGVs non-stop?
#include <QApplication>
#include <QDebug>
#include "MyApplication.h"
#include <initializer_list>
#include <signal.h>
#include <unistd.h>
#include <stdio.h>
#define RESTART_CODE 1000
void catchUnixSignals(std::initializer_list<int> quitSignals)
{
auto handler = [](int sig) -> void
{
if (sig == SIGSEGV)
{
QCoreApplication::exit(RESTART_CODE);
}
else
{
QCoreApplication::quit();
}
};
sigset_t blocking_mask;
sigemptyset(&blocking_mask);
for (auto sig : quitSignals)
sigaddset(&blocking_mask, sig);
struct sigaction sa;
sa.sa_handler = handler;
sa.sa_mask = blocking_mask;
sa.sa_flags = 0;
for (auto sig : quitSignals)
sigaction(sig, &sa, nullptr);
}
int main(int argc, char *argv[])
{
catchUnixSignals({SIGSEGV, SIGQUIT, SIGINT, SIGTERM, SIGHUP, SIGKILL});
int i = 0;
do
{
QApplication app(argc, argv);
MyApp myapp;
MyApp.start();
app.exec();
if (app.exec() != RESTART_CODE) break;
} while(1);
return 0;
}
This does not directly answers your question, but is another way to achieve similar behavior.
To recover from a segmentation fault, an option is to use a watchdog, i.e. another independent process that checks the status of your main software and restarts it when needed.
When you start your software, you create another process that runs a 2nd software, the watchdog. Ensure to start it in "detached" mode to avoid that it gets closed if your main software crashes.
In the watchdog, frequently call "tasklist" on Windows or "ps" or "top" on Linux and parse the output to check whether your software is still running. OR, use a UDP or TCP port to communicate between the main software and the watchdog, to tell the watchdog that the main software is still running well.
In the watchdog, if the main software is no longer running, restart the main software process (also in detached).
CAUTION: You need to manage the case where the main software is exited correctly. In that case the main software should either kill the watchdog itself when exiting normally (calling "kill" on the pid), or send it a message so that the watchdog exits as well.

QML Components Library [duplicate]

I have 4 qml files and one main.cpp to load qml file.
Is it possible for me to create 1 dll file for those 4 qml file.
And use it in different application if so how to do that.
As already said, there is no need for embedding qml files only in a library. But of course you have the right to do all you want, even that. I know at least 2 ways to do that:
1. Create binary resource file
Prepare resource file containing qml files and then compile it:
rcc -binary plugin.qrc -o plugin.rcc
Now you can include this file into your application :
QResource::registerResource("plugin.rcc");
and use it as regular qrc file:
QResource::registerResource(qApp->applicationDirPath() + "/plugin.rcc");
QQuickView *view = new QQuickView();
view->setSource(QUrl("qrc:/qml/myfile.qml"));
Here qml/ is prefix in resource file.
2. Shared libraryAnother way is to create a shared library containing the same resource file. For example your plugin's shared library implements following interface:
interface.h
#ifndef PLUGIN_INTERFACE_H
#define PLUGIN_INTERFACE_H
#include <QString>
#include <QObject>
class PluginInterface
{
public:
virtual ~PluginInterface() {}
virtual QByteArray getQML(const QString &name) = 0;
};
#define PluginInterface_iid "org.qt-project.PluginInterface"
Q_DECLARE_INTERFACE(PluginInterface, PluginInterface_iid)
#endif
and its implementation is:
QByteArray PluginImpl::getQML(const QString &name)
{
QFile file(":/qml/" + name);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return QByteArray();
return file.readAll();
}
Now, in your application you load your plugin and get its resource as a string:
QDir pluginsDir(qApp->applicationDirPath());
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath("plugin.dll"));
QObject *plugin = pluginLoader.instance();
if (plugin) {
PluginInterface *pluginInstance = qobject_cast<PluginInterface *>(plugin);
if (pluginInstance) {
QByteArray content = pluginInstance->getQML("file1.qml");
QQuickView *view = new QQuickView();
QQmlComponent component(view->engine());
component.setData(content, QUrl());
QQuickItem *childItem = qobject_cast<QQuickItem*>(component.create());
childItem->setParentItem(view->contentItem());
QWidget *container = QWidget::createWindowContainer(view);
container->setFocusPolicy(Qt::TabFocus);
ui->verticalLayout->addWidget(container);
}
}
But pay attention, when you deploy your application you anyway have to copy all qml system files, like #QTPATH/qml/QtQml, #QTPATH/qml/QtQuick.2, #QTPATH/qml/QtQuick.2 etc.
Links:
Resource compiler
Same theme
Plugin example
Have a look at the documentation for QML Modules
There are options for QML-only modules, C++ only and mixed mode.

Qt C++ Q_OBJECT error undefined reference to vtable

I am constantly running into problems while using the Q_OBJECT macro: (I use QT Creator 2.8.1 / Qt 4.8.4) I asked before but it seems to be leading to even more trouble. Can anybody help me? I am totally lost .
I have a huge C++ program with about 50+ classes to adapt to new needs.
Now I created a new (very simple) parent-class and 3 child classes in a new directory within the src-directory. To do so I used the template Qt->Qt Designer Form Class.( I did that because this automatically implements Q_OBJECT even though I do not need a *.ui-file. I then removed all concerning ui-Fileand the ui-file itself))
When I run my program I always get lots of „ undefined reference to vtable for“ ..-errors. When I remove all Q_OBJECT my program runs OK. But then I am not able to use signal-slots which I would need later on.
I looked it up in the internet and found out it has something to do with the .pro-file/.o-Files in my build-directory. I (several times) tried to delete all .o-Files including the .pro.user and compile again. Sometimes I still got the error, sometimes not.
This is my code ( the 3 child classes are the same at the moment):
geometry.h:
#ifndef GEOMETRY_H
#define GEOMETRY_H
#include <QMetaType>
#include <QWidget>
#include <QObject>
#include <QDebug>
class Geometry
{
Q_OBJECT
protected:
public:
Geometry();
virtual ~Geometry(void) {}
virtual void write_LNE();
//Q_DECLARE_METATYPE(Geometry);
#endif // GEOMETRY_H
-
geometry.cpp:
#include "geometry.h"
Geometry::Geometry()
{ qDebug() << "Constructor: hier ist Geometry"; }
void Geometry::Haupt()
{ qDebug() << " Das hier ist die Haupt von Geometry ....." ; }
void Geometry::write_LNE(){}
-
Geo_1PF.h:
#ifndef GEO_1PF_H
#define GEO_1PF_H
#include "geometry.h"
class Geo_1PF : public Geometry
{
Q_OBJECT
public:
Geo_1PF();
~Geo_1PF() {}
virtual void write_LNE();
};
//Q_DECLARE_METATYPE(Geo_1PF);
#endif // GEO_1PF_H
Geo_1PF.cpp:
#include "Geo_1PF.h"
Geo_1PF::Geo_1PF()
{
}
I found the advice to do qmake manually. I never used qmake manually.
How and from which directory do I do this ? Exactly what do I write qmake …….?
Is it correct to use the template Qt->Qt Designer Form Class to create these classes?
Do I have to create the classes in another directory?
Are there any additional entries I have to make in the +.pro-File
and where in the file do they have tob be put?
Do I have to change anything in my makefile? And if so what?
Thank you
If you use Qt Creator:
Everytime you create a class using Q_OBJECT,
Build → Run qmake
Build → Rebuild All
To use the QOBJECT macro in your class you need to extend QObject.
class MyObject: public QObject
{
Q_OBJECT
public:
MyObject (QObject *_parent);
.....
};

How create shared library in QT/QML

I have 4 qml files and one main.cpp to load qml file.
Is it possible for me to create 1 dll file for those 4 qml file.
And use it in different application if so how to do that.
As already said, there is no need for embedding qml files only in a library. But of course you have the right to do all you want, even that. I know at least 2 ways to do that:
1. Create binary resource file
Prepare resource file containing qml files and then compile it:
rcc -binary plugin.qrc -o plugin.rcc
Now you can include this file into your application :
QResource::registerResource("plugin.rcc");
and use it as regular qrc file:
QResource::registerResource(qApp->applicationDirPath() + "/plugin.rcc");
QQuickView *view = new QQuickView();
view->setSource(QUrl("qrc:/qml/myfile.qml"));
Here qml/ is prefix in resource file.
2. Shared libraryAnother way is to create a shared library containing the same resource file. For example your plugin's shared library implements following interface:
interface.h
#ifndef PLUGIN_INTERFACE_H
#define PLUGIN_INTERFACE_H
#include <QString>
#include <QObject>
class PluginInterface
{
public:
virtual ~PluginInterface() {}
virtual QByteArray getQML(const QString &name) = 0;
};
#define PluginInterface_iid "org.qt-project.PluginInterface"
Q_DECLARE_INTERFACE(PluginInterface, PluginInterface_iid)
#endif
and its implementation is:
QByteArray PluginImpl::getQML(const QString &name)
{
QFile file(":/qml/" + name);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return QByteArray();
return file.readAll();
}
Now, in your application you load your plugin and get its resource as a string:
QDir pluginsDir(qApp->applicationDirPath());
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath("plugin.dll"));
QObject *plugin = pluginLoader.instance();
if (plugin) {
PluginInterface *pluginInstance = qobject_cast<PluginInterface *>(plugin);
if (pluginInstance) {
QByteArray content = pluginInstance->getQML("file1.qml");
QQuickView *view = new QQuickView();
QQmlComponent component(view->engine());
component.setData(content, QUrl());
QQuickItem *childItem = qobject_cast<QQuickItem*>(component.create());
childItem->setParentItem(view->contentItem());
QWidget *container = QWidget::createWindowContainer(view);
container->setFocusPolicy(Qt::TabFocus);
ui->verticalLayout->addWidget(container);
}
}
But pay attention, when you deploy your application you anyway have to copy all qml system files, like #QTPATH/qml/QtQml, #QTPATH/qml/QtQuick.2, #QTPATH/qml/QtQuick.2 etc.
Links:
Resource compiler
Same theme
Plugin example
Have a look at the documentation for QML Modules
There are options for QML-only modules, C++ only and mixed mode.

QtDBus Simply Example With PowerManager

I'm trying to use QtDbus to communicate with interface provided by PowerManager in my system. My goal is very simple. I will be writing code which causes my system to hibernate using DBus interface.
So, I installed d-feet application to see what interfaces DBus is available on my system, and what I saw:
As we see, I have a few interfaces and methods from which I can choose something. My choice is Hibernate(), from interface org.freedesktop.PowerManagment
In this goal I prepared some extremely simple code to only understand mechanism. I of course used Qt library:
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QtCore/QStringList>
#include <QtDBus/QtDBus>
#include <QDBusInterface>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
if (!QDBusConnection::sessionBus().isConnected()) {
fprintf(stderr, "Cannot connect to the D-Bus session bus.\n"
"To start it, run:\n"
"\teval `dbus-launch --auto-syntax`\n");
return 1;
}
QDBusInterface iface("org.freedesktop.PowerManagement" ,"/" , "" , QDBusConnection::sessionBus());
if(iface.isValid())
{
qDebug() << "Is good";
QDBusReply<QString> reply = iface.call("Methods" , "Hibernate");
if(reply.isValid())
{
qDebug() << "Hibernate by by " << qPrintable(reply.value());
}
qDebug() << "some error " << qPrintable(reply.error().message());
}
return 0;
}
Unfortunately I get error in my terminal:
Is good
some error Method "Methods" with signature "s" on interface "(null)" doesn't exist
So please tell me what's wrong with this code? I am sure that I forgot some arguments in function QDBusInterface::call() but what ?
When creating interface you have to specify correct interface, path, service. So that's why your iface object should be created like this:
QDBusInterface iface("org.freedesktop.PowerManagement", // from list on left
"/org/freedesktop/PowerManagement", // from first line of screenshot
"org.freedesktop.PowerManagement", // from above Methods
QDBusConnection::sessionBus());
Moreover, when calling a method you need to use it's name and arguments (if any):
iface.call("Hibernate");
And Hibernate() doesn't have an output argument, so you have to use QDBusReply<void> and you can't check for .value()
QDBusReply<void> reply = iface.call("Hibernate");
if(reply.isValid())
{
// reply.value() is not valid here
}

Resources