You are not logged in.

1

Sunday, October 31st 2010, 5:34am

connect()'ing to the "dataChanged SIGNAL of QAbstractItemModel...

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?

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Sunday, October 31st 2010, 1:35pm

I would not overlook the QTreeView to help out here. There is a resizePolicy that can be set to automatically resize the columns to content, take a look at QHeaderView setResizeModesettings on the view itself. This could be set in the designer your using as well on the QTreeView properties.

3

Sunday, October 31st 2010, 6:40pm

I'll ofcourse take a look at it but still. I don't understand why I cannot connect my code to that signal. It's supposed to work.

4

Sunday, October 31st 2010, 6:45pm

Thank you. Setting the resize mode does indeed do what I want without me having to intercept any signals. Still I'd like to know why my code isn't working. Obviously I don't understand the SIGNAL/SLOT mechanism...

5

Monday, November 1st 2010, 1:38pm

And also, I need to intercept this signal for other purposes too. I simply cannot be the first to want to catch the dataChanged signal. How do other people do this?

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

6

Monday, November 1st 2010, 2:17pm

Unfortunately I'm at work and do not have the build environment to test this out, but could you try something like:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
...

m_FileSystemMode l= new FileSystemModelEx(ui->treeViewFileSystem, this);

// test
connect( m_FileSystemModel, SIGNAL(dataChanged(const QModelIndex&,const QModelIndex&)),
      m_FileSystemModel, SLOT(adaptColumns(const QModelIndex&,const QModelIndex&)));

QModelIndex rootIndex = m_FileSystemModel->setRootPath(p.branch_path().string().c_str());
ui->treeViewFileSystem->setModel(m_FileSystemModel);
ui->treeViewFileSystem->setRootIndex(rootIndex);
...


Just on a hunch that the signal is not being emitted because the FileSystemModel in how it has adapted ownership of it somehow because it being inherited from QAbstractItemModel. -- Just a thought --

7

Wednesday, November 3rd 2010, 3:02am

I fail to see how this is different from what I'm already dong. You just moved the connect call out of the QFileSystemModel derivative and into the MainWindow class. I went ahead and tried anyway but still no luck.