控制台应用程序中的QFileDialog :: getOpenFileName [英] QFileDialog::getOpenFileName in console application

查看:448
本文介绍了控制台应用程序中的QFileDialog :: getOpenFileName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个首先发布在使用后,我无法在控制台应用程序中隐藏打开"对话框. 这是用于测试此行为的main.cc文件的内容:

I have trouble hiding the Open dialog in a console application after it has been used. Here is the content of main.cc file used to test this behaviour:

#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文件构建应用程序:

I build the application using this .pro file:

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

操作系统:Ubuntu 12.04

OS: Ubuntu 12.04

Qt:4.8.2从主干构建

Qt: 4.8.2 builded from the trunk

推荐答案

您可能想尝试

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

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

I found it sometimes necessary to call the loop again...

这篇关于控制台应用程序中的QFileDialog :: getOpenFileName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆