QFileDialog::getOpenFileName in console application
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
操作系统: 乌班图 12.04
Qt:4.8.2 从后备箱构建
你可能想试试
QEventLoop loop;
while (loop.processEvents())
/* nothing */;
我发现有时有必要再次调用循环......
在Windows 7上,使用Qt4.8.1和Qt4.8.3编译,openDialog是自然的,隐藏的使用后。
你能描述更多,在你身边发生了什么吗?
相关文章:
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 如何识别符号表中的符号是我'application'函数
- 无法创建 DLL:获取 DLL "is not a valid Win32 application"
- C++ DLL 运行时错误"abc.dll is not a valid WIN32 application" 。请帮助解决这个问题
- Gtk::Main 和 Gtk::Application::create 有什么区别?
- V8 无法使用名称设置对象模板"console"
- Windows Console API鼠标事件只是进入选择模式
- C++ Console::WriteLine()-like 方法,没有 .net 框架
- 如何使用 c++ 在 "Application and Services Logs" 部分创建事件日志
- 尝试执行IPConfig时,Console System()C 无限循环
- 无法使用 Visual Studio 2010 和 Qt 版本 4.8.0 创建"Qt Gui Application"
- Qt Application with QTabWidget, QGraphicsScene and QPushButt
- 仅在一台计算机上出现"0xc000007b Application Unable to Start Correctly"错误
- webextension native application c++ hello world
- Qtapplication.h : 没有这样的文件或目录
- QThreadPool maxThreadCount 在 Application 和 DLL 中不同
- 使用pipe()和fork()从文件中读取并输出到console/new文件
- GCC创建MIME类型应用程序/X-SharedLib而不是应用程序/X-Application
- Winelib - console app
- QFileDialog::getOpenFileName in console application