我用 QT/C++ 编写了一个应用程序。这个应用程序有多个类来管理窗口、treewidget..和一个自定义类。
该应用程序的目标是像 MacOSx 上的 QT/c++ 一样进行 android 文件传输。
目前整个应用程序在一个线程中工作,包括 UI 管理和 android 设备管理。 android 设备访问由名为 DeviceManager 的类管理。此类将主要打开设备、读取设备、添加/删除文件....
我想做的是创建一个线程来处理 DeviceManager 中定义的所有方法。我希望 UI 在一个线程上,devicemngr 在一个单独的线程上。
这是我当前的代码:
主要.cpp
int main(int argc, char *argv[])
{
PULS_mtp_error_t error = ERROR_GENERAL;
QThread PulsDeviceThread;
QApplication app(argc, argv);
DeviceMngr *MyMtp = new DeviceMngr;
error = MyMtp->OpenDevice();
...
MainUI MyWindow(*MyMtp);
MyWindow.show();
return app.exec();
}
MainUI 类定义如下
MainUI::MainUI(DeviceMngr& device) :
m_device(device)
{
m_closing = false;
setWindowTitle(QString::fromUtf8("PULS"));
resize(800,600);
setUnifiedTitleAndToolBarOnMac(true);
/* Creation of the Top bar section */
createBackForwardButton();
createLogoSection();
createAddFolder();
QWidget *TopBarWidget = new QWidget();
TopBarWidget->setFixedHeight(61);
QHBoxLayout *TopBarLayout = new QHBoxLayout(TopBarWidget);
TopBarLayout->addWidget(BackForwardSection);
TopBarLayout->addWidget(LogoSection);
TopBarLayout->addWidget(AddFolderSection);
/* Creation of Tree View Section */
createTreeView();
/* Creation of the bottom bar section */
createInfoSection();
/*about*/
aboutAction = new QAction(tr("&About"),this);
connect(aboutAction, SIGNAL(triggered()),this ,SLOT(aboutPuls()));
QMenu *helpMenu = new QMenu("Help", this);
helpMenu->addAction(aboutAction);
menuBar()->addMenu(helpMenu);
/*Overall Layout*/
QWidget *MainWindowWidget = new QWidget();
QVBoxLayout *MainWindowLayout = new QVBoxLayout(MainWindowWidget);
MainWindowLayout->setSpacing(0);
MainWindowLayout->addWidget(TopBarWidget);
MainWindowLayout->addWidget(TreeViewSection);
MainWindowLayout->addWidget(CreateInfoSection);
setCentralWidget(MainWindowWidget);
PulsUnplugged = false;
#if 1
activeTimer = new QTimer(this);
activeTimer->setSingleShot(false);
activeTimer->setInterval(200);
connect(activeTimer, SIGNAL(timeout()), this, SLOT(PulsDetection()));
activeTimer->start();
#endif
show();
}
某些方法(例如 createTreeView)也将使用 m_device 来访问设备。
void MainUI::createTreeView()
{
TreeViewSection = new QWidget();
QVBoxLayout *TreeViewLayout = new QVBoxLayout(TreeViewSection);
MyTreeWidget = new MyNewTreeWidget(m_device, *this);
TreeViewLayout->addWidget(MyTreeWidget);
}
MyNewTreeWidget 还需要访问 DeviceMngr 类。
大部分用于UI管理的类都可以访问到DeviceMngr类。我不知道如何使用 QThread 来确保所有 UI 类都可以访问 DeviceMngr 类。
我想在 main.cpp 中创建一个 Qthread,但我看不到如何在 main.cpp 中添加槽/信号,DeviceMngr 将有信号/槽与所有其他线程讨论。例如,主要需要打开设备并接收结果。
我是否需要在主类中创建所有信号/槽连接,或者我可以只在不同类中添加我需要的内容并在需要时创建连接。
有什么想法吗?我已经尝试了第一次实现,但效果并不理想。
谢谢
最佳答案
我建议创建工作线程并将您的 DeviceMngr
移动到它。它的槽(和整个事件循环)将在线程的上下文中运行,您必须使用 Qt 的信号/槽机制,以确保线程安全地从其他 QObject 访问 DeviceMngr
。
int main(...) {
// ...
QThread MtpThread;
DeviceMngr MyMtp;
MyMtp.moveToThread(&MtpThread);
// connect signals/slots of DeviceMngr
// ...
// launch the thread
MtpThread.start();
// should you need to call slots of DeviceMngr from main use metacalls
QMetaObject::invokeMethod(&MyMtp, "nameOfSlot");
// run application
// in the end join
MtpThread.quit(); // stop event queue
MtpThread.wait(); // join the thread
}
希望您能理解。关键是 moveToThread()
和元调用。
关于c++ - QThread的复杂使用——,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30130894/