My goal here is to create a signal "expandParentItem" in my QFileSystemModel subclass so that when a parent item is checked in the associated QtreeView, the QtreeView can automatically expand it to show its children.
I define and I emit the signal like this:
FileSysSelectModel.h
#ifndef FILESYSSELECTMODEL_H
#define FILESYSSELECTMODEL_H
#include<QFileSystemModel>
#include <QSet>
#include <QPersistentModelIndex>
#include <QVariant>
class FileSysSelectModel: public QFileSystemModel
{
private:
QSet<QPersistentModelIndex> m_checkTable;
public:
int FileSysSelectModel
::columnCount(const QModelIndex &parent
) const;
virtual Qt
::ItemFlags FileSysSelectModel
::flags(const QModelIndex &index
) const;
virtual bool FileSysSelectModel
::setData(const QModelIndex &index,
const QVariant &value,
int role
);
signals:
};
#endif // FILESYSSELECTMODEL_H
#ifndef FILESYSSELECTMODEL_H
#define FILESYSSELECTMODEL_H
#include<QFileSystemModel>
#include <QSet>
#include <QPersistentModelIndex>
#include <QVariant>
class FileSysSelectModel: public QFileSystemModel
{
private:
QSet<QPersistentModelIndex> m_checkTable;
QVariant m_noData;
void FileSysSelectModel::updateSubMembers(const QModelIndex &index, const QVariant &value);
public:
int FileSysSelectModel::columnCount(const QModelIndex &parent) const;
QVariant FileSysSelectModel::data(const QModelIndex &index, int role) const;
virtual Qt::ItemFlags FileSysSelectModel::flags(const QModelIndex &index) const;
virtual bool FileSysSelectModel::setData(const QModelIndex &index, const QVariant &value, int role);
signals:
void expandParentItem(const QModelIndex &index);
};
#endif // FILESYSSELECTMODEL_H
To copy to clipboard, switch view to plain text mode
Function managing the signal in FileSysSelectModel.cpp
//if the current item has children:
// check all the child items if the parent item is checked.
// uncheck all the child items if the parent item is unchecked.
{
if (firstChild.isValid())
{
if(value == Qt::Checked) emit expandParentItem(index);
int i = 0;
while(1)
{
currentChild = index.child(i,0);
//Recursive call
FileSysSelectModel::updateSubMembers(currentChild, value);
if (!currentChild.isValid()) break;
if(value == Qt::Checked) m_checkTable.insert(currentChild); else m_checkTable.remove(currentChild);
i = i+1;
}
emit dataChanged(firstChild, currentChild);
}
}
//if the current item has children:
// check all the child items if the parent item is checked.
// uncheck all the child items if the parent item is unchecked.
void FileSysSelectModel::updateSubMembers(const QModelIndex &index, const QVariant &value)
{
QModelIndex firstChild = index.child(0,0);
QModelIndex currentChild;
if (firstChild.isValid())
{
if(value == Qt::Checked) emit expandParentItem(index);
int i = 0;
while(1)
{
currentChild = index.child(i,0);
//Recursive call
FileSysSelectModel::updateSubMembers(currentChild, value);
if (!currentChild.isValid()) break;
if(value == Qt::Checked) m_checkTable.insert(currentChild); else m_checkTable.remove(currentChild);
i = i+1;
}
emit dataChanged(firstChild, currentChild);
}
}
To copy to clipboard, switch view to plain text mode
I connect the signal to the QtreeView in my main window like this:
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_fileSystemModel = new FileSysSelectModel;
m_fileSystemModel
->setRootPath
(QDir::currentPath());
ui->scriptFileView->setModel(m_fileSystemModel);
}
MainWindow::~MainWindow()
{
delete ui;
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_fileSystemModel = new FileSysSelectModel;
m_fileSystemModel->setRootPath(QDir::currentPath());
ui->scriptFileView->setModel(m_fileSystemModel);
QObject::connect(m_fileSystemModel,SIGNAL(expandParentItem(QModelIndex)),ui->scriptFileView,SLOT(expand(QModelIndex)));
}
MainWindow::~MainWindow()
{
delete ui;
}
To copy to clipboard, switch view to plain text mode
When I try to compile, I get the following error message:
FileSysSelectModel.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall FileSysSelectModel::expandParentItem(class QModelIndex const &)" (?expandParentItem@FileSysSelectModel@@QAEXABVQMod elIndex@@@Z) referenced in function "private: void __thiscall FileSysSelectModel::updateSubMembers(class QModelIndex const &,class QVariant const &)" (?updateSubMembers@FileSysSelectModel@@AAEXABVQMod elIndex@@ABVQVariant@@@Z)
debug\Designer_test.exe:-1: error: LNK1120: 1 unresolved externals
I'm new to Qt and I'm having a hard time understanding this error message.
Could someone help me with this issue?
Bookmarks