gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:55:01 26 4
gpt4 key购买 nike

这是在 qtforum.org 上首次发布的问题我在哪里 got no answer :

我无法在控制台应用程序中隐藏使用过的“打开”对话框。以下是用于测试此行为的 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 */;

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

关于c++ - 控制台应用程序中的 QFileDialog::getOpenFileName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12866490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com