Qt Creator:AlwaysStaysOnTop阻止打开文件提示

Qt Creator: AlwaysStaysOnTop blocks open file prompt

本文关键字:文件 提示 Creator AlwaysStaysOnTop Qt      更新时间:2023-10-16

我的主窗口上有一个按钮,允许用户选择要打开的文件。

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函数时,它都会将主窗口推到顶部(从而在打开的文件对话框上方)。