Hi,
I am working on an application, where tabs are opened and closed dynamically - just like in webbrowsers. The problem is that, I keep experiencing random but regular crashes when closing a tab. I made a very short piece of code to demonstrate the problem:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtWidgets>
#include <mytab.h>
{
Q_OBJECT
bool deleteBusy = false;
public:
~MainWindow();
public slots:
void newTab();
void closeTab(int);
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtWidgets>
#include <mytab.h>
class MainWindow : public QMainWindow
{
Q_OBJECT
QWidget *central;
QListWidget *listWidget;
QTabWidget *tabWidget;
QGridLayout *layout;
bool deleteBusy = false;
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void newTab();
void closeTab(int);
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
#ifndef MYTAB_H
#define MYTAB_H
#include <QWidget>
#include <QtWidgets>
{
Q_OBJECT
public:
explicit myTab
(QWidget *parent
= 0);
signals:
public slots:
};
#endif // MYTAB_H
#ifndef MYTAB_H
#define MYTAB_H
#include <QWidget>
#include <QtWidgets>
class myTab : public QWidget
{
Q_OBJECT
public:
explicit myTab(QWidget *parent = 0);
QTableWidget *tableWidget;
QTableWidget *tableWidget2;
QGridLayout *layout;
signals:
public slots:
};
#endif // MYTAB_H
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
){
setCentralWidget(central);
for (int a = 0; a < 14 ; a++) // Just to make the listWidget easier to click
{
}
layout->addWidget(listWidget, 0, 0);
layout->addWidget(tabWidget, 1, 0);
tabWidget->addTab(new myTab, tr("Tab"));
QObject::connect(tabWidget,
SIGNAL(tabBarClicked
(int)),
this,
SLOT(closeTab
(int)) );
}
MainWindow::~MainWindow()
{
}
void MainWindow::newTab()
{
tabWidget->addTab(new myTab, tr("Tab"));
}
void MainWindow::closeTab(int)
{
if (deleteBusy == true)
QMessageBox::information(0,
"Information",
"Another tab is closing!" );
deleteBusy = true;
if (tabWidget->count() < 1)
QMessageBox::information(0,
"Information",
"No tabs!" );
QWidget *tab
= tabWidget
->currentWidget
();
tabWidget->removeTab(tabWidget->currentIndex());
if (tab != NULL)
{
delete tab;
tab = NULL;
}
else
{
QMessageBox::information(0,
"Information",
"Tab is null!" );
}
deleteBusy = false;
}
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
central = new QWidget(this);
setCentralWidget(central);
listWidget = new QListWidget();
for (int a = 0; a < 14 ; a++) // Just to make the listWidget easier to click
{
new QListWidgetItem(tr("Click here"), listWidget);
}
tabWidget = new QTabWidget();
layout = new QGridLayout(central);
layout->addWidget(listWidget, 0, 0);
layout->addWidget(tabWidget, 1, 0);
tabWidget->addTab(new myTab, tr("Tab"));
QObject::connect(listWidget, SIGNAL(clicked(QModelIndex)), this, SLOT(newTab()) );
QObject::connect(tabWidget, SIGNAL(tabBarClicked(int)), this, SLOT(closeTab(int)) );
}
MainWindow::~MainWindow()
{
}
void MainWindow::newTab()
{
tabWidget->addTab(new myTab, tr("Tab"));
}
void MainWindow::closeTab(int)
{
if (deleteBusy == true)
QMessageBox::information(0, "Information", "Another tab is closing!" );
deleteBusy = true;
if (tabWidget->count() < 1)
QMessageBox::information(0, "Information", "No tabs!" );
QWidget *tab = tabWidget->currentWidget();
tabWidget->removeTab(tabWidget->currentIndex());
if (tab != NULL)
{
delete tab;
tab = NULL;
}
else
{
QMessageBox::information(0, "Information", "Tab is null!" );
}
deleteBusy = false;
}
To copy to clipboard, switch view to plain text mode
#include "mytab.h"
{
layout->addWidget(tableWidget, 0, 0);
layout->addWidget(tableWidget2, 0, 1);
for (int a = 0; a<1000; a++) // Adding some content to the tab...
{
tableWidget->insertRow(0);
tableWidget2->insertRow(0);
}
}
#include "mytab.h"
myTab::myTab(QWidget *parent) :
QWidget(parent)
{
tableWidget = new QTableWidget();
tableWidget2 = new QTableWidget();
layout = new QGridLayout(this);
layout->addWidget(tableWidget, 0, 0);
layout->addWidget(tableWidget2, 0, 1);
for (int a = 0; a<1000; a++) // Adding some content to the tab...
{
tableWidget->insertRow(0);
tableWidget2->insertRow(0);
}
}
To copy to clipboard, switch view to plain text mode
If I VERY quickly open 3-4 tabs and close them, and then reopen, etc, the program will sometimes crash (about 1 case from 4). In my actual application the problem is more evident. Any ideas why this is happening? Suspecting some kind on heap/stack problem, but as far as I understand it, I can't point the error.
Bookmarks