gpt4 book ai didi

c++ - app.exec() 无法调用其他方法(静态库 Qt)

转载 作者:行者123 更新时间:2023-11-28 08:03:33 26 4
gpt4 key购买 nike

我创建了一个静态库 Qt。我需要通过调用一些方法来改变图片的位置。问题是“app.exec()”完全阻塞了进程。你知道怎么做吗?

非常感谢!

这是我的lib的头文件

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

我的初始化是: QWidget* 窗口(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;
}

问题是我无法在我的主代码(使用此库)中调用方法 changePos 或 destroy,因为 app.exec() 阻塞了进程...该怎么做?

非常感谢!

最佳答案

您需要在单独的 QThread 中启动 QApplication。这显示在下面的代码中。我还没有让它在 OS X 上运行,但我相信它应该是可行的。我需要先获得一个为 OS X 制作的调试版本,然后我会看看发生了什么。它经过测试可在 Windows 上运行,也可能在 X11 上运行。 Objective C 代码仅适用于 mac,它可能是必需的,但还不够。

它是一个完整的可运行应用程序,对于一个库,您将删除 main() 函数并调用 init、changePos 并在适当时直接完成。

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

关于c++ - app.exec() 无法调用其他方法(静态库 Qt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10772794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com