0

I create a static librairy Qt. I need by calling some method, to change the position of a picture. The problem is that the "app.exec()" block completly the process. Do you know how to do it ?

Thanks a lot !

Here is my header file of the lib

int init(int argc, char *argv[], int width, int height);
void changePos (int x, int y);
void destroyPic();

and my init is : QWidget* window(0); QLabel* pic(0);

int init(int argc, char *argv[], int width, int height) {
    // initialize resources, if needed
    Q_INIT_RESOURCE(target);

    QApplication app(argc, argv);
    window = new QWidget();
    window->setWindowTitle("Test");
    window->setFixedSize(width, height);
    // create and show your widgets here
    pic = new QLabel(window);

    QPixmap pixmap(QString(":/res/pic.png"));
    pic->setPixmap(pixmap);
    pic->move(100,100);
    pic->setVisible(true);
    window->show();
    app.exec();

    return 0;
}

the problem is i can't called in my main code (which use this lib) the method changePos or destroy because the app.exec() block the process... how to do it ?

Thanks a lot !

Nakasa
  • 17
  • 4
  • Indeed, i know that app.exec() is a blocking function and i wanna know how to display the window without use this function or another way to do this without blocking my prog. – Nakasa May 27 '12 at 09:25
  • it seems your static library is in fact a program... what are you trying to do again?? – UmNyobe May 27 '12 at 09:32
  • I have a program which not use QT. I just add the Qt lib inside. Because one part of this program need to show a window, with some pic, move this pic, and then "destroy" the window, i thought to use a lib in order to show the window, the pic, move them, and then destroy them without adding the Qt (with qmake, etc...) in my main program (because is complicated and i really don't want to include Qt in my main program) – Nakasa May 27 '12 at 09:36
  • In this case you need to launch Qt in a new process – UmNyobe May 27 '12 at 09:38
  • you mean launch init method in a thread, right ? – Nakasa May 27 '12 at 09:42
  • Your previous comment doesn't really make any sense. Are you using a library (not Qt) to display a windows & a picture? If so, why use Qt at all? – Mat May 27 '12 at 10:37

2 Answers2

1

You need to start QApplication in a separate QThread. This is shown in the code below. I haven't gotten it working on OS X yet, but I believe it should be doable. I need first to get a debug build made for OS X, then I'll see what's up. It's tested to work on Windows, and will probably work on X11 too. The Objective C code is for mac only, it's probably required but not sufficient yet.

It is an entire runnable application, for a library you'd remove the main() function and call init, changePos and finish directly as appropriate.

#applib.pro
QT       += core gui
TARGET = applib
TEMPLATE = app
!x11:!embedded:!qpa:mac {
    LIBS_PRIVATE += -framework Cocoa -lz
    OBJECTIVE_SOURCES += thread.mm
}
SOURCES += main.cpp
//main.cpp
#include <cstdlib>
#include <QtCore/QThread>
#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>
#include <QtCore/QWaitCondition>
#include <QtCore/QEvent>
#include <QtGui/QLabel>
#include <QtGui/QApplication>

//
// API
//

extern "C" {
    void init(int argc, char ** argv);
    void changePos(int x, int y);
    void finish();
}

// demonstration code, remove from the library
int main(int argc, char ** argv)
{
    class Helper : private QThread {
    public:
        static void msleep(unsigned long ms) {
            QThread::msleep(ms);
        }
    };

    init(argc, argv);
    Helper::msleep(2000);
    changePos(0, 0);
    Helper::msleep(2000);
    finish();
}

//
// IMPLEMENTATION
//

struct PosEvent : public QEvent
{
    PosEvent(int x_, int y_) : QEvent(t()),
        x(x_), y(y_) {}
    const int x, y;
    static QEvent::Type t() { return (QEvent::Type)(QEvent::User + 0); }
};

class Widget : public QLabel
{
public:
    Widget() : QLabel("Hello!") {}
protected:
    void customEvent(QEvent * ev) {
        if (ev->type() == PosEvent::t()) {
            PosEvent * pev = static_cast<PosEvent*>(ev);
            move(pev->x, pev->y);
        }
    }
};

class QCoreApplicationPrivate {
public:
    static QThread * theMainThread;
};

extern "C++" { Qt::HANDLE qt_application_thread_id; }

class AppThread : public QThread
{
public:
    AppThread(int& argc_, char ** argv_) {
        Q_ASSERT(!instance);
        instance = this;
        QMutexLocker lock(&argMutex);
        argc = &argc_;
        argv = argv_;
        start();
        wc.wait(&argMutex);
    }
    static void changePos(int x, int y) {
        QCoreApplication::postEvent(instance->widget, new PosEvent(x,y));
    }
    static void finish() {
        instance->app->exit();
        instance->wait();
        delete instance;
    }

protected:
    void run() {

        int i = 0;

#ifdef Q_OS_UNIX
        //qDebug("%p", qt_application_thread_id);
        QCoreApplicationPrivate::theMainThread = QThread::currentThread();
#endif
        QApplication a(i, 0);
        app = &a;
        wc.wakeAll();

        Widget w;
        widget = &w;
        w.show();

        rc = a.exec();
    }
private:
    static AppThread * instance;
    QCoreApplication * app;
    QObject * widget;
    QMutex argMutex;
    QWaitCondition wc;
    int* argc;
    char** argv;
    int rc;

};
AppThread * AppThread::instance = 0;

bool multiThreader();

#ifndef Q_OS_MAC
bool multiThreader() { return true; }
#endif

extern "C" void init(int argc, char ** argv) {
    multiThreader();
#ifdef Q_OS_MAC
    qDebug("DYLD_IMAGE_SUFFIX=%s", getenv("DYLD_IMAGE_SUFFIX"));
#endif
    new AppThread(argc, argv);
}

extern "C" void changePos(int x, int y) { AppThread::changePos(x, y); }

extern "C" void finish() { AppThread::finish(); }
//thread.mm
#import <Foundation/Foundation.h>

@interface DeadThread : NSObject { ;; }
    + (void)enterMultiThreadedMode;
    + (void)emptyThreadMethod:(id)obj;
@end
@implementation DeadThread
    + (void)enterMultiThreadedMode {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        [NSThread detachNewThreadSelector:@selector(emptyThreadMethod:)
            toTarget:[DeadThread class] withObject:nil];
        [pool release];
    }
+ (void)emptyThreadMethod:(id)obj { (void)obj; }
@end

bool multiThreader()
{
    [DeadThread enterMultiThreadedMode];
    return [NSThread isMultiThreaded];
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
0

You can call init() before entering the QApplication event loop using QApplication::exec().

The only other options I can think of are:

  1. Lazy init, when even the user of your library calls changePos() or destroyPic() then call init() if it hasn't been called yet.

  2. Execute after the event loop using using a single shot timer:

Something like:

QTimer::singleShot(0, this, SLOT(init()));

This would require that you have a QObject and that init or some other function that calls init be a lot. Call this before QApplication::exec() and the slot will be executed after the event loop has started.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
paulm
  • 5,629
  • 7
  • 47
  • 70