Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Super Contributor
Meghasanghvi
Posts: 306
Registered: ‎08-10-2010
My Device: Not Specified
Accepted Solution

Mulitasking on Casacde (Backbround thread for accelerometer reading and in foreground update the UI)

[ Edited ]

Hi,

 

I make application to read accelerometer data. I create UI using C++ and when user click on the button application start taking users reading of accelerometer. I tried to update textarea or label text and update the x,y position of the component. How can i do that?

 

I attached my app.cpp file in which i want to update the button or label text and move image to different x,y coordinate.

 

 

 

 

Please use plain text.
Developer
rakesh86shankar
Posts: 1,003
Registered: ‎05-22-2009
My Device: Not Specified

Re: Mulitasking on Casacde (Backbround thread for accelerometer reading and in foreground update the UI)

You can have global variables  for button,absolute layout properties and you can update it when you recieve the signal right.

 

Regards

Rakesh Shankar.P

Please use plain text.
Super Contributor
Meghasanghvi
Posts: 306
Registered: ‎08-10-2010
My Device: Not Specified

Re: Mulitasking on Casacde (Backbround thread for accelerometer reading and in foreground update the UI)

Ya i want to update its value but inside the button click event UI is not updated.

I also tried with Qthread for my example but in this also i have to check how worker thread and GUI thread is working together,

Please use plain text.
New Developer
vladest
Posts: 88
Registered: ‎06-16-2012
My Device: N950/E7/N808/N9/X7/BB10 Alpha

Re: Mulitasking on Casacde (Backbround thread for accelerometer reading and in foreground update the UI)

hi

 

its absolutely incorrect to try to catch all events in an event handler

you have to redesign your application

you have to give Qt's event dispatcher chance to get a piece of CPU :smileyhappy:

Qt/Symbian/Meego/BB10/Cascades developer
Please use plain text.
Super Contributor
Meghasanghvi
Posts: 306
Registered: ‎08-10-2010
My Device: Not Specified

Re: Mulitasking on Casacde (Backbround thread for accelerometer reading and in foreground update the UI)

Hi vladest,

 

I am updating my UI using the signal-slot mechanismof GUI and worker thread. Is it not a correct way to do multitaksing in Casacde. 

Below is my code for signal-slot mechanism.

QObject::connect(myThread, SIGNAL(signalGUI(float,float,float)), this,
SLOT(showUpdatedData(float,float,float)));

 

Thanks,

Megha.

Please use plain text.
Super Contributor
Meghasanghvi
Posts: 306
Registered: ‎08-10-2010
My Device: Not Specified

Re: Mulitasking on Casacde (Backbround thread for accelerometer reading and in foreground update the UI)

[ Edited ]

I am successfully implemented multitasking on Cascade.

Below is my code in which both thread work in parallel.

 

Step 1: Extend the class with the Thread class

Step 2: Emit the signal whenever you want to update your UI from the Thread class

Step 3: 

QObject::connect(myThread, SIGNAL(signalGUI(float,float,float)), this,
SLOT(showUpdatedData(float,float,float)));

 

myThread is a object of thread class which we createad by exteding the Qthread.

signalGUI is a signal which we emit from the worker thread.

showUpdatedData is a slot in which we write the code to update the GUI.

 

This way we can implement multitasking on cascade.

 

Please use plain text.
Contributor
casantos
Posts: 33
Registered: ‎01-13-2013
My Device: BlackBerry Z10, Q10

Re: Mulitasking on Casacde (Backbround thread for accelerometer reading and in foreground update the UI)

[ Edited ]

Ressurrecting an old thread: I'm trying to load an image in a background thread and use it to update an ImageView. contained in an item of a ListView with grid layout. My code is like this:

 

== Worker.hpp ==

class Worker: public QObject {
Q_OBJECT
public:
    Worker(const QString& imagePath);

public slots:
    // Update image and text for the current item
    void doWork();

signals:
    void workDone(const Image& image);

private:
    const QString mImagePath;
};

== Worker.cpp ==

 

static bb::ImageData fromQImage(const QImage &qImage) {

    [...]
    return imageData;
}

Worker::Worker(const QString& imagePath) :
        mImagePath(imagePath) {
            ;
}

 

static QSize scaledSize(252, 252);

void Worker::doWork() {
    QImageReader reader(mImagePath);
    if (reader.canRead()) {
        QSize size = reader.size();
        int width = size.width();
        int height = size.height();
        if (width != height) {
            int aleft, atop, awidth, aheight;
            if (width < height) {
                aleft = 0;
                atop = (height - width) / 2, awidth = aheight = width;
            } else {
                aleft = (width - height) / 2;
                atop = 0, awidth = aheight = height;
            }
            QRect clipRect(aleft, atop, awidth, aheight);
            reader.setClipRect(clipRect);
        }
        reader.setScaledSize(scaledSize);
        Image img(fromQImage(reader.read()));
        emit workDone(img);
    }
}

== GalleryItem.hpp ==

 

class GalleryItem: public Container, public ListItemListener {
Q_OBJECT

public:
    GalleryItem(Container *parent = 0);
    ~GalleryItem();

public slots:
    void updateItem(const QString& text, const QString& imagePath);
    void select(bool select);
    void reset(bool selected, bool activated);
    void activate(bool activate);

private:
    ImageView *mThumbView;
    QThread *mWorkerThread;
    Label *mLabel;
    Container *mHighlight;
};

== GalleryItem.cpp ==

 

void GalleryItem::updateItem(const QString& text, const QString& imagePath) {
    Worker *worker = new Worker(imagePath);
    mWorkerThread = new QThread(this);
    worker->moveToThread(mWorkerThread);

    connect(mWorkerThread, SIGNAL(started()), worker, SLOT(doWork()));
    connect(mWorkerThread, SIGNAL(finished()), worker, SLOT(deleteLater()));
    connect(worker, SIGNAL(workDone(Image)), mThumbView, SLOT(setImage(Image)), Qt::QueuedConnection);
    connect(worker, SIGNAL(workDone(Image)), mWorkerThread, SLOT(quit()));
    // Starts an event loop, and emits workerThread->started()
    mWorkerThread->start();
    mLabel->setText(text);
}

===

 

If I leave the code as is I get a bunch of messages like this in the console:

 

ApplicationPrivate::context: ERROR called from non-UI thread QThread(0x81f30b0)
ApplicationPrivate::context: Method called from non-UI thread

Then the application crashes. If I comment out the call to moveToThread(). Ths looks strange, since it seems to be the right way to use QThread, according to the documentation (http://qt-project.org/doc/qt-4.8/qthread.html).

This is also the apprach taken in the ImageLoader example (https://github.com/blackberry/Cascades-Samples/tree/master/imageloader).

 

Please use plain text.