Qt Creator:AlwaysStaysOnTop阻止打开文件提示
Qt Creator: AlwaysStaysOnTop blocks open file prompt
我的主窗口上有一个按钮,允许用户选择要打开的文件。
std::fstream infile;
std::string filename = QFileDialog::getOpenFileName(this, tr("TXT file"), qApp->applicationDirPath (),tr("TXT File (*.txt)")).toStdString();
if (filename.empty())
return;
infile.open(filename, std::fstream:: in | std::fstream::out | std::fstream::app);
if (true) {
//Does stuff with the data
}
infile.close();
这通常工作正常,我已经在以前的 gui qt 应用程序中使用它。 但是,对于此应用程序,主窗口(在其设置时)设置其窗口标志,如下所示:
setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
这会产生一个问题,因为主窗口试图始终保持在顶部(从而阻止打开的文件窗口出现)。 如果没有 staysontop 标志,文件对话框将正常工作。
有没有办法暂时禁用此标志(这样我就可以在单击按钮时禁用,然后在文件对话框完成后重新启用)?
setWindowFlags(Qt::FramelessWindowHint| ~Qt::WindowStaysOnTopHint);
这似乎是最常见的建议解决方案,但对我不起作用。 我相信这是因为必须重新创建窗口才能注册对窗口标志的更改 - 但是,我相信如果我杀死主窗口,文件对话框也会超出范围?
总之,我正在尝试找到一种解决方法,使主窗口始终位于顶部,除非我尝试选择要打开的文件(由按钮触发的文件对话框)。
问题不在于标志,而是一个发出信号的计时器,它更新了窗口位置:
QTimer* timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update_pos()));
timer->start(50);
其中update_pos函数如下:
void MainWindow::update_pos(){
RECT rect;
if (GetWindowRect(target_window, &rect)) {
SetWindowPos((HWND)this->winId(), HWND_TOPMOST, rect.left, rect.top, 0, 0, SWP_NOSIZE);
} else {
//maybe window was closed
qDebug() << "GetWindowRect failed";
QApplication::quit();
}
}
每当调用update_pos函数时,它都会将主窗口推到顶部(从而在打开的文件对话框上方)。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 读取文件并输入到矢量中
- 提示存档文件中的动态库依赖项
- qt检查文件中是否存在文件,如果它没有提示用户输入其位置,则将文件复制到程序工作目录
- Qt Creator:AlwaysStaysOnTop阻止打开文件提示
- 有条件地提示安装文件夹对话框
- >使用 RegCreateKeyEx() 的 Windows 可执行文件不会提示输入 UAC 管理员权限并失败?
- 在没有警告提示的情况下打开文件
- 文件不显示在cmd提示窗口
- Eclipse悬停提示可以显示头文件中的Doxygen注释吗