QT C++ wait till specific time to execute function - qt

I am trying to create an app that holds a list of tasks and for each time a deadline, now i want to execute a function (show a popup) once a deadline is met.
i have this:
#ifndef TIMER_H
#define TIMER_H
#include <QWidget>
#include <QTimer>
#include <QtGui>
#include <QObject>
class Timer : public QWidget
{
Q_OBJECT
public:
Timer(QWidget * parent = 0);
void setTimer(QString title, QString description, QDate date, QTime reminderTime);
public slots:
void showWarning() {QString show = tit;
QPushButton * thanks = new QPushButton(QObject::tr("Thank you for reminding me!"));
show.append("\n");
show.append(des);
QMessageBox popup;
popup.setText(show);
popup.setWindowTitle("Calendar : Reminder");
popup.setDefaultButton(thanks);
popup.exec();
}
private:
QString tit;
QString des;
QDateTime now;
QDateTime timeoftheaction;
QTimer *timer;
};
cpp file:
#endif // TIMER_H
#include "timer.h"
#include <iostream>
using namespace std;
Timer::Timer(QWidget * parent)
: QWidget(parent)
{
}
void Timer::setTimer(QString title, QString description, QDate date, QTime reminderTime)
{
now.currentDateTime();
timer = new QTimer;
tit = title;
des = description;
timeoftheaction.setDate(date);
timeoftheaction.setTime(reminderTime);
connect(timer, SIGNAL(timeout()),this,SLOT(showWarning()));
timer->start(now.secsTo(timeoftheaction)*1000);
}
Yet function showWarning is never being called...
no compilation errors, function showWarning works perfectly (tested)
I think the error is in the connect but i am not sure...

Short answer:
Change:
now.currentDateTime();
to
now = QDateTime::currentDateTime();
Longish answer:
currentDateTime() is a static function which instead of changing your existing object, actually returns a new QDataTime object. Although you are calling it as a member function, it's still called as a static one and leaves your object intact, which is still invalid.
Your later call to secsTo() on an invalid data time probably gets you an negative or really large number that either has passed (never going to trigger) or really late in the future.

Here is something that might be a more generic solution.
#include <QThread>
#include <QTimer>
#include <QObject>
#include <map>
/**
* Singleton to implement simple 'relative' timer.
* Implements busy wait and also timeout-notifications (useful to monitor operations that could hang, etc).
*
* If the whole application is stalled (e.g. when a new device is connected), and we only want to
* wait for a period during which application was 'really' working (not just hanging waiting for OS)
* - then ticks will be missed too. This way - it's should be possible to avoid unnecessary timeouts
* that could happen if global time was measured (especially annoying on WINdows platforms)
*/
class RelativeTimer : public QObject
{
Q_OBJECT
typedef std::multimap <unsigned int, std::pair <QObject*, QString> > Notifications;
public:
/**
* Call to busy-wait for number of ticks.
*/
static void wait_num_of_ticks(unsigned int num_of_ticks_to_wait)
{
if(self.timer_id == 0)
{
qDebug("timer not initialised, call 'RelativeTimer::Init()'");
return;
}
if(num_of_ticks_to_wait > 0)
{
unsigned long until = self.tick_counter + num_of_ticks_to_wait; // it's ok if it wraps around..
while(self.tick_counter != until)
{
QCoreApplication::processEvents(); // let others to their job..
// or comment above out and just busy wait..
}
}
}
/**
* Call to busy-wait until ms_to_wait have elapsed.
* If ms_to_wait is < tick period
* Interval will define 'tick' frequency (and accuracy).
*/
static void wait_ms(unsigned int ms_to_wait)
{
wait_num_of_ticks(num_of_ticks_to_wait(ms_to_wait));
}
/**
* Call to schedule a notification after a given timeout.
* returns notification_id that can be used to cancel this notification.
*/
static unsigned long notify_timeout_ms(unsigned int ms_to_wait,
QObject *receiver,
const char* method_name)
{
unsigned long ticks_to_wait = 0;
if(receiver && method_name)
{
ticks_to_wait = num_of_ticks_to_wait(ms_to_wait);
if(ticks_to_wait > 1)
{
ticks_to_wait += self.tick_counter;
if(ticks_to_wait == 0) // avoid 0 - make it one tick more (to alow to see if successfully added this notif)
{
ticks_to_wait = 1;
}
self.notifications.insert(std::make_pair(ticks_to_wait,
std::make_pair(receiver, method_name)));
qDebug("added delayed call..");
}
else
{
QMetaObject::invokeMethod(receiver, method_name, Qt::QueuedConnection);
ticks_to_wait = 0;
}
}
return ticks_to_wait;
}
/**
* Call to cancel a notification with a given id.
* Specify name if there were more notification with the same id (scheduled for the same tick).
* returns true on successfull cancellation, false otherwise.
*/
static bool cancel_timeout_notification(unsigned long notification_id, QString notification_name="")
{
bool cancelled = false;
if(self.notifications.size())
{
std::pair<Notifications::iterator, Notifications::iterator> to_cancel = self.notifications.equal_range(notification_id);
Notifications::iterator n = to_cancel.first;
for( ;n != to_cancel.second; ++n)
{
if(notification_name.size()== 0 || n->second.second == notification_name)
{
self.notifications.erase(n);
cancelled = true;
break;
}
}
}
return cancelled;
}
static const unsigned int default_tick_period_ms = 100;
/**
* Call this method after event loop is created- to initiate (re-start) timer.
* tick period defines 'tick' frequency (and accuracy of the timer)
* (note on Windows that there's no point to go down below 100ms).
*/
static void Init(unsigned int tick_period_ms = default_tick_period_ms)
{
self.moveToThread(&self.thread);
self.thread.start();
while(!self.thread.isRunning());
self.current_interval = tick_period_ms;
// InitMe() should execute in the thread context..
QMetaObject::invokeMethod(&self, "InitMe", Qt::QueuedConnection);
}
private:
/**
* Internal method to convert ms to number of ticks.
*/
static unsigned int num_of_ticks_to_wait(unsigned int ms_to_wait)
{
if(ms_to_wait > self.current_interval)
{
if(ms_to_wait % self.current_interval)
{
// average it..
ms_to_wait = ms_to_wait + self.current_interval / 2;
}
ms_to_wait /= self.current_interval;
}
else
{
ms_to_wait = 0;
}
return ms_to_wait;
}
/**
* Internal method to handle tick. Increments counter and invokes notifications.
*/
void timerEvent ( QTimerEvent* /*event*/ )
{
tick_counter++;
if(notifications.size())
{
std::pair<Notifications::iterator, Notifications::iterator> to_notify = notifications.equal_range(tick_counter);
Notifications::iterator n = to_notify.first;
for( ;n != to_notify.second; ++n)
{
QMetaObject::invokeMethod(n->second.first,
n->second.second.toStdString().c_str(),
Qt::QueuedConnection);
}
notifications.erase(to_notify.first, to_notify.second);
}
}
private slots:
/**
* Internal slot to initialize the timer. Should be called in this->timer context.
*/
void InitMe()
{
if(timer_id != 0)
{
killTimer(timer_id);
timer_id = 0;
}
tick_counter = 0;
timer_id = self.startTimer(self.current_interval);
}
private:
RelativeTimer()
{
}
~RelativeTimer()
{
thread.quit();
thread.wait();
}
QThread thread;
Notifications notifications;
int timer_id;
unsigned int current_interval;
unsigned long tick_counter;
static RelativeTimer self; // implement it as a signleton.. Define it in your C file, e.g.:
// RelativeTimer RelativeTimer::self;
};
Can be used like:
CurrQObjectClass::OnTimeout()
{
// ...
}
CurrQObjectClass::SomeMethod()
{
RelativeTimer::notify_timeout_ms(5000, this, "OnTimeout");
}
but also for busy-waiting:
RelativeTimer::wait_ms(2000);
Enjoy.

Related

Property values are lost (after each loop) when nesting libraries

I created 2 libraries to use in my Arduino code. One is a HwSwitch library, the other is a HwServo library which uses the HwSwitch library.
HwSwitch Library:
HwSwitch::HwSwitch(String switchName, int switchPort, int inputType, int pressedState)
{
Name = switchName;
SwitchPort = switchPort;
_pressedState = pressedState;
_lastCheckMillis = 0;
pinMode(switchPort, inputType);
_lastPinState = digitalRead(SwitchPort);
}
bool HwSwitch::IsPressed()
{
int currentPinState = GetPinState();
return currentPinState == _pressedState;
}
bool HwSwitch::SwitchStateChanged()
{
int currentPinState = GetPinState();
if (_lastPinState != currentPinState)
{
Serial.println("---");
Serial.println("1. Now: " + String(currentPinState) + " - Prev: " + String(_lastPinState));
_lastPinState = currentPinState;
Serial.println("2. Now: " + String(currentPinState) + " - Prev: " + String(_lastPinState));
return true;
}
return false;
}
int HwSwitch::GetPinState()
{
unsigned long ms = millis();
if ((ms - _lastCheckMillis) < 50)
{
return _lastPinState;
}
_lastCheckMillis = ms;
return digitalRead(SwitchPort);
}
HwServo Library:
HwServo::HwServo(int servoPort, int zeroPoint, HwSwitch limitSwitch)
{
_servo.attach(servoPort);
_servo.write(zeroPoint);
ServoPort = servoPort;
ZeroPoint = zeroPoint;
LimitSwitch = limitSwitch;
}
void HwServo::RotateUp()
{
_servo.write(ZeroPoint + UP);
}
void HwServo::RotateDown()
{
if (!LimitSwitch.IsPressed())
{
_servo.write(ZeroPoint + DOWN);
}
}
void HwServo::Stop()
{
_servo.write(ZeroPoint);
}
And this is how I initialized it in the Arduino code:
HwServo HwServos[] = {
HwServo(9, 94, HwSwitch("S1", 14, INPUT_PULLUP, HIGH)),
HwServo(5, 90, HwSwitch("S2", 8, INPUT_PULLUP, HIGH)),
};
void setup() { }
void loop() {
for(int i = 0; i < 2; i++)
{
HwServo hwServo = HwServos[i];
if (hwServo.LimitSwitch.SwitchStateChanged())
{
SendSwitchStateUpdate(hwServo.LimitSwitch);
if (hwServo.LimitSwitch.IsPressed())
{
hwServo.Stop();
}
}
}
}
Now finally to the problem! As you can see in the HwSwitch library I output some data using Serial.println. Here I can see that _lastPinState is successfully updated, but gets reset after every loop. However, when I create a HwSwitch directly and use it, _lastPinState is not reset. In other words, the resetting of the value only seems to occur when the HwSwitch library is used inside the HwServo library.
Appearently this has something to do with the pointers? I am probably initializing my classes incorrectly, but I have no idea how to fix it. Anyone that can help with (and preferably explain) this issue?
I don't have my Arduino on me right now, but I took look and re-wrote your code, added the omitted constructors at my best guess, and got it to compile. There were some things which needed corrected. I'm sure there are other ways, but this is what I did.
For complete code, go here.
First, I created some pointers to objects I'd like to stick around, like so:
HwServo *HwServos[2];
HwSwitch *s1;
HwSwitch *s2;
HwServo *sv1;
HwServo *sv2;
Now each is reserved in memory on the Arduino.
Now, construct the objects in setup():
void setup() {
s1 = new HwSwitch("S1", 14, INPUT_PULLUP, HIGH);
s2 = new HwSwitch("S2", 8, INPUT_PULLUP, HIGH);
sv1 = new HwServo(9, 94, *s1);
sv2 = new HwServo(5, 90, *s2);
//Now, since you're going through an array:
HwServos[0] = sv1;
HwServos[1] = sv2;
}
Use that setup function!!! Maybe not always necessary, or in some cases even recommended, but it's nice to collect things which only need created once there, especially is this case.
Note that new was not used inside the scope of either object, but rather in the scope of the program... So no fancy destructors in your objects are required. Normally, you'd worry about deleting them all before program termination (or whenever best suited), but in Arduino's case, it'll just lose power and kill everything anyway.
You should change your class definitions to this:
class HwSwitch {
public:
String Name;
int SwitchPort;
int _pressedState;
int _lastCheckMillis;
int _lastPinState;
HwSwitch(String, int, int, int);
bool IsPressed();
bool SwitchStateChanged();
int GetPinState();
};
class HwServo {
public:
HwServo();
HwServo(int, int, HwSwitch &);
int ServoPort;
int ZeroPoint;
HwSwitch & LimitSwitch;
void RotateUp();
void RotateDown();
void Stop();
Servo _servo;
};
Note: I made everything public, feel free to move private stuff back to private if you wish.
I changed the constructors to:
HwSwitch::HwSwitch(String switchName, int switchPort, int inputType, int pressedState)
{
Name = switchName;
SwitchPort = switchPort;
_pressedState = pressedState;
_lastCheckMillis = 0;
pinMode(switchPort, inputType);
_lastPinState = digitalRead(SwitchPort);
}
HwServo::HwServo(int servoPort, int zeroPoint, HwSwitch &limitSwitch)
{
_servo.attach(servoPort);
_servo.write(zeroPoint);
ServoPort = servoPort;
ZeroPoint = zeroPoint;
LimitSwitch = limitSwitch;
}
And I modified loop() like so:
void loop() {
// put your main code here, to run repeatedly:
for(int i = 0; i < 2; i++)
{
if (HwServos[i]->LimitSwitch.SwitchStateChanged())
{
SendSwitchStateUpdate(HwServos[i]->LimitSwitch);
if (HwServos[i]->LimitSwitch.IsPressed())
{
HwServos[i]->Stop();
}
}
}
}

How to stop QElapsedTimer?

QElapsedTimer timer;
timer.start();
slowOperation1();
qDebug() << "The slow operation took" << timer.elapsed() << "milliseconds";
http://doc.qt.io/qt-5/qelapsedtimer.html#invalidate
After qDebug() I would want to stop this timer. I can't see a stop function there, nor a single shot property.
What's the way out?
You can't stop QElapsedTimer, because there is no timer. When you call method start(), QElapsedTimer saves the current time.
From qelapsedtimer_generic.cpp
void QElapsedTimer::start() Q_DECL_NOTHROW
{
restart();
}
qint64 QElapsedTimer::restart() Q_DECL_NOTHROW
{
qint64 old = t1;
t1 = QDateTime::currentMSecsSinceEpoch();
t2 = 0;
return t1 - old;
}
When elapsed, it gets current time again, and calculate difference.
qint64 QElapsedTimer::elapsed() const Q_DECL_NOTHROW
{
return QDateTime::currentMSecsSinceEpoch() - t1;
}
P.S. Specific realization is platform dependent: Windows, Unix, Mac
QElapsedTimer will use the platform's monotonic reference clock in all platforms that support it. This has the added benefit that QElapsedTimer is immune to time adjustments, such as the user correcting the time. Also unlike QTime, QElapsedTimer is immune to changes in the timezone settings, such as daylight-saving periods.
https://doc.qt.io/qt-5/qelapsedtimer.html#details
I needed an elapsed timer that wouldn't count the paused time, so here's what I came up with:
ElapsedTimer.hpp:
#pragma once
#include <time.h>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <errno.h>
namespace your_namespace {
class ElapsedTimer {
public:
ElapsedTimer();
~ElapsedTimer();
void Continue();
void Pause();
int64_t elapsed_ms();
private:
struct timespec start_ = {};
int64_t worked_time_ = 0;
/// CLOCK_MONOTONIC_COARSE is faster but less precise
/// CLOCK_MONOTONIC_RAW is slower but more precise
const clockid_t clock_type_ = CLOCK_MONOTONIC_RAW;
};
}
ElapsedTimer.cpp:
#include "ElapsedTimer.hpp"
namespace your_namespace {
ElapsedTimer::ElapsedTimer() {}
ElapsedTimer::~ElapsedTimer() {}
inline int64_t GetDiffMs(const timespec &end, const timespec &start) {
return (end.tv_sec - start.tv_sec) * 1000L + (end.tv_nsec - start.tv_nsec) / 1000000L;
}
void ElapsedTimer::Continue()
{
int status = clock_gettime(clock_type_, &start_);
if (status != 0)
printf("%s", strerror(errno));
}
int64_t ElapsedTimer::elapsed_ms()
{
const bool paused = (start_.tv_sec == 0 && start_.tv_nsec == 0);
if (paused)
return worked_time_;
struct timespec now;
int status = clock_gettime(clock_type_, &now);
if (status != 0)
printf("%s", strerror(errno));
const int64_t extra = GetDiffMs(now, start_);
return worked_time_ + extra;
}
void ElapsedTimer::Pause() {
struct timespec now;
int status = clock_gettime(clock_type_, &now);
if (status != 0)
printf("%s", strerror(errno));
worked_time_ += GetDiffMs(now, start_);
start_ = {};
}
}
To be used as:
my_namespace::ElapsedTimer timer;
timer.Continue(); // starts recording the amount of time
timer.Pause();// stops recording time
///do something else
timer.Continue();// resumes recording time
/// and at any time call this to find out how many
/// ms passed excluding the paused time:
int64_t passed_ms = timer.elapsed_ms();

How to test a QStateMachine?

I'm a bit confused about how to test a QStateMachine.
I have a project well organized with source code in one side and test code on the other side.
header
class Foo
{
signals:
void sigGoToStateOne();
void sigGoToStateTwo();
void sigGoToStateThree();
private:
QStateMachine *stateMachine;
QState *state1;
QState *state2;
void initStateMachine();
}
And in the source file
Foo::initStateMachine()
{
// constructors
state1->addTransition(this,SIGNAL(sigGoToStateTwo()),this->state2);
state2->addTransition(this,SIGNAL(sigGoToStateOne()),this->state1);
}
I would like to know if there is a beautiful way to test if my stateMachine is right. In other words, how my state machine reacts if I emit sigGoToStateThree() if I'm there, etc..
Solutions i see:
1 - Get the address of stateMachine (and eventually all other states) and test it (But i don't know how)
2 - Simulate signals (sigGoToStateX()) from a test file (Again, don't know if it's possible to emit signals of my class Foo in an other class)
My unique demand is I don't want to modify the core of my source file.
Thank's in advance.
In Qt 5, signals are always public methods. To make your code compatible with Qt 4, you can make the signals explicitly public like so:
class Foo {
public:
Q_SIGNAL void sigGoToStateOne();
...
}
Alternatively, you can keep arbitrary signal visibility, and declare a friend test class:
class Foo {
friend class FooTest;
...
}
Finally, you can create a test project where you use the Qt's test framework to test the Foo class's behavior. The code below works in both Qt 4 and Qt 5.
// main.cpp
#include <QCoreApplication>
#include <QStateMachine>
#include <QEventLoop>
#include <QtTest>
#include <QTimer>
class Waiter {
QTimer m_timer;
public:
Waiter() {}
Waiter(QObject * obj, const char * signal) {
m_timer.connect(obj, signal, SIGNAL(timeout()));
}
void stop() {
m_timer.stop();
QMetaObject::invokeMethod(&m_timer, "timeout");
}
void wait(int timeout = 5000) {
QEventLoop loop;
m_timer.start(timeout);
loop.connect(&m_timer, SIGNAL(timeout()), SLOT(quit()));
loop.exec();
}
};
class SignalWaiter : public QObject, public Waiter {
Q_OBJECT
int m_count;
Q_SLOT void triggered() {
++ m_count;
stop();
}
public:
SignalWaiter(QObject * obj, const char * signal) : m_count(0) {
connect(obj, signal, SLOT(triggered()), Qt::QueuedConnection);
}
int count() const { return m_count; }
};
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
typedef QSignalSpy SignalSpy;
#else
class SignalSpy : public QSignalSpy, public Waiter {
public:
SignalSpy(QObject * obj, const char * signal) :
QSignalSpy(obj, signal), Waiter(obj, signal) {}
};
#endif
class Foo : public QObject {
Q_OBJECT
friend class FooTest;
QStateMachine m_stateMachine;
QState m_state1;
QState m_state2;
Q_SIGNAL void sigGoToStateOne();
Q_SIGNAL void sigGoToStateTwo();
public:
explicit Foo(QObject * parent = 0) :
QObject(parent),
m_state1(&m_stateMachine),
m_state2(&m_stateMachine)
{
m_stateMachine.setInitialState(&m_state1);
m_state1.addTransition(this, SIGNAL(sigGoToStateTwo()), &m_state2);
m_state2.addTransition(this, SIGNAL(sigGoToStateOne()), &m_state1);
}
Q_SLOT void start() {
m_stateMachine.start();
}
};
class FooTest : public QObject {
Q_OBJECT
void call(QObject * obj, const char * method) {
QMetaObject::invokeMethod(obj, method, Qt::QueuedConnection);
}
Q_SLOT void test1() {
// Uses QSignalSpy
Foo foo;
SignalSpy state1(&foo.m_state1, SIGNAL(entered()));
SignalSpy state2(&foo.m_state2, SIGNAL(entered()));
call(&foo, "start");
state1.wait();
QCOMPARE(state1.count(), 1);
call(&foo, "sigGoToStateTwo");
state2.wait();
QCOMPARE(state2.count(), 1);
call(&foo, "sigGoToStateOne");
state1.wait();
QCOMPARE(state1.count(), 2);
}
Q_SLOT void test2() {
// Uses SignalWaiter
Foo foo;
SignalWaiter state1(&foo.m_state1, SIGNAL(entered()));
SignalWaiter state2(&foo.m_state2, SIGNAL(entered()));
foo.start();
state1.wait();
QCOMPARE(state1.count(), 1);
emit foo.sigGoToStateTwo();
state2.wait();
QCOMPARE(state2.count(), 1);
emit foo.sigGoToStateOne();
state1.wait();
QCOMPARE(state1.count(), 2);
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
FooTest test;
QTest::qExec(&test, a.arguments());
QMetaObject::invokeMethod(&a, "quit", Qt::QueuedConnection);
return a.exec();
}
#include "main.moc"
I am forcing all signal invocations to be done from the event loop, so that the event transitions will only happen while the event loop is running. This makes the test code uniformly wait after each transition. Otherwise, the second wait would time out:
Q_SLOT void test1() {
SignalSpy state1(&m_foo.m_state1, SIGNAL(entered()));
SignalSpy state2(&m_foo.m_state2, SIGNAL(entered()));
m_foo.start();
state1.wait();
QCOMPARE(state1.count(), 1);
emit m_foo.sigGoToStateTwo(); // The state2.entered() signal is emitted here.
state2.wait(); // But we wait for it here, and this wait will time out.
QCOMPARE(state2.count(), 1); // But of course the count will match.
emit m_foo.sigGoToStateOne();
state1.wait(); // This would timeout as well.
QCOMPARE(state1.count(), 2);
}
This can be worked around without the use of explicit queued calls by the use of a signal spy class that internally uses a queued connection.
Kuba Ober gives a very good analysis of how to use the test framework & SignalSpy to do in depth testing of your state machine.
If all you're trying to do is generate a sigGoToStateX() from a test file then don't forget that you can chain signals together.
So for example given a class "Tester":
class Tester : public QObject {
Q_OBJECT
public:
Tester(Foo *fooClass) {
//Connecting signals gives you the kind of behaviour you were asking about
connect(this, SIGNAL(testTransitionToState1()), fooClass, SIGNAL(sigGoToState1()));
connect(this, SIGNAL(testTransitionToState2()), fooClass, SIGNAL(sigGoToState2()));
connect(this, SIGNAL(testTransitionToState3()), fooClass, SIGNAL(sigGoToState3()));
}
void SwitchState(int newState) {
//Now any time we emit the test signals, the foo class's signals will be emitted too!
if (newState == 1) emit testTransitionToState1();
else if (newState == 2) emit testTransitionToState1();
else if (newState == 3) emit testTransitionToState1();
}
signals:
void testTransitionToState1();
void testTransitionToState2();
void testTransitionToState3();
}
So for example calling SwitchState(1) will invoke the correct signals for switching to state 1. If this simple case is all you need for testing then that's all you really need.
If you need something more complex, go with the full SignalSpy example.

How to get list of application icons which are installed in device with Qt

I am working with Qt application in which I want to create a QListWidget with names of all application installed on device and its icon.
So I was unable get all application names and UID of each application from the code in this LINK.
But I was also unable to get the application icons. I tried both this LINK1 & LINK2 but here I came across few more issues like how to convert CGulIcon into QIcon & CApaMaskedBitmap into QIcon.
How can I do this?
If you are unable to get the solution or have any more doubts click here for disscussion.
main.cpp
#include "GetInstalledApps.h"
#include <QtGui>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
GetInstalledApps w;
w.showMaximized();
return a.exec();
}
.pro
TEMPLATE = app
TARGET = GetInstalledApps
QT += core \
gui
HEADERS += GetInstalledApps.h
SOURCES += main.cpp \
GetInstalledApps.cpp
FORMS += GetInstalledApps.ui
RESOURCES +=
HEADERS += xqinstaller_p.h \
xqinstaller.h \
SOURCES += xqinstaller_p.cpp \
xqinstaller.cpp
symbian:LIBS += -lswinstcli \
-lcommdb \
-lapparc \
-lefsrv \
-lapgrfx \
symbian:TARGET.CAPABILITY += TrustedUI
symbian:TARGET.UID3 = 0xEF3055F4
GetInstalledApps.h
#ifndef GETINSTALLEDAPPS_H
#define GETINSTALLEDAPPS_H
#include <QtGui/QMainWindow>
#include "ui_GetInstalledApps.h"
#include "xqinstaller.h"
class GetInstalledApps : public QMainWindow
{
Q_OBJECT
public:
GetInstalledApps(QWidget *parent = 0);
~GetInstalledApps();
private:
void GetApps();
private:
Ui::GetInstalledAppsClass ui;
XQInstaller* m_installer;
QMap<QString, uint> m_applications;
QList<QString> m_appNames;
QList<uint> m_appUID;
};
#endif // GETINSTALLEDAPPS_H
GetInstalledApps.cpp
#include "GetInstalledApps.h"
#include <QScrollArea>
GetInstalledApps::GetInstalledApps(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
setWindowTitle("GetAllInstalledApps");
m_installer = new XQInstaller(this);
GetApps();
}
void GetInstalledApps::GetApps()
{
/* Get List of applications.
* type of m_applications is QMap<QString, uint>.
* type of m_installer is XQInstaller
*/
m_applications = m_installer->applications();
/* Get List of application names
type of m_appNames is QList<QString> */
m_appNames = m_applications.keys();
/* Get List of application UID in decimal.
type of m_appUID is QList<uint> */
m_appUID = m_applications.values();
ui.listWidget->clear();
for (int i = 0; i < m_appNames.count(); i++)
{
QString str;
/* convert UID from decimal to hex and then string. */
str.setNum(m_appUID.at(i),16);
/* append name and UID to string */
QString string(m_appNames.at(i)+" UID:"+ str);
/* append string to list widget to display on screen */
ui.listWidget->addItem(string);
}
/* Let's make the UI scale so that we can scroll it. */
QScrollArea* scrollArea = new QScrollArea;
scrollArea->setWidget(ui.listWidget);
scrollArea->setWidgetResizable(true);
setCentralWidget(scrollArea);
}
GetInstalledApps::~GetInstalledApps()
{
}
xqinstaller.h
#ifndef XQINSTALLER_H
#define XQINSTALLER_H
// INCLUDES
#include <QObject>
#include <QMap>
class XQInstallerPrivate;
// CLASS DECLARATION
class XQInstaller : public QObject
{
Q_OBJECT
public:
enum Error {
NoError = 0,
OutOfMemoryError,
AlreadyInUseError,
UserCancelError,
PackageNotSupportedError,
SecurityFailureError,
MissingDependencyError,
NoRightsError,
BusyError,
AccessDeniedError,
UpgradeError,
UnknownError = -1
};
enum Drive {
DriveA, DriveB, DriveC, DriveD, DriveE,
DriveF, DriveG, DriveH, DriveI, DriveJ,
DriveK, DriveL, DriveM, DriveN, DriveO,
DriveP, DriveQ, DriveR, DriveS, DriveT,
DriveU, DriveV, DriveW, DriveX, DriveY,
DriveZ
};
XQInstaller(QObject *parent = 0);
~XQInstaller();
bool install(const QString& file, XQInstaller::Drive drive = XQInstaller::DriveC);
QMap<QString, uint> applications() const;
bool remove(uint uid);
XQInstaller::Error error() const;
Q_SIGNALS:
void applicationInstalled();
void applicationRemoved();
void error(XQInstaller::Error);
private:
friend class XQInstallerPrivate;
XQInstallerPrivate *d;
};
#endif // XQINSTALLER_H
xqinstaller.cpp
#include "xqinstaller.h"
#include "xqinstaller_p.h"
/*!
\class XQInstaller
\brief The XQInstaller class is used to install sis-packages silently.
This extension can be used for example to install back-end applications.
Example:
\code
XQInstaller *installer = new XQInstaller(this);
QMap<QString, uint> applications = installer->applications();
QListWidget *applicationList = new QListWidget(this);
QList<QString> appNames = applications.keys();
for (int i = 0; i < appNames.count(); i++) {
applicationList->addItem(appNames.at(i));
}
\endcode
*/
/*!
Constructs a XQInstaller object with the given parent.
\sa install(), remove()
*/
XQInstaller::XQInstaller(QObject *parent)
: QObject(parent), d(new XQInstallerPrivate(this))
{
}
/*!
Destroys the XQInstaller object.
*/
XQInstaller::~XQInstaller()
{
delete d;
}
/*!
Installs a sis package silently given as parameter.
\param file Sis package
\param drive Drive letter where the sis is installed to. Default value is 'C'.
\return If false is returned, an error has occurred. Call error() to get a value of
XQInstaller::Error that indicates which error occurred
\sa error()
*/
bool XQInstaller::install(const QString& file, XQInstaller::Drive drive)
{
return d->install(file, drive);
}
/*!
Get list of installed applications
If an empty QMap is returned, an error has possibly occurred. Call error() to get a value of
XQInstaller::Error that indicates which error occurred if any
\return List of installed applications
\sa error()
*/
QMap<QString, uint> XQInstaller::applications() const
{
return d->applications();
}
/*!
Removes application specified by the uid
\param uid of the application
\return True if removing was successfully started, otherwise false
\sa error()
*/
bool XQInstaller::remove(uint uid)
{
return d->remove(uid);
}
/*!
\enum XQInstaller::Error
This enum defines the possible errors for a XQInstaller object.
*/
/*! \var XQInstaller::Error XQInstaller::NoError
No error occured.
*/
/*! \var XQInstaller::Error XQInstaller::OutOfMemoryError
Not enough memory.
*/
/*! \var XQInstaller::Error XQInstaller::AlreadyInUseError
Installer is already in used.
*/
/*! \var XQInstaller::Error XQInstaller::UserCancelError
Installer cancelled by the user.
*/
/*! \var XQInstaller::Error XQInstaller::PackageNotSupportedError
Package not supported
*/
/*! \var XQInstaller::Error XQInstaller::SecurityFailureError
Security failure
*/
/*! \var XQInstaller::Error XQInstaller::MissingDependencyError
Missing dependency
*/
/*! \var XQInstaller::Error XQInstaller::NoRightsError
No rights
*/
/*! \var XQInstaller::Error XQInstaller::BusyError
Installer is busy
*/
/*! \var XQInstaller::Error XQInstaller::AccessDeniedError
Access denied
*/
/*! \var XQInstaller::Error XQInstaller::UpgradeError
Error while upgrading
*/
/*! \var XQInstaller::Error XQInstaller::UnknownError
Unknown error.
*/
/*!
Returns the type of error that occurred if the latest function call failed; otherwise returns NoError
\return Error code
*/
XQInstaller::Error XQInstaller::error() const
{
return d->error();
}
/*!
\fn void XQInstaller::applicationInstalled()
This signal is emitted when the application has been installed.
\sa install()
*/
/*!
\fn void XQInstaller::error(XQInstaller::Error)
This signal is emitted if error occured during the asynchronous operation
\sa install()
*/
/*!
\fn void XQInstaller::applicationRemoved()
This signal is emitted when the application has been removed.
\sa remove()
*/
// End of file
xqinstaller_h.h
#ifndef XQINSTALLER_P_H
#define XQINSTALLER_P_H
// INCLUDES
#include "xqinstaller.h"
#include <SWInstApi.h>
#include <SWInstDefs.h>
// FORWARD DECLARATIONS
class QString;
class QFile;
// CLASS DECLARATION
class XQInstallerPrivate: public CActive
{
public:
enum State {
ERemove,
EInstall
};
XQInstallerPrivate(XQInstaller *installer);
~XQInstallerPrivate();
bool install(const QString& file, XQInstaller::Drive drive);
bool remove(uint uid);
QMap<QString, uint> applications() const;
public:
XQInstaller::Error error();
protected:
void DoCancel();
void RunL();
private:
XQInstaller *q;
mutable int iError;
SwiUI::RSWInstSilentLauncher iLauncher;
SwiUI::TInstallOptions iOptions;
SwiUI::TInstallOptionsPckg iOptionsPckg;
SwiUI::TUninstallOptions iUninstallOptions;
SwiUI::TUninstallOptionsPckg iUninstallOptionsPckg;
XQInstallerPrivate::State iState;
TFileName iFileName;
bool iLauncherConnected;
};
#endif /*XQINSTALLER_P_H*/
// End of file
xqinstaller_h.cpp
#include "xqinstaller.h"
#include "xqinstaller_p.h"
#include <f32file.h>
#include <apgcli.h>
XQInstallerPrivate::XQInstallerPrivate(XQInstaller *installer)
: CActive(EPriorityNormal), q(installer), iOptionsPckg(iOptions),
iUninstallOptionsPckg(iUninstallOptions), iLauncherConnected(false)
{
CActiveScheduler::Add(this);
}
XQInstallerPrivate::~XQInstallerPrivate()
{
Cancel();
if (iLauncherConnected) {
iLauncher.Close();
}
}
bool XQInstallerPrivate::install(const QString& file, XQInstaller::Drive drive)
{
int asciiValue = 10; // = 'A'
TRAP(iError,
if (!iLauncherConnected) {
User::LeaveIfError(iLauncher.Connect());
iLauncherConnected = true;
}
if (IsActive()) {
User::Leave(KErrInUse);
}
iState = XQInstallerPrivate::EInstall;
iOptions.iUpgrade = SwiUI::EPolicyAllowed;
iOptions.iOCSP = SwiUI::EPolicyNotAllowed;
iOptions.iDrive = TChar(asciiValue+drive);
iOptions.iUntrusted = SwiUI::EPolicyAllowed;
iOptions.iCapabilities = SwiUI::EPolicyAllowed;
iOptions.iOptionalItems = SwiUI::EPolicyAllowed;
iOptions.iOverwrite = SwiUI::EPolicyAllowed;
TPtrC16 fileName(reinterpret_cast<const TUint16*>(file.utf16()));
iFileName = fileName;
iLauncher.SilentInstall(iStatus, iFileName, iOptionsPckg);
SetActive();
)
return (iError == KErrNone);
}
bool XQInstallerPrivate::remove(uint uid)
{
TRAP(iError,
if (!iLauncherConnected) {
User::LeaveIfError(iLauncher.Connect());
iLauncherConnected = true;
}
if (IsActive()) {
User::Leave(KErrInUse);
}
iState = XQInstallerPrivate::ERemove;
iLauncher.SilentUninstall(iStatus,TUid::Uid(uid),
iUninstallOptionsPckg, SwiUI::KSisxMimeType);
SetActive();
)
return (iError == KErrNone);
}
QMap<QString, uint> XQInstallerPrivate::applications() const
{
RApaLsSession lsSession;
QMap<QString, uint> applications;
// Connect to application architecture server
TRAP(iError,
User::LeaveIfError(lsSession.Connect());
CleanupClosePushL(lsSession);
TApaAppInfo appInfo;
lsSession.GetAllApps();
while (lsSession.GetNextApp(appInfo) == KErrNone) {
TApaAppCapabilityBuf capability;
User::LeaveIfError(lsSession.GetAppCapability(capability,
appInfo.iUid));
if (appInfo.iCaption.Length() > 0 && !capability().iAppIsHidden) {
QString fullName = QString::fromUtf16(
appInfo.iCaption.Ptr(), appInfo.iCaption.Length());
applications.insert(fullName, (TUint)appInfo.iUid.iUid);
}
}
CleanupStack::PopAndDestroy(&lsSession);
)
return applications;
}
void XQInstallerPrivate::DoCancel()
{
if (iState == XQInstallerPrivate::EInstall) {
iLauncher.CancelAsyncRequest(SwiUI::ERequestSilentInstall);
} else if (iState == XQInstallerPrivate::ERemove) {
iLauncher.CancelAsyncRequest(SwiUI::ERequestSilentUninstall);
}
}
void XQInstallerPrivate::RunL()
{
if (iStatus.Int() == KErrNone) {
if (iState == XQInstallerPrivate::EInstall) {
emit q->applicationInstalled();
} else if (iState == XQInstallerPrivate::ERemove) {
emit q->applicationRemoved();
}
} else {
iError = iStatus.Int();
emit q->error(error());
}
}
XQInstaller::Error XQInstallerPrivate::error()
{
switch (iError) {
case KErrNone:
return XQInstaller::NoError;
case SwiUI::KSWInstErrInsufficientMemory:
case KErrNoMemory:
return XQInstaller::OutOfMemoryError;
case SwiUI::KSWInstErrFileInUse:
case KErrInUse:
return XQInstaller::AlreadyInUseError;
case SwiUI::KSWInstErrUserCancel:
return XQInstaller::UserCancelError;
case SwiUI::KSWInstErrPackageNotSupported:
return XQInstaller::PackageNotSupportedError;
case SwiUI::KSWInstErrSecurityFailure:
return XQInstaller::SecurityFailureError;
case SwiUI::KSWInstErrMissingDependency:
return XQInstaller::MissingDependencyError;
case SwiUI::KSWInstErrNoRights:
return XQInstaller::NoRightsError;
case SwiUI::KSWInstErrBusy:
return XQInstaller::BusyError;
case SwiUI::KSWInstErrAccessDenied:
return XQInstaller::AccessDeniedError;
case SwiUI::KSWInstUpgradeError:
return XQInstaller::UpgradeError;
case SwiUI::KSWInstErrGeneralError:
default:
return XQInstaller::UnknownError;
}
}
// End of file
And for getting QIcon::
Hearders Required:
#include <fbs.h> //CFbsBitmap
#include <aknsskininstance.h> //MAknsSkinInstance
#include <aknsutils.h> //AknsUtils
Library required:
LIBRARY fbscli.lib ///CFbsBitmap
LIBRARY aknskins.lib aknskinsrv.lib aknswallpaperutils.lib //MAknsSkinInstance ,AknsUtils
Source Code:
CGulIcon* CMyClass::GetApplicationIconL(const TUid& aAppUID)
{
CFbsBitmap* AppIcon(NULL);
CFbsBitmap* AppIconMsk(NULL);
MAknsSkinInstance* skin = AknsUtils::SkinInstance();
AknsUtils::CreateAppIconLC(skin,aAppUID, EAknsAppIconTypeContext,AppIcon,AppIconMsk);
CleanupStack::Pop(2);
return CGulIcon::NewL(AppIcon,AppIconMsk);
}
Well, for the icon thing look at this: CGullIcon there's a function that returns the Bitmap, a CFbsBitmap. Once you've done that look at this: http://qt-project.org/doc/qt-4.8/qpixmap.html#fromSymbianCFbsBitmap then you create a new QIcon(QPixmap) (QPixmap = the icon you've transformed). So most likely you're first passing it to a CFbsBitmap and then with QPixmap you use fromSymibnaCFbsBitmap().
CApaMaskedBitmap is a sub-class of CFbsBitmap if i'm not wrong, so it should work the same.
And for getting the applications and their UID try to look at this: http://www.developer.nokia.com/Community/Wiki/Get_list_of_installed_applications_and_its_UID_in_Qt

Qt state machine framework connect signals

In the Qt State Machine Framework documentation there is an example how to set properties on state activation:
s1->assignProperty(label, "text", "In state s1");
s2->assignProperty(label, "text", "In state s2");
s3->assignProperty(label, "text", "In state s3");
Is there a way to connect slots on state activation? like s1_buttonclick will only be connected when s1 is active and s2_buttonclick will only be connected when s2 is active?
You want the connections to be different based on which state the state machine is currently in?
I think you will have to manage this yourself using other slots and the entered() and exited() signals. Just create a slot for the each entrance and exit of the states.
QObject::connect(s1, SIGNAL(entered()), connectionManager, SLOT(connectS1()));
QObject::connect(s1, SIGNAL(exited()), connectionManager, SLOT(disconnectS1()));
//continue for each state
Filtering signal-slot connections can be done using a helper class that represents a connection and provides an active property of the connection. Note that Qt 5's QMetaObject::Connection is not sufficient for this.
#include <QMetaMethod>
#include <QPointer>
class Connection : public QObject
{
Q_OBJECT
Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged USER true)
Q_PROPERTY(bool valid READ isValid)
QMetaMethod m_signal, m_slot;
QPointer<QObject> m_source, m_target;
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
QMetaObject::Connection m_connection;
#else
bool m_connection;
#endif
bool m_active;
void release() {
if (!m_source || !m_target) return;
#if QT_VERSION >= QT_VERSION_CHECK(5,0,0)
disconnect(m_connection);
#else
disconnect(m_source, m_signal, m_target, m_slot);
#endif
}
public:
Connection(QObject * source, const char * signal, QObject * target, const char * slot, QObject * parent = 0) :
QObject(parent),
m_signal(source->metaObject()->method(source->metaObject()->indexOfSignal(signal))),
m_slot(target->metaObject()->method(target->metaObject()->indexOfSlot(slot))),
m_source(source), m_target(target),
m_connection(connect(m_source, m_signal, m_target, m_slot)),
m_active(m_connection)
{}
~Connection() { release(); }
QObject* source() const { return m_source; }
QObject* target() const { return m_target; }
QMetaMethod signal() const { return m_signal; }
QMetaMethod slot() const { return m_slot; }
bool isActive() const { return m_active && m_source && m_target; }
bool isValid() const { return m_connection && m_source && m_target; }
Q_SIGNAL void activeChanged(bool);
Q_SLOT void setActive(bool active) {
if (active == m_active || !m_source || !m_target) return;
m_active = active;
if (m_active) {
m_connection = connect(m_source, m_signal, m_target, m_slot);
} else {
release();
}
emit activeChanged(m_active);
}
};

Resources