从QItemDelegate的自定义编辑器调用getOpenFileName时崩溃 [英] Crash when calling getOpenFileName from QItemDelegate's custom editor

查看:220
本文介绍了从QItemDelegate的自定义编辑器调用getOpenFileName时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先决条件:

我有一个自定义QItemDelegate,它为树视图项目创建了自定义编辑器. 编辑器由QLineEdit和QToolButton组成.该按钮用于打开文件对话框,以选择文件并将其路径放入行编辑中. 我使用对QFileDialog :: getOpenFileName的调用来打开文件对话框.该对话框是Windows的本机文件对话框(这很重要).

I have a custom QItemDelegate which creates custom editor for treeview items. The editor consists of a QLineEdit and QToolButton. The button is used to open the file dialog for selecting the file and placing it's path into line edit. I use a call to QFileDialog::getOpenFileName to open the file dialog. The dialog is native Windows file dialog (this is important).

第一次创建编辑器时,无论我按该按钮多少次,一切正常.但是在关闭编辑器并再次将其重新打开后,第一次按下该按钮会导致崩溃.崩溃的原因是我的自定义编辑器最终被删除了.

When the editor is created the first time, everything works ok no matter how often I press that button. But after the editor is closed and reopened again the first press on the button results in a crash. The reason of the crash is that my custom editor eventually gets deleted.

这是一些代码:

void CCustomEditor::on_RunSetupBtn_clicked()
{
    auto qFilename = QFileDialog::getOpenFileName(this,
        "Select application to run",
        QString(),
        "Executable files (*.exe)");

    if (qFilename.isEmpty())
        return;

    SetCommandLine(QDir::toNativeSeparators(qFilename), m_qParameters);
}

推荐答案

我对getOpenFileName和getSaveFileName有类似的问题.我的解决方案是,在getOpenFileName之后,getSaveFileName之后或在函数末尾使用activateWindow()将窗口设置为活动窗口.

I have a similar problem with getOpenFileName and getSaveFileName. My solution is, after getOpenFileName, getSaveFileName or at the end of the function set the windows as active with activateWindow().

这篇关于从QItemDelegate的自定义编辑器调用getOpenFileName时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆