Mauricio
Mauricio

Reputation: 742

Empty lines in QStandardItemModel with custom data

The header is ok, but I only have three empty lines.

The method FileConfig::data is never called!
Have you any idea why?

model= new MyModel;

model->setHeaderData(0, Qt::Horizontal, tr("Title"));
model->setHeaderData(1, Qt::Horizontal, tr("Direcory"));
model->setHeaderData(2, Qt::Horizontal, tr("Date"));

model->invisibleRootItem()->setChild(0, new FileConfig("/home/user/dir/riri.conf"));
model->invisibleRootItem()->setChild(1, new FileConfig("/home/user/dir/fifi.conf"));
model->invisibleRootItem()->setChild(2, new FileConfig("/home/user/dir/loulou.conf"));

proxy= new QSortFilterProxyModel(this);
proxy->setSourceModel(model);

view= new QTreeView;
view->setModel(proxy);

MyModel inherite from QStandardItemModel, empty for the moment.
My custom class:

class FileConfig : public QStandardItem {
    public:
        std::string getFileName() const;
        std::string getFileDirectory() const;
        std::string getDate() const;

        FileConfig(const char *fileconfig);

        virtual QStandardItem *clone() const;
        virtual QVariant data(const QModelIndex &index, int role= Qt::DisplayRole) const;

    private:
        boost::filesystem::path file;
};

FileConfig::FileConfig(const char *fileconfig) : QStandardItem() {
    file= boost::filesystem::path(fileconfig);
}

QVariant FileConfig::data(const QModelIndex &index, int role) const {
    if(role == Qt::DisplayRole)
        switch(index.column()) {
            case 0: return getFileName().c_str();
            case 1: return getFileDirectory().c_str();
            case 2: return getDate().c_str(); }
    return QVariant();
}

Upvotes: 0

Views: 519

Answers (1)

Carlton
Carlton

Reputation: 4297

Try using appendRow instead of setChild when you add new items to the model. For example:

model->invisibleRootItem()->appendRow(new FileConfig(/*path*/));

Edit: you also need the Q_OBJECT macro in your class definition of FileConfig, otherwise signals and slots will not work. Remember to re-run QMake after you add the macro.

Upvotes: 1

Related Questions