0

This is a question first posted on qtforum.org where I've got no answer:

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;
}

I build the application using this .pro file:

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

OS: Ubuntu 12.04

Qt: 4.8.2 builded from the trunk

Nicu Tofan
  • 1,052
  • 14
  • 34
  • Funny, with Qt 4.7.4 on my openSUSE, the dialog is disappearing just fine... So, I cannot really help. You might want to try `QEventLoop loop; while (loop.processEvents()) /* nothing */;`. I found it sometimes necessary to call the loop again... – Tilman Vogel Oct 12 '12 at 21:35
  • QEventLoop loop; while (loop.processEvents()) /* nothing */; does the trick. Can you post this as an ansewr, please? Thank you! – Nicu Tofan Oct 13 '12 at 09:28

2 Answers2

1

You might want to try

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

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

Tilman Vogel
  • 9,337
  • 4
  • 33
  • 32
0

On Windows 7, compiled using Qt4.8.1 and Qt4.8.3 and openDialog was natural and hides after using.

Can you describe more, what happens at your side?

NG_
  • 6,895
  • 7
  • 45
  • 67
  • Hello, troyane, thanks for testing. The dialog remains visible but buttons are not accesible (like disabled). A new invokation will hide old dialog and show a new one. However, Tilman Vogel's solution is working - QEventLoop loop; while (loop.processEvents()) /* nothing */; will hide the dialog – Nicu Tofan Oct 13 '12 at 09:26