QFileDialog删除多个文件夹时崩溃

时间:2018-10-25 12:59:57

标签: qt qlabel qfiledialog qfile qgridlayout

下面是代码, 头文件 QtGuiApplication1.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__; 
}
}
}

Ui文件 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:“索引超出范围”,ASSERT失败,文件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:“索引超出范围”中,ASSERT失败,文件c:\ users \ qt \ work \ qt \ qtbase \ include \ qtcore ../../ src / corelib / tools / qlist.h ,第541行

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

1 个答案:

答案 0 :(得分:0)

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

一个问题:“试捕”的意义何在?不需要。