使用Qt编程。界⾯上有⼀个⽂本显示框 QLabel,计算 2 到 50 的斐波那契数列结果,每次计算完成后,将计算结果显示在 QLabel上。 要求: 计算过程中界⾯不能卡顿; 程序关闭时⽴即退出,并且不会出现崩溃; 分别使⽤QThread和moveToThread两种⽅式实现。
时间: 2024-02-17 16:01:35 浏览: 63
使用QThread实现:
```cpp
// fibthread.h
#ifndef FIBTHREAD_H
#define FIBTHREAD_H
#include <QThread>
class FibThread : public QThread {
Q_OBJECT
public:
explicit FibThread(QObject *parent = nullptr);
signals:
void result(int);
protected:
void run() override;
};
#endif // FIBTHREAD_H
// fibthread.cpp
#include "fibthread.h"
FibThread::FibThread(QObject *parent) : QThread(parent) {}
void FibThread::run() {
int fib1 = 0, fib2 = 1;
for (int i = 2; i <= 50; ++i) {
int fib = fib1 + fib2;
fib1 = fib2;
fib2 = fib;
emit result(fib);
}
}
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QThread>
namespace Ui {
class MainWindow;
}
class FibThread;
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void onCalculateFinished(int fib);
private:
Ui::MainWindow *ui;
FibThread *fibThread;
};
#endif // MAINWINDOW_H
// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "fibthread.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
fibThread(new FibThread(this))
{
ui->setupUi(this);
connect(fibThread, &FibThread::result, this, &MainWindow::onCalculateFinished);
fibThread->start();
}
MainWindow::~MainWindow()
{
delete ui;
fibThread->quit();
fibThread->wait();
}
void MainWindow::onCalculateFinished(int fib) {
ui->label->setText(QString::number(fib));
}
```
使用moveToThread实现:
```cpp
// fibworker.h
#ifndef FIBWORKER_H
#define FIBWORKER_H
#include <QObject>
#include <QThread>
class FibWorker : public QObject {
Q_OBJECT
public:
explicit FibWorker(QObject *parent = nullptr);
public slots:
void calculate();
signals:
void result(int);
};
#endif // FIBWORKER_H
// fibworker.cpp
#include "fibworker.h"
FibWorker::FibWorker(QObject *parent) : QObject(parent) {}
void FibWorker::calculate() {
int fib1 = 0, fib2 = 1;
for (int i = 2; i <= 50; ++i) {
int fib = fib1 + fib2;
fib1 = fib2;
fib2 = fib;
emit result(fib);
}
}
// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QThread>
namespace Ui {
class MainWindow;
}
class FibWorker;
class MainWindow : public QMainWindow {
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void onCalculateFinished(int fib);
private:
Ui::MainWindow *ui;
QThread *workerThread;
FibWorker *fibWorker;
};
#endif // MAINWINDOW_H
// mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "fibworker.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
workerThread(new QThread(this)),
fibWorker(new FibWorker)
{
ui->setupUi(this);
fibWorker->moveToThread(workerThread);
connect(workerThread, &QThread::finished, fibWorker, &QObject::deleteLater);
connect(this, &MainWindow::destroyed, workerThread, &QThread::quit);
connect(fibWorker, &FibWorker::result, this, &MainWindow::onCalculateFinished);
connect(this, &MainWindow::destroyed, fibWorker, &FibWorker::deleteLater);
workerThread->start();
QMetaObject::invokeMethod(fibWorker, "calculate", Qt::QueuedConnection);
}
MainWindow::~MainWindow()
{
delete ui;
workerThread->quit();
workerThread->wait();
}
void MainWindow::onCalculateFinished(int fib) {
ui->label->setText(QString::number(fib));
}
```
阅读全文