Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 如何以编程方式关闭QFileDialog?_Qt - Fatal编程技术网

Qt 如何以编程方式关闭QFileDialog?

Qt 如何以编程方式关闭QFileDialog?,qt,Qt,我需要在应用程序测试中处理并关闭QFileDialog。 由以下程序调用的对话框: QFileDialog::getOpenFileName( ... ); 在测试中,我通过以下方式“捕获”此对话框: QApplication::topLevelWidgets(); 然后我选择了QFileDialog API所需的文件 现在我应该关闭对话框,从QFileDialog::getOpenFileName()获取有效答案(文件名); 但是QdiLog的插槽没有效果(accept()、reject(

我需要在应用程序测试中处理并关闭QFileDialog。 由以下程序调用的对话框:

QFileDialog::getOpenFileName( ... );
在测试中,我通过以下方式“捕获”此对话框:

QApplication::topLevelWidgets();
然后我选择了QFileDialog API所需的文件

现在我应该关闭对话框,从QFileDialog::getOpenFileName()获取有效答案(文件名); 但是QdiLog的插槽没有效果(accept()、reject()、close()…)。对话框保持打开状态

所描述的解决方案是可行的,但在我的案例中没有选择。我必须使用标准对话框


有什么方法可以正确关闭它吗?

QFileDialog::getOpenFileName是一种静态方法,因此您受到如何使用它的限制

如果您想要更多的控制,我建议您创建一个QFileDialog实例并使用它。通过调用实例的函数,可以通过编程方式关闭对话框

作为对这不起作用的评论的回应,下面是示例代码:-

// Must create the FileDialog on the heap, so we can call close and the dialog is deleted
// Set the Qt::WA_DeleteOnClose flag if the instance is still required
QFileDialog* fileDlg = new QFileDialog(this, QString("Select Config file"), QDir::homePath(),    QString("Config (*.xml)"));

// One shot timer to close the dialog programmatically
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [=]() 
{
    fileDlg->close();
    timer->deleteLater();
} );

timer->start(3000);
fileDlg->exec();

QFileDialog::getOpenFileName是一个静态方法,因此您受到如何使用它的限制

如果您想要更多的控制,我建议您创建一个QFileDialog实例并使用它。通过调用实例的函数,可以通过编程方式关闭对话框

作为对这不起作用的评论的回应,下面是示例代码:-

// Must create the FileDialog on the heap, so we can call close and the dialog is deleted
// Set the Qt::WA_DeleteOnClose flag if the instance is still required
QFileDialog* fileDlg = new QFileDialog(this, QString("Select Config file"), QDir::homePath(),    QString("Config (*.xml)"));

// One shot timer to close the dialog programmatically
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [=]() 
{
    fileDlg->close();
    timer->deleteLater();
} );

timer->start(3000);
fileDlg->exec();

QFileDialog::getOpenFileName是一个静态方法,因此您受到如何使用它的限制

如果您想要更多的控制,我建议您创建一个QFileDialog实例并使用它。通过调用实例的函数,可以通过编程方式关闭对话框

作为对这不起作用的评论的回应,下面是示例代码:-

// Must create the FileDialog on the heap, so we can call close and the dialog is deleted
// Set the Qt::WA_DeleteOnClose flag if the instance is still required
QFileDialog* fileDlg = new QFileDialog(this, QString("Select Config file"), QDir::homePath(),    QString("Config (*.xml)"));

// One shot timer to close the dialog programmatically
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [=]() 
{
    fileDlg->close();
    timer->deleteLater();
} );

timer->start(3000);
fileDlg->exec();

QFileDialog::getOpenFileName是一个静态方法,因此您受到如何使用它的限制

如果您想要更多的控制,我建议您创建一个QFileDialog实例并使用它。通过调用实例的函数,可以通过编程方式关闭对话框

作为对这不起作用的评论的回应,下面是示例代码:-

// Must create the FileDialog on the heap, so we can call close and the dialog is deleted
// Set the Qt::WA_DeleteOnClose flag if the instance is still required
QFileDialog* fileDlg = new QFileDialog(this, QString("Select Config file"), QDir::homePath(),    QString("Config (*.xml)"));

// One shot timer to close the dialog programmatically
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
connect(timer, &QTimer::timeout, [=]() 
{
    fileDlg->close();
    timer->deleteLater();
} );

timer->start(3000);
fileDlg->exec();

在Windows中,可以使用WinAPI关闭对话框:

#define WAIT(A) while (!(A)) {}
HWND dialogHandle, button;
WAIT(dialogHandle = FindWindow(NULL, L"Open")); //write here title of the dialog
WAIT(button = FindWindowEx(dialogHandle, NULL, L"Button", L"&Open")); //write here title of the button to click
SendMessage(button, BM_CLICK, 0, 0);

在Windows中,可以使用WinAPI关闭对话框:

#define WAIT(A) while (!(A)) {}
HWND dialogHandle, button;
WAIT(dialogHandle = FindWindow(NULL, L"Open")); //write here title of the dialog
WAIT(button = FindWindowEx(dialogHandle, NULL, L"Button", L"&Open")); //write here title of the button to click
SendMessage(button, BM_CLICK, 0, 0);

在Windows中,可以使用WinAPI关闭对话框:

#define WAIT(A) while (!(A)) {}
HWND dialogHandle, button;
WAIT(dialogHandle = FindWindow(NULL, L"Open")); //write here title of the dialog
WAIT(button = FindWindowEx(dialogHandle, NULL, L"Button", L"&Open")); //write here title of the button to click
SendMessage(button, BM_CLICK, 0, 0);

在Windows中,可以使用WinAPI关闭对话框:

#define WAIT(A) while (!(A)) {}
HWND dialogHandle, button;
WAIT(dialogHandle = FindWindow(NULL, L"Open")); //write here title of the dialog
WAIT(button = FindWindowEx(dialogHandle, NULL, L"Button", L"&Open")); //write here title of the button to click
SendMessage(button, BM_CLICK, 0, 0);

要显示本机对话框,必须运行
exec()
或调用其中一个静态函数

不幸的是,在Windows中,这会调用Windows API中的阻塞函数,使显示的对话框成为模态,运行自己的事件循环。如果不返回Qt事件循环,则无法使用信号/插槽接口执行
close()
函数

我试图通过直接从另一个线程调用
close()
函数来绕过这个问题,但这会导致Qt试图向底层对话框发送事件。由于在Qt中不允许跨线程边界发送(而不是发布)事件,因此会生成致命错误

所以,至少对于Windows来说,这是不可能的

我没有在Windows以外的平台上进行测试。我使用的代码是:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    QFileDialog* fileDlg = new QFileDialog(0, QString("Select Config file"), QDir::homePath(),    QString("Config (*.xml)"));

    // spawn a new thread
    QtConcurrent::run([=](){
        QTimer timer;
        timer.setSingleShot(true);

        QEventLoop *loop = new QEventLoop;

        QObject::connect(&timer, &QTimer::timeout, [=](){
            fileDlg->close();
            fileDlg->deleteLater();
            loop->quit();
        });

        timer.start(3000);
        loop->exec();

        delete loop;
    });

    fileDlg->exec();

    return a.exec();
}

要显示本机对话框,必须运行
exec()
或调用其中一个静态函数

不幸的是,在Windows中,这会调用Windows API中的阻塞函数,使显示的对话框成为模态,运行自己的事件循环。如果不返回Qt事件循环,则无法使用信号/插槽接口执行
close()
函数

我试图通过直接从另一个线程调用
close()
函数来绕过这个问题,但这会导致Qt试图向底层对话框发送事件。由于在Qt中不允许跨线程边界发送(而不是发布)事件,因此会生成致命错误

所以,至少对于Windows来说,这是不可能的

我没有在Windows以外的平台上进行测试。我使用的代码是:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    QFileDialog* fileDlg = new QFileDialog(0, QString("Select Config file"), QDir::homePath(),    QString("Config (*.xml)"));

    // spawn a new thread
    QtConcurrent::run([=](){
        QTimer timer;
        timer.setSingleShot(true);

        QEventLoop *loop = new QEventLoop;

        QObject::connect(&timer, &QTimer::timeout, [=](){
            fileDlg->close();
            fileDlg->deleteLater();
            loop->quit();
        });

        timer.start(3000);
        loop->exec();

        delete loop;
    });

    fileDlg->exec();

    return a.exec();
}

要显示本机对话框,必须运行
exec()
或调用其中一个静态函数

不幸的是,在Windows中,这会调用Windows API中的阻塞函数,使显示的对话框成为模态,运行自己的事件循环。如果不返回Qt事件循环,则无法使用信号/插槽接口执行
close()
函数

我试图通过直接从另一个线程调用
close()
函数来绕过这个问题,但这会导致Qt试图向底层对话框发送事件。由于在Qt中不允许跨线程边界发送(而不是发布)事件,因此会生成致命错误

所以,至少对于Windows来说,这是不可能的

我没有在Windows以外的平台上进行测试。我使用的代码是:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    QFileDialog* fileDlg = new QFileDialog(0, QString("Select Config file"), QDir::homePath(),    QString("Config (*.xml)"));

    // spawn a new thread
    QtConcurrent::run([=](){
        QTimer timer;
        timer.setSingleShot(true);

        QEventLoop *loop = new QEventLoop;

        QObject::connect(&timer, &QTimer::timeout, [=](){
            fileDlg->close();
            fileDlg->deleteLater();
            loop->quit();
        });

        timer.start(3000);
        loop->exec();

        delete loop;
    });

    fileDlg->exec();

    return a.exec();
}

要显示本机对话框,必须运行
exec()
或调用其中一个静态函数

不幸的是,在Windows中,这会调用Windows API中的阻塞函数,使显示的对话框成为模态,运行自己的事件循环。如果不返回Qt事件循环,则无法使用信号/插槽接口执行
close()
函数

我试图通过直接从另一个线程调用
close()
函数来绕过这个问题,但这会导致Qt试图向底层对话框发送事件。由于在Qt中不允许跨线程边界发送(而不是发布)事件,因此会生成致命错误

所以,至少对于Windows来说,这是不可能的

我没有在Windows以外的平台上进行测试。我使用的代码是:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);


    QFileDialog* fileDlg = new QFileDialog(0, QString("Select Config file"), QDir::homePath(),    QString("Config (*.xml)"));

    // spawn a new thread
    QtConcurrent::run([=](){
        QTimer timer;
        timer.setSingleShot(true);

        QEventLoop *loop = new QEventLoop;

        QObject::connect(&timer, &QTimer::timeout, [=](){
            fileDlg->close();
            fileDlg->deleteLater();
            loop->quit();
        });

        timer.start(3000);
        loop->exec();

        delete loop;
    });

    fileDlg->exec();

    return a.exec();
}

正如我所说,我“捕捉”了应用程序小部件之间的对话。(无dlg->getOpenFileName();dlg->close();)close()-对标准对话框无效。(它只适用于Qt的对话框)。我不明白你所说的“标准对话框”是什么意思。我添加到答案中的代码在打开对话框3秒钟后关闭该对话框。@Merlin069,我认为“标准直径”