介绍
我想要的非常简单:我想通过主 ui 线程中的按钮开始读取二进制文件的过程,让单独的线程处理包括转换在内的处理QImage
并将该图像返回到主 ui 线程它应该显示在标签中。
因此我使用 Qt 的信号/槽机制及其线程功能。
我已经有了一个单线程工作解决方案,但是当使用线程尝试时,它会在我不理解的完全任意步骤上崩溃,因为整个过程是完全封装的,而不是时间关键......
工作单线程解决方案:
TragVisMain
是一个QMainWindow
:
class TragVisMain : public QMainWindow
按下按钮readBinSingle
开始该过程:
void TragVisMain::on_readBinSingle_clicked()
{
// Open binary file
FILE *imageBinFile = nullptr;
imageBinFile = fopen("imageFile.bin", "rb");
if(imageBinFile == NULL) {
return;
}
// Get binary file size
fseek(imageBinFile, 0, SEEK_END); // seek to end of file
size_t size = static_cast<size_t>(ftell(imageBinFile)); // get current file pointer
fseek(imageBinFile, 0, SEEK_SET);
// Read binary file
void *imageData = malloc(size);
fread(imageData, 1, size, imageBinFile);
// Create cv::Mat
cv::Mat openCvImage(1024, 1280, CV_16UC1, imageData);
openCvImage.convertTo(openCvImage, CV_8UC1, 0.04); // Convert to 8 Bit greyscale
// Transform to QImage
QImage qImage(
openCvImage.data,
1280,
1024,
QImage::Format_Grayscale8
);
// Show image in label, 'imageLabel' is class member
imageLabel.setPixmap(QPixmap::fromImage(qImage));
imageLabel.show();
}
这就像一个魅力,但当然 ui 被阻止了。
不工作的多线程解决方案
正如您将看到的代码与上面的代码基本相同,只是移动到另一个类DoCameraStuff
。TragVisMain
因此,这是头文件中用于此目的的主要组件:
namespace Ui {
class TragVisMain;
}
class TragVisMain : public QMainWindow
{
Q_OBJECT
public:
explicit TragVisMain(QWidget *parent = nullptr);
~TragVisMain();
private:
Ui::TragVisMain *ui;
DoCameraStuff dcs;
QLabel imageLabel;
QThread workerThread;
public slots:
void setImage(const QImage &img); // Called after image processing
// I have also tried normal parameter 'setImage(QImage img)', non-const referenc 'setImage(const QImage &img)' and pointer 'setImage(QImage *img)'
private slots:
void on_readBin_clicked(); // Emits 'loadBinaryImage'
// void on_readBinSingle_clicked();
signals:
void loadBinaryImage(); // Starts image processing
};
DoCameraStuff
只是一个QObject
:
class DoCameraStuff : public QObject
{
Q_OBJECT
public:
explicit DoCameraStuff(QObject *parent = nullptr);
public slots:
void readBinaryAndShowBinPic();
signals:
void showQImage(const QImage &image);
};
移动dcs
到workerThread
and connect
ing 信号和槽发生在 的构造函数中TragVisMain
:
TragVisMain::TragVisMain(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::TragVisMain),
dcs()
{
ui->setupUi(this);
dcs.moveToThread(&workerThread);
// Object should be deletable after the thread finished!
connect(&workerThread, &QThread::finished, &dcs, &QObject::deleteLater);
// For starting the image processing
connect(this, &TragVisMain::loadBinaryImage, &dcs, &DoCameraStuff::readBinaryAndShowBinPic);
// Showing QImage after image processing is done
connect(&dcs, &DoCameraStuff::showQImage, this, &TragVisMain::setImage);
// Start workerThread
workerThread.start();
}
通过按下readBinSingle
按钮开始图像处理:
void TragVisMain::on_readBin_clicked()
{
emit loadBinaryImage(); // slot: readBinaryAndShowBinPic
}
该过程发生在DoCameraStuff::readBinaryAndShowBinPic
:
void DoCameraStuff::readBinaryAndShowBinPic() {
// Exact same code from single thread solution:
// Open binary file
FILE *imageBinFile = nullptr;
imageBinFile = fopen("imageFile.bin", "rb");
if(imageBinFile == NULL) {
return;
}
// Get binary file size
fseek(imageBinFile, 0, SEEK_END); // seek to end of file
size_t size = static_cast<size_t>(ftell(imageBinFile)); // get current file pointer
fseek(imageBinFile, 0, SEEK_SET);
// Read binary file
void *imageData = malloc(size);
fread(imageData, 1, size, imageBinFile);
// Create cv::Mat
cv::Mat openCvImage(1024, 1280, CV_16UC1, imageData);
openCvImage.convertTo(openCvImage, CV_8UC1, 0.04); // Convert to 8 Bit greyscale
// Transform to QImage
QImage qImage(
openCvImage.data,
1280,
1024,
QImage::Format_Grayscale8
);
// Send qImage to 'TragVisMain'
emit showQImage(qImage);
}
显示图像TragVisMain::setImage
:
void TragVisMain::setImage(const QImage &img)
{
imageLabel.setPixmap(QPixmap::fromImage(img));
imageLabel.show();
}
问题
好吧,多线程尝试只是在不同步骤中使整个应用程序崩溃而没有任何消息。但老实说,我没有任何想法。对我来说,这个DoCameraStuff
类是一个标准的工人类,在成员函数中做与时间无关的东西,没有任何关键关系。
我还检查了内部使用的某些函数是否不是线程安全的,但我在,和等效条件下DoCameraStuff::readBinaryAndShowBinPic
找不到任何问题。<cstdio>
cv::Mat
QImage
所以:
- 为什么多线程尝试会崩溃?
- 需要应用哪些更改,才能使线程中的进程不崩溃?
我总是感谢你的帮助。