I am working on an embedded Qt-based application, using Wt to present a
web-based interface.
I would like to be able to connect signals emitted from objects created
in the Qt application to slots of objects created in my Wt application.
For example, when something changes in the state of my application, it
emits a signal and the part of the web application that is reflecting
that state is updated accordingly.
When my Qt signal is emitted, it is posting a QMetaCallEvent to the
object in the Wt application, because it knows that they are in
different threads, but I am not seeing that event arrive at the other end.
By reimplementing QApplication::notify() and tracing all events received
in all threads, I do not see an event with type QEvent::MetaCall at all.
If I step into QApplication::postEvent, it thinks the thread of the
receiver hasn't got an event dispatcher, so I think the event is left
sitting in the post queue.
I have attached a simple example, based on the code from the wtwithqt
example, and it is not behaving as I need. It creates an object in the
main thread that emits a signal when a button is clicked. I have then
connected this to a slot in my WApplication that should increment a
counter every time the button is pressed and refresh a Wt::WLabel with
the count. However, the somethingWasDone() slot is never called.
Am I doing something obviously wrong?
Tristan
#include "WQApplication"
#include <Wt/WContainerWidget>
#include <Wt/WServer>
#include <Wt/WText>
#include <Wt/WVBoxLayout>
#include <QApplication>
#include <QPushButton>
/*------------------------------------------------------------------------------
QtObject
------------------------------------------------------------------------------*/
class QtObject : public QObject
{
Q_OBJECT
public:
static QtObject * get();
public slots:
void doSomething() { emit didSomething(); }
signals:
void didSomething();
protected:
QtObject() : QObject(NULL) {}
};
//------------------------------------------------------------------------------
QtObject * QtObject::get()
{
static QtObject * instance = NULL;
if (instance == NULL)
{
instance = new QtObject();
}
return instance;
}
/*------------------------------------------------------------------------------
WebApplication
------------------------------------------------------------------------------*/
class WebApplication : public QObject, public Wt::WQApplication
{
Q_OBJECT
public:
WebApplication(const Wt::WEnvironment & env);
virtual void create();
virtual void destroy();
protected slots:
void somethingWasDone();
protected:
Wt::WText * label;
uint count;
};
//------------------------------------------------------------------------------
WebApplication::WebApplication(const Wt::WEnvironment & env)
: Wt::WQApplication(env, true), count(0)
{
}
//------------------------------------------------------------------------------
void WebApplication::create()
{
Wt::WVBoxLayout * mainLayout = new Wt::WVBoxLayout();
root()->setLayout(mainLayout, Wt::AlignTop | Wt::AlignJustify);
label = new Wt::WText(root());
mainLayout->addWidget(label);
// connect signal from object in the main thread to ourselves in the web application thread
connect(QtObject::get(), SIGNAL(didSomething()), SLOT(somethingWasDone()));
}
//------------------------------------------------------------------------------
void WebApplication::destroy()
{
}
//------------------------------------------------------------------------------
void WebApplication::somethingWasDone()
{
++count;
label->setText(Wt::toWString(QString::number(count)));
}
//------------------------------------------------------------------------------
Wt::WApplication * createApplication(const Wt::WEnvironment & env)
{
return new WebApplication(env);
}
/*------------------------------------------------------------------------------
main()
------------------------------------------------------------------------------*/
#include "main.moc"
// fake the commandline arguments for Wt::WebServer
char * fake_argv[] =
{
"connect_test",
"--http-address=0.0.0.0",
"--http-port=80",
"--deploy-path=/",
"--docroot=."
};
//------------------------------------------------------------------------------
int main(int argc, char ** argv)
{
Wt::WServer server(fake_argv[0], "wt_config.xml");
server.setServerConfiguration(5, fake_argv, WTHTTP_CONFIGURATION);
server.addEntryPoint(Wt::Application, &createApplication);
// create a Qt object in the main thread
QtObject * object = QtObject::get();
QApplication app(argc, argv);
QPushButton button;
button.setText("Press me");
// connect button to emit signal from Qt object in the main thread
QObject::connect(&button, SIGNAL(clicked()), object, SLOT(doSomething()));
button.show();
server.start();
return app.exec();
}
------------------------------------------------------------------------------
Increase Visibility of Your 3D Game App & Earn a Chance To Win $500!
Tap into the largest installed PC base & get more eyes on your game by
optimizing for Intel(R) Graphics Technology. Get started today with the
Intel(R) Software Partner Program. Five $500 cash prizes are up for grabs.
http://p.sf.net/sfu/intelisp-dev2dev
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest