[Qt-interest] Weird qobject_cast issue with QFutureWatcher...
Gary Coulbourne
gcoulbourne at mediaguide.com
Thu Jul 23 22:12:39 CEST 2009
Howdy!
I'm having a devil of a time with qobject_cast on the sender() method in a
slot when the sender is QFutureWatcher. I've created a trivial test case that
duplicates it, and have copied it into this email. When it compiles, it ends
up with "void value not ignored as it ought to be" as the error on the line
with the qobject_cast. I've searched the mailing lists, and they suggested
that a missing Q_OBJECT macro could be the trouble, but no dice.
Anyone have any suggestions? Thanks!
Peace,
Gary
----- FutureShock.pro -------
# -------------------------------------------------
# Project created by QtCreator 2009-07-23T15:39:40
# -------------------------------------------------
QT -= gui
TARGET = FutureShock
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += main.h
------ main.h ------
#ifndef MAIN_H
#define MAIN_H
#include <QDebug>
#include <QObject>
class IntHolder {
public:
IntHolder() : t_int(0) {};
void setInt(int i) { t_int = i; }
int getInt() const { return t_int; }
private:
int t_int;
};
extern IntHolder makeHeldInt(const int i);
class FutureShock : public QObject {
Q_OBJECT
public:
FutureShock(QObject* parent=0):QObject(parent),i(0) {}
public slots:
void started();
void finished();
protected:
void timerEvent(QTimerEvent *event);
private:
int i;
};
#endif // MAIN_H
----- main.cpp ----
#include <QtConcurrentRun>
#include <QCoreApplication>
#include <QFutureWatcher>
#include "main.h"
void FutureShock::timerEvent(QTimerEvent *event) {
QFuture<IntHolder> future = QtConcurrent::run(makeHeldInt,i++);
QFutureWatcher<IntHolder>* watcher = new QFutureWatcher<IntHolder>(this);
watcher->setFuture(future);
connect(watcher,SIGNAL(started()),SLOT(started()));
connect(watcher,SIGNAL(finished()),SLOT(finished()));
}
void FutureShock::started() {
qDebug() << "Started: " << qobject_cast< QFutureWatcher<IntHolder>*
>(sender()) -> result() -> getInt();
}
void FutureShock::finished() {
qDebug() << "Ended";
sender()->deleteLater();
}
IntHolder makeHeldInt(const int i) {
IntHolder held;
held.setInt(i);
return held;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
FutureShock f;
f.startTimer(2000);
return a.exec();
}
More information about the Qt-interest-old
mailing list