Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is MyModel::data() not being called (subclassing QSqlQueryModel)

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!

like image 316
L Co Avatar asked Dec 13 '25 10:12

L Co


1 Answers

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

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

like image 83
Mike Avatar answered Dec 16 '25 11:12

Mike



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!