控制台应用程序中的QFileDialog :: getOpenFileName

时间:2012-10-12 20:18:00

标签: c++ qt console-application getopenfilename

这是我qtforum.org got no answer首先发布的问题:<{3}}:

使用后,我无法在控制台应用程序中隐藏“打开”对话框。 以下是用于测试此行为的main.cc文件的内容:

#include <QApplication>
#include <QFile>
#include <QFileDialog>
#include <QString>

bool b_closing = false;

static QString gofn ( void )
{
    QString    s_file;
    s_file = QFileDialog::getOpenFileName(
            qApp->activeWindow(),
            QObject::tr( "Select the file to open:" )
            );
    if ( !s_file.isEmpty() )
    {
        /* ... */
    }

    /* have no effect; */
    QApplication::processEvents();
    QApplication::sendPostedEvents();

    return s_file;
}

static void userInpLoop ( void )
{
    QFile    cons_inp;
    QFile    cons_outp;
    QString  s_ln;

    cons_inp.open( stdin, QIODevice::ReadOnly );
    cons_outp.open( stdout, QIODevice::WriteOnly );

    for ( ;; )
    {
        if ( b_closing )
            break;

        cons_outp.write( "\n>" );
        cons_outp.flush();
        s_ln = cons_inp.readLine().trimmed();

        if ( s_ln == "q" )
        {
            b_closing = true;
            cons_outp.write( "Closng...\n" );
        }
        else if ( s_ln == "gofn" )
        {
            cons_outp.write( gofn().toLatin1() );
        }
        else
        {
            cons_outp.write( "ERROR!!! \nInvalid input!\n" );
        }
        cons_outp.flush();
        //break; /* just to test that a.exec() hides the dialog */
    }

}

int main( int argc, char *argv[] )
{
    /* we choose QApplication instead of QCoreApplication because we need some Gui components */
    QApplication a(argc, argv);
    userInpLoop();
    //return a.exec(); /* this will hide the dialog */
    return 0;
}

我使用这个.pro文件构建应用程序:

QT += core gui
TARGET = test_gofn
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cc

操作系统:Ubuntu 12.04

Qt:从主干建造的4.8.2

2 个答案:

答案 0 :(得分:1)

您可能想尝试

QEventLoop loop; 
while (loop.processEvents()) 
    /* nothing */;

我发现有时需要再次调用循环...

答案 1 :(得分:0)

在Windows 7上,使用Qt4.8.1和Qt4.8.3和openDialog编译是自然的并且隐藏 使用完毕后。

你能描述一下吗,你身边会发生什么?