Pau Garcia i Quiles wrote:

Have you tried to use the wtwithqt library in examples/wtwithqt ?

I am using wtwithqt, as I mentioned in the last paragraph of my original mail.

As it happens, since posting this, I have made some progress. I realised that I had made my WApplication subclass also derive from QObject and it was a slot of this object that I was connecting the signal to. However, this was of course incorrect because the application is not created in the QThread that WQApplication creates so, as I noted, there was no event dispatcher for that object's thread.

I have modified my example code (attached) to connect the signal to an object created in the correct QThread and the signal/slot connection now works correctly. Unfortuanely, the application crashes when I try to update the text in the label because calling WApplication::instance()->session() is return an invalid pointer. I am still digging into that.

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;
}

/*------------------------------------------------------------------------------
  CounterWidget
------------------------------------------------------------------------------*/

class CounterWidget : public QObject, public Wt::WContainerWidget
{
    Q_OBJECT

public:
            CounterWidget(Wt::WContainerWidget * parent = NULL);

protected slots:

    void    increment();

protected:

    Wt::WText * label;
    uint count;
};

//------------------------------------------------------------------------------

CounterWidget::CounterWidget(Wt::WContainerWidget * parent)
 : Wt::WContainerWidget(parent), count(0)
{
    Wt::WVBoxLayout * mainLayout = new Wt::WVBoxLayout();
    setLayout(mainLayout, Wt::AlignTop | Wt::AlignJustify);

    label = new Wt::WText(this);
    label->setText("0");
    mainLayout->addWidget(label);

    connect(QtObject::get(), SIGNAL(didSomething()), SLOT(increment()));
}

//------------------------------------------------------------------------------

void CounterWidget::increment()
{
    ++count;
    label->setText(Wt::toWString(QString::number(count)));
}

/*------------------------------------------------------------------------------
  WebApplication
------------------------------------------------------------------------------*/

class WebApplication : public Wt::WQApplication
{
public:
            WebApplication(const Wt::WEnvironment & env);

    virtual void create();
    virtual void destroy() {}
};

//------------------------------------------------------------------------------

WebApplication::WebApplication(const Wt::WEnvironment & env)
 : Wt::WQApplication(env, true)
{

}

//------------------------------------------------------------------------------

void WebApplication::create()
{
    Wt::WVBoxLayout * mainLayout = new Wt::WVBoxLayout();
    root()->setLayout(mainLayout, Wt::AlignTop | Wt::AlignJustify);

    mainLayout->addWidget(new CounterWidget(root()));
}

//------------------------------------------------------------------------------

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

Reply via email to