QFileDialog 在删除多个文件夹时使应用程序崩溃



下面是代码, 头文件 QtGui申请1.h

#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h" 
class QtGuiApplication1 : public QMainWindow 
{
Q_OBJECT
public:     
QtGuiApplication1(QWidget *parent = Q_NULLPTR);
private:    
Ui::QtGuiApplication1Class ui; 
public slots:
void on_pushButton_clicked();
};

源文件代码

#include "QtGuiApplication1.h"
#include<qdebug.h>
#include <qfiledialog.h>
#include <qlabel.h>
#include<qlineedit.h>
#include <qgridlayout.h> 
QtGuiApplication1::QtGuiApplication1(QWidget *parent)   : 
QMainWindow(parent) 
{
ui.setupUi(this); 
}
void QtGuiApplication1::on_pushButton_clicked()
{
qDebug() <<__FUNCTION__;
QFileDialog dialogBox; 
QLabel *passwordLabel = NULL; 
QLabel *message = NULL;     
QLineEdit *password = NULL;      
dialogBox.setAcceptMode(QFileDialog::AcceptOpen);    
dialogBox.setNameFilter("files(*.text)");    
dialogBox.setOption(QFileDialog::DontUseNativeDialog);
dialogBox.setStyleSheet("background:white;color: black;");
QGridLayout *layout = NULL; 
layout = (QGridLayout*)dialogBox.layout(); 
if (layout != NULL) 
{       
passwordLabel = new QLabel("Enter Password:");  
if (passwordLabel != NULL)  
{           
layout->addWidget(passwordLabel, 4, 0);     
}       
else        
{           
qDebug() << __FUNCTION__; 
}   
message = new QLabel("Please use vlc to play this file"); 
if (message != NULL)    
{       
layout->addWidget(message, 5, 0, 2, 2);     
}       
else        
{           
qDebug() << __FUNCTION__;   
}       
password = new QLineEdit();
if (password != NULL)   
{           
layout->addWidget(password, 4, 1);  
password->setEchoMode(QLineEdit::Password); 
}       
else    
{       
qDebug() << __FUNCTION__;   
}
try {   
int status = dialogBox.exec();  
if (status)         
{
QStringList FileNameList = dialogBox.selectedFiles();   
}
}   
catch (...)     
{           
qDebug()<<__FUNCTION__; 
}
}
}

用户界面文件 QtGuiApplication1.ui

<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0">  
<class>QtGuiApplication1Class</class> 
<widget class="QMainWindow" name="QtGuiApplication1Class">  
<property name="geometry">   
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>400</height>  
</rect>   
</property>  
<property name="windowTitle"> 
<string>QtGuiApplication1</string> 
</property>   
<widget class="QWidget" name="centralWidget"> 
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>170</x>
<y>140</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>   
</widget>  
</widget>  
<widget class="QMenuBar" name="menuBar">  
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>21</height>
</rect>  
</property> 
</widget>   
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>  
</attribute>   
<attribute name="toolBarBreak">
<bool>false</bool>  
</attribute>   
</widget> 
<widget class="QStatusBar" name="statusBar"/>
</widget>  <layoutdefault spacing="6" margin="11"/> 
<resources>   
<include location="QtGuiApplication1.qrc"/>
</resources>  <connections/> </ui>

我需要创建一个具有密码功能的自定义对话框来保存文件。 它工作正常,但是如果我尝试打开对话框,如果我删除并创建多个文件夹大约 20 次或更多,那么它会崩溃。我不明白为什么它在为 QFileDialog 调用 exec(( 函数时崩溃。请帮忙。

附加的调试器日志如下:

线程0x47b0已退出,代码为 0 (0x0(。 线程0x640已退出,代码为 0 (0x0(。 线程0x986c已退出,代码为 0 (0x0(。 线程0x9898已退出,代码为 0 (0x0(。 线程0x6704已退出,代码为 0 (0x0(。 线程0x5328已退出,代码为 0 (0x0(。 线程0x193c已退出,代码为 0 (0x0(。 线程0x9698已退出,代码为 0 (0x0(。 线程0x638已退出,代码为 0 (0x0(。 线程0x80e0已退出,代码为 0 (0x0(。 线程0x68c8已退出,代码为 0 (0x0(。 线程0xa0d8已退出,代码为 0 (0x0(。 线程0x4684已退出,代码为 0 (0x0(。 QList::at: "index out of range", file c:\users\qt\work\qt\qtbase\include\qtcore../../src/corelib/tools/qlist.h,第 541 行 调试错误!

程序: C:\Qt\Qt5.9.2\5.9.2\msvc2015_64\bin\Qt5Cored.dll 模块:5.9.2 文件: c:\users\qt\work\qt\qtbase\include\qtcore../../src/corelib/tools/qlist.h 行: 541

QList::at: "index out of range", file c:\users\qt\work\qt\qtbase\include\qtcore../../src/corelib/tools/qlist.h,第 541 行

(按重试以调试应用程序( QtGuiApplication1.exe 触发了一个断点。

可能是你漏了内存。我可以建议你只创建一次QFileDialog,然后显示/隐藏它(在你的插槽中(,而不是每次都重新创建它。

只是一个问题:这种"尝试捕获"的意义何在?它不应该是必需的。

最新更新