c++ - 如何在程序后台运行函数(特别是自动保存函数)? QT/C++

标签 c++ qt qt-creator

在我的代码中,我想集成一个每隔几秒左右运行一次的自动保存功能。我希望它在后台运行,因为我还有其他东西要同时运行。那么我该怎么做呢?

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <fstream>
#include <QFile>
#include <QDebug>
using namespace std;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow) {
    ui->setupUi(this);

    // Setup code
    ui->textEdit->setReadOnly(true);
    ui->textEdit->append("Select one of the buttons on the left to pick a log");

}

MainWindow::~MainWindow() {
    delete ui;
}

string lastSavedText[] = {
    " ",

        " "
    };

QString qLastSavedTextHome, qLastSavedTextWork;

这是我的第一个按钮

void MainWindow::on_homeButton_clicked() {
    // Preparing text edit
    ui->textEdit->setReadOnly(false);
    ui->textEdit->clear();
    ui->textEdit->setOverwriteMode(true);

    // Loading previously saved text
    QFile file { "home.apl" };
    if ( !file.open(QIODevice::ReadOnly | QIODevice::Text) ) {
        qDebug() << "Could not open file!";
        return;
    }

    const auto& lastSavedText = file.readAll();
    file.close();

    ui->textEdit->setPlainText( lastSavedText );
}

这是我的第二个

void MainWindow::on_workButton_clicked() {
    // Preparing text edit
    ui->textEdit->setReadOnly(false);
    ui->textEdit->clear();
    ui->textEdit->setOverwriteMode(true);

    // Loading previously saved text
    QFile file2 { "work.apl" };
    if ( !file2.open(QIODevice::ReadOnly | QIODevice::Text) ) {
        qDebug() << "Could not open file!";
        return;
    }

    const auto& lastSavedText = file2.readAll();
    file2.close();

    ui->textEdit->setPlainText( lastSavedText );

}

这是我希望通过自动保存消除的保存按钮

void MainWindow::on_saveButton_clicked() {

    // Converts textEdit to string
    QString textEditText = ui->textEdit->toPlainText();
    lastSavedText[0] = textEditText.toStdString();

    // Saving files
    ofstream home;
    home.open("home.apl");
    home << lastSavedText[0];
    home.close();

    ofstream work;
    work.open("work.apl");
    work << lastSavedText[1];
    work.close();
}

最佳答案

有 2 个解决方案。

简单的

只需使用一个计时器来执行保存按钮的代码。您可以设置定时器在任意时间段内执行。

QTimer

但是如果此操作花费太多时间,可能会导致软件卡住。在这种情况下,您可以将保存的函数放在线程中。


线程

您可以使用线程来做到这一点。

线程,基本上是一个与主进程分离的进程,可以同时运行,每个线程都做自己的工作。

请注意,线程间通信最安全的方法是使用信号。

Qt Threads Documentation

Example

void MyObject::startWorkInAThread()
{
    WorkerThread *workerThread = new WorkerThread(this);
    connect(workerThread, SIGNAL(resultReady(QString)), this, SLOT(handleResults(QString)));
    connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
    workerThread->start();
}

关于c++ - 如何在程序后台运行函数(特别是自动保存函数)? QT/C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45069997/

相关文章:

c++ - Anaconda 在 Mac 上在哪里安装库?

c++ - 在 C++ 中从 vector 中删除元素

c++ - Qt - 在 Windows 上按下 Alt 后防止菜单栏获取焦点

c++ - 如何使用 Qt 在 D-Bus 上创建/实例化配对代理

c++ - 根据触发的事件连接任意两个(多个)表单

c++ - Qt Creator IDE 似乎错误地将 reinterpret_cast<::GlobalType> 标记为无效

c++ - vector的第一个元素的地址是固定的吗?

c++ - 如何强制多个 View 显示同一模型行

c++ - 什么可以解释 std::cout 不显示任何内容?

c++ - Visual Studio 2015 : Compile C/C++ without a runtime library