I'm using QFileSystemModel with QTreeView. I want to autosize the columns so they fir the data. Problem is that QFileSystemModel uses a separate thread so right after I create the model the columns aren't filled yet. I want to catch the dataChanged SIGNAL emitted by QAbstractItemModel when the data change and resize the columns there. Thus I do this:
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
#ifndef FILESYSTEMMODELEX_H
#define FILESYSTEMMODELEX_H
#include <QFileSystemModel>
#include <QModelIndex>
#include <QTreeView>
class FileSystemModelEx : public QFileSystemModel {
Q_OBJECT
public:
explicit FileSystemModelEx(QTreeView *view, QObject *parent = 0)
: QFileSystemModel(parent), m_View(view)
{
connect(this, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)), this, SLOT(adaptColumns(const QModelIndex&,const QModelIndex&)));
}
protected slots:
virtual void adaptColumns(const QModelIndex& topleft, const QModelIndex& bottomRight);
protected:
QTreeView* m_View;
};
#endif // FILESYSTEMMODELEX_H
|
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include "FileSystemModelEx.h"
#include <QDebug>
void FileSystemModelEx::adaptColumns(const QModelIndex& topleft, const QModelIndex& bottomRight)
{
qDebug() << "HEEEELO";
int firstColumn= topleft.column();
int lastColumn = bottomRight.column();
// Resize the column to the size of its contents
do {
m_View->resizeColumnToContents(firstColumn);
firstColumn++;
} while (firstColumn < lastColumn);
}
|
My code to create the QTreeView (although irrelevant to my problem) is here:
|
Source code
|
1
2
3
4
5
6
7
|
...
m_FileSystemModel = new FileSystemModelEx(ui->treeViewFileSystem, this);
QModelIndex rootIndex = m_FileSystemModel->setRootPath(p.branch_path().string().c_str());
ui->treeViewFileSystem->setModel(m_FileSystemModel);
ui->treeViewFileSystem->setRootIndex(rootIndex);
...
|
I get no errors but my adaptColumns functin is never called. I've tried every combination of arguments to the function and tried to make it public and private but I simply cannot make the framework call it!
Please what am I doing wrong?