[英]QFileDialog::getOpenFileName in console application
這是一個首先發布在qtforum.org上的問題,我沒有答案 :
使用完畢后,我無法在控制台應用程序中隱藏“打開”對話框。 這是用於測試此行為的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從主干構建
您可能要嘗試
QEventLoop loop;
while (loop.processEvents())
/* nothing */;
我發現有時有必要再次調用循環...
在Windows 7上,使用Qt4.8.1和Qt4.8.3以及openDialog進行編譯是很自然的,並且在使用后會隱藏。
您能否描述更多,您身邊會發生什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.