首页 > 解决方案 > QFileDialog 在删除多个文件夹时使应用程序崩溃

问题描述

下面是代码,头文件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 中的断言失败:“索引超出范围”,文件 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 中的断言失败:“索引超出范围”,文件 c:\users\qt\work\qt\qtbase\include\qtcore../../src/corelib/tools/qlist.h,第 541 行

(按重试调试应用程序) QtGuiApplication1.exe 已触发断点。

标签: qtqlabelqfiledialogqfileqgridlayout

解决方案


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

只是一个问题:那个“try-catch”有什么意义?它不应该是必需的。


推荐阅读