-1

Below is the code, Header File 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();

};

Source File Code

#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 File 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>

I need to create a custom dialog box with password feature to save a file. It's working fine fine but If I try to open dialog box and in that If I delete and create multiple folders around 20 times or more than that then it crashes. I am not getting why does it crashes on calling exec() function for QFileDialog. Please help.

Attached Debugger logs below:

The thread 0x47b0 has exited with code 0 (0x0). The thread 0x640 has exited with code 0 (0x0). The thread 0x986c has exited with code 0 (0x0). The thread 0x9898 has exited with code 0 (0x0). The thread 0x6704 has exited with code 0 (0x0). The thread 0x5328 has exited with code 0 (0x0). The thread 0x193c has exited with code 0 (0x0). The thread 0x9698 has exited with code 0 (0x0). The thread 0x638 has exited with code 0 (0x0). The thread 0x80e0 has exited with code 0 (0x0). The thread 0x68c8 has exited with code 0 (0x0). The thread 0xa0d8 has exited with code 0 (0x0). The thread 0x4684 has exited with code 0 (0x0). ASSERT failure in QList::at: "index out of range", file c:\users\qt\work\qt\qtbase\include\qtcore../../src/corelib/tools/qlist.h, line 541 Debug Error!

Program: C:\Qt\Qt5.9.2\5.9.2\msvc2015_64\bin\Qt5Cored.dll Module: 5.9.2 File: c:\users\qt\work\qt\qtbase\include\qtcore../../src/corelib/tools/qlist.h Line: 541

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

(Press Retry to debug the application) QtGuiApplication1.exe has triggered a breakpoint.

sk110
  • 77
  • 2
  • 9
  • 2
    Please correct the formatting in your question. – G.M. Oct 25 '18 at 13:19
  • @G.M. I have formatted the code. Please have a look at it. – sk110 Oct 25 '18 at 19:13
  • Can you clarify what you mean by `"...Crashes application on deleting multiple folders"` -- the code shown doesn't delete anything as far as I can see. Run the program under a debugger and get the backtrace at the crash point. As an aside, you really shouldn't be using `C`-style casts such as `layout = (QGridLayout*)dialogBox.layout();` -- use [`dynamic_cast`](https://en.cppreference.com/w/cpp/language/dynamic_cast) instead. – G.M. Oct 26 '18 at 07:33
  • When I click on push button it opens the file dialog box. In that If I create few folders in any directory(approx 20) and then delete those directory then it crashes. – sk110 Oct 26 '18 at 12:40

1 Answers1

0

Probably you are leaking memory. I can suggest you to create the QFileDialog just once, then show/hide it (in your slot) instead of recreating it every time.

Just a question: what's the point of that "try-catch"? It should not be needed.

JuanDeLosMuertos
  • 4,532
  • 15
  • 55
  • 87
  • Yeah, I know. But it helps in my application and crashes after some time. e.g If I don't use try catch block then on creating two three folders and deleting them the application crashes. But on using try catch block it crashes but on deleting 20 folders or above. – sk110 Oct 26 '18 at 12:46
  • And I don't think it's happening due to memory leak. I am not creating multiple instance of filedialog. In first instance only in dialog box I am creating multiple folders and deleting them. – sk110 Oct 26 '18 at 16:31
  • You should show the code which deletes the folders. – JuanDeLosMuertos Oct 29 '18 at 10:57