Frage

I have very simple, custom model derived from QStandardItemModel. Functions rowCount and columnCount are called several times whereas none of the remaining functions, in particular data() is never called. I cannot find the reason.

#include <IBAdditions/IB_events.h>

#include <QtGui/QStandardItemModel>
#include <QtGui/QStandardItem>
#include <QtCore/QtCore>

#include <vector>

/**
 * model which allows for display 
 * of available data streams as tickers
 */
class TickerDisplayModel : public QStandardItemModel {

Q_OBJECT
 public:
     TickerDisplayModel(std::vector<IBAdditions::ContractEvent> availableTickers, QObject *parent = 0);
     int rowCount(const QModelIndex &parent = QModelIndex()) const ;
     int columnCount(const QModelIndex &parent = QModelIndex()) const;
     QVariant headerData(int section, Qt::Orientation orientation, int role) const;
     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
     bool setData(const QModelIndex & index, const QVariant & value, int role = Qt::EditRole);
     Qt::ItemFlags flags(const QModelIndex & index) const;
 private:
     int m_;
     int n_;
     std::vector<IBAdditions::ContractEvent>& availableTickers_;
 signals:
     void editCompleted(const QString &);
};

how this is called:

SubscribeToDataGUI::SubscribeToDataGUI(QWidget* parent) : QDialog(parent) {
    widget_.setupUi(this);
    IBAdditions::ContractEvent c;
    c.symbol = "EUR"; c.currency = "USD"; c.event_ = IBAdditions::TickPrice;
    availableTickers_.push_back(c);
    model_.reset(new TickerDisplayModel(availableTickers_));
    widget_.listView->setModel(model_.get());
    widget_.listView->show();
}
War es hilfreich?

Lösung

I think it is not possible to subclass data in QStandardItemModel.

I just try change my own QAbstractItemModel to QStandardItemModel and data is not called either.

Try to use QAbstractItemModel. I think you have implemented almost everything you need to use QAbstractItemModel

You need to implement index and parent. Since you have no hiearchical model, parent should always return invalid index.

It can be implemented like this:

    QModelIndex TickerDisplayModel::index(int row, int column, const QModelIndex &parent) const
    {
       return hasIndex(row, column, parent) ? createIndex(row, column, 0) : QModelIndex();
    }

    QModelIndex TickerDisplayModel::parent(const QModelIndex &child) const
    {
        return QModelIndex(); // since no hiearchy model, this should always return invalid index
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top