07-11-2012 03:10 AM - edited 07-11-2012 05:01 AM
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.
Solved! Go to Solution.
07-11-2012 09:27 AM
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
07-12-2012 01:21 AM
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,
07-12-2012 02:24 AM
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 ![]()
07-12-2012 02:27 AM
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.
07-16-2012 02:03 AM - edited 07-16-2012 02:04 AM
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.
01-24-2013 11:25 AM - edited 01-24-2013 11:36 AM
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/tre