這是qtforum.org先貼在那裏我已經got no answer一個問題:QFileDialog :: getOpenFileName在控制檯應用程序
我有麻煩躲藏在一個控制檯應用程序的打開對話框已被使用之後。 這裏是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從主幹上構建
搞怪,與我的openSUSE的Qt 4.7.4,對話正在消失就好了。 ..所以,我不能真正幫助。你可能想嘗試'QEventLoop循環; while(loop.processEvents())/ * nothing * /;'。我發現有時需要再次調用循環... –
QEventLoop循環; while(loop.processEvents())/ * nothing * /;做的伎倆。你可以張貼這個作爲ansewr嗎?謝謝! –
這樣做了。很高興我能幫上忙! –