相同模型代理视图连接但在第二个项目中不起作用

时间:2011-11-28 11:39:13

标签: qt

这有效:

/*Just copy and paste*/  
#include <QApplication>
#include <QtGui>
#include <QDebug>
#include <QAbstractProxyModel>

class File_List_Proxy : public QAbstractProxyModel
{
public:
    virtual QModelIndex     mapFromSource ( const QModelIndex & sourceIndex ) const
    {
        return sourceModel()->index(sourceIndex.row(),sourceIndex.column());
    }
    virtual QModelIndex     mapToSource ( const QModelIndex & proxyIndex ) const
    {
        return sourceModel()->index(proxyIndex.row(),proxyIndex.column());
    }
    virtual QModelIndex index(int row, int column, const QModelIndex&) const

    {
        return createIndex(row,column);
    }
    virtual QModelIndex parent(const QModelIndex&) const
    {
        return QModelIndex();
    }
    virtual int rowCount(const QModelIndex&) const
    {
        return sourceModel()->rowCount();
    }
    virtual int columnCount(const QModelIndex&) const
    {
        return sourceModel()->columnCount();
    }
};

class File_List_Model : public QAbstractItemModel
{
private:
    QStringList data_;
public:

    File_List_Model(/*const QStringList& value*/)//:QStringListModel(value)
    {
    }
    QVariant data(const QModelIndex &index, int role) const
    {
        if (role == Qt::DisplayRole){
        /*QVariant t = data_.at(index.row());
        qDebug() << "index.row(): " << index.row();
        qDebug() << "data_.at(index.row()): " << data_.at(index.row());*/
        return data_.at(index.row());
        }
        else
        {
            return QVariant();
        }
    }

    bool set_entries(const QStringList& entries)
    {
        beginInsertRows(createIndex(0,0),0,entries.count());
        data_ = entries;
        endInsertRows();
        emit dataChanged(createIndex(0,0),createIndex(0,entries.count()));
        return true;
    }

    bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole)
    {
        switch(role)
        {
        case Qt::DisplayRole:
        data_ =  value.toStringList();
        emit dataChanged(index,index);
        return true;
        }
        return false;
    }

    virtual QModelIndex index(int row, int column, const QModelIndex&) const
    {
        return createIndex(row,column);
    }
    virtual QModelIndex parent(const QModelIndex&) const
    {
        return QModelIndex();
    }
    virtual int rowCount(const QModelIndex&) const
    {
        return data_.size();
    }
    virtual int columnCount(const QModelIndex&) const
    {
        return 1;
    }
};


int main(int argc,char** argv)
{
    QApplication app(argc,argv);
    QDir dir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
    File_List_Model* model = new File_List_Model;//(dir.entryList());
   bool t =  model->set_entries(dir.entryList());
   File_List_Proxy* proxy = new File_List_Proxy;
   proxy->setSourceModel(model);
    QListView* view = new QListView;
    view->setModel(proxy);
    //new ModelTest(model);
    view->show();

    return app.exec();
}
/*End of copy*/

相反,从上面的代码中File_List_Model和File_List_Proxy被复制且未更改的不同项目不起作用:

Line_Counter::Line_Counter(QWidget *parent) :
    QDialog(parent), model_(new File_List_Model),
    proxy_model_(new File_List_Proxy)
{
    setupUi(this);

    setup_mvc_();

    QDir dir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
    File_List_Model* model = new File_List_Model;//(dir.entryList());
   bool t =  model->set_entries(dir.entryList());

}

void Line_Counter::setup_mvc_()
{
    proxy_model_->setSourceModel(model_);


    listView->setModel(proxy_model_);

}

//Line counter  
class Line_Counter : public QDialog, private Ui::Line_Counter
{
    Q_OBJECT
private:
    File_List_Model* model_;
    //QStringListModel* model_;
    File_List_Proxy* proxy_model_;
};

这里发生了什么?!

1 个答案:

答案 0 :(得分:1)

在模型创建之前调用setup_mvc_。在这种情况下,model_是默认构造的模型,其中set_entries尚未被调用。另一方面,您调用未设置为视图的set_entries上的model

这将有效:

Line_Counter::Line_Counter(QWidget *parent) :
    QDialog(parent), model_(new File_List_Model),
    proxy_model_(new File_List_Proxy)
{
    setupUi(this);

    QDir dir(QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
    bool t =  model_->set_entries(dir.entryList());

    setup_mvc_();
}