将指针传递给以参数作为参数的函数

Passing pointer to a function with parameters as parameter

本文关键字:参数 函数 指针      更新时间:2023-10-16

我有代码,我需要在其中实现将指针传递给带有参数的函数,就像下面的代码一样,但这似乎是不可能的。

bool StudentAbsenceTableApp::loadFile(const QString &fileName)
{
    fw.moveToThread(this->thread());
    fw.setParent(this);
    //I need: &(StudentAbsenceTableApp::experimentFunction(fileName)
    QFuture<bool> future =  QtConcurrent::run(this, &StudentAbsenceTableApp::experimentFunction);
    fw.setFuture(future);
    progressBar->show();
}
// I need: experimentFunction(const QString& fileName)
bool StudentAbsenceTableApp::experimentFunction()
{
    QString fileName = "/media/bsuir/data.xml";
    XMLParser *xmlParser = new XMLParser(model);
    xmlParser->read(fileName);
    setCurrentFileName(fileName);
    statusBar()->showMessage(tr("Файл загружен"), 2000);
    documentModified = false;
    return true;
}

但是没有办法,我能找到。

QtConcurrent::run 支持将参数传递给函数。

QFuture<bool> future =  QtConcurrent::run(this, &StudentAbsenceTableApp::experimentFunction, fileName);

请参阅文档 : http://doc.qt.io/qt-4.8/qtconcurrentrun.html#passing-arguments-to-the-function