bug-fix: currentItemChanged() may have NULL pointer

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-10-06 13:30:42 +08:00
parent 9dd22760fc
commit b47c9f3368
3 changed files with 19 additions and 1 deletions

View File

@ -465,6 +465,10 @@ bool VDirectoryTree::isConflictNameWithChildren(const QTreeWidgetItem *parent, c
void VDirectoryTree::currentDirectoryItemChanged(QTreeWidgetItem *currentItem)
{
if (!currentItem) {
emit currentDirectoryChanged(QJsonObject());
return;
}
QJsonObject itemJson = currentItem->data(0, Qt::UserRole).toJsonObject();
Q_ASSERT(!itemJson.isEmpty());
itemJson["root_path"] = treePath;

View File

@ -31,7 +31,11 @@ void VFileList::initActions()
void VFileList::setDirectory(QJsonObject dirJson)
{
Q_ASSERT(!dirJson.isEmpty());
if (dirJson.isEmpty()) {
clearDirectoryInfo();
return;
}
directoryName = dirJson["name"].toString();
rootPath = dirJson["root_path"].toString();
relativePath = QDir(dirJson["relative_path"].toString()).filePath(directoryName);
@ -40,6 +44,11 @@ void VFileList::setDirectory(QJsonObject dirJson)
updateFileList();
}
void VFileList::clearDirectoryInfo()
{
directoryName = rootPath = relativePath = "";
clear();
}
void VFileList::updateFileList()
{
@ -253,6 +262,10 @@ void VFileList::deleteFileAndUpdateList(QListWidgetItem *item)
void VFileList::currentFileItemChanged(QListWidgetItem *currentItem)
{
if (!currentItem) {
emit currentFileChanged(QJsonObject());
return;
}
QJsonObject itemJson = currentItem->data(Qt::UserRole).toJsonObject();
Q_ASSERT(!itemJson.isEmpty());
itemJson["path"] = QDir::cleanPath(QDir(rootPath).filePath(relativePath));

View File

@ -33,6 +33,7 @@ private:
QListWidgetItem *createFileAndUpdateList(const QString &name,
const QString &description);
void deleteFileAndUpdateList(QListWidgetItem *item);
void clearDirectoryInfo();
QString rootPath;
QString relativePath;