質問

Hi I can't figure out why my data() function is never called when populating QTableView

I subclassed QSqlQueryModel. The header is like:

class TicketModel : public QSqlQueryModel
{
    Q_OBJECT
public:
    explicit TicketModel(QObject *parent = 0);

    QVariant data(const QModelIndex &index, int role);
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;

};

In the main window I set my model to the table

TicketModel *model = new TicketModel();
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(model);

QSqlQuery *query = _tf->search(1); 
model->setQuery(*query); 

_ui->dashTable->setModel(proxyModel);    // <<<<<<<<<<<<<< I setting model here too, didn't work
_ui->dashTable->setSortingEnabled(true);              
_ui->dashTable->horizontalHeader()->setVisible(true);  
_ui->dashTable->setSelectionBehavior(QAbstractItemView::SelectRows);

The TicketModel::headerData(...) is called but TicketModel::data(...) is never called when the table is created. Why? How can I get it to be called?

I hope I just overlooked something simple but I have been trying for a few hours to figure it out.

Thanks for the help!

役に立ちましたか?

解決

You've got the signature wrong. You need a const.

QVariant data( const QModelIndex & index, int role = Qt::DisplayRole ) const

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top