[Qt-interest] QTableView doesn't use delegate
Timothy Reaves
treaves at silverfieldstech.com
Fri Dec 11 19:35:00 CET 2009
On Dec 11, 2009, at 12:35 PM, Israel Brewster wrote:
>
> On Dec 11, 2009, at 6:41 AM, Timothy Reaves wrote:
>
>> I have a QTableView, a QSqlTableModel, and an QItemDelegate subclass. I create my model, and set that on the table view. I create my delegate, and set that on the table view.
>>
>> QSqlTableModel *tableModel = new QSqlTableModel(0, db);
>> tableModel->setTable("data_table");
>> tableModel->select();
>> ui->tableView->setModel(tableModel);
>> ui->tableView->setItemDelegate(new MyTableDelegate(ui->tableView));
>>
>>
>> When my window display, the table does display the data from the SQL table. However, not a single method in my delegate is called. I implement
>> QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const;
>> void setEditorData(QWidget *editor, const QModelIndex &index) const;
>> void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
>>
>> and have a log statement as the first line in each method. None gets hit.
>>
>> Is there something about the combination of thee three classes that make them incompatible? Am I missing something?
>
> Nothing obvious to me- I have the almost exact same combination of components in a number of places across several of my programs. I never explicitly put the parent view in the instantiation of my delegate, and I also don't typically create the delegate inside the call to setItemDelegate, but I wouldn't think either of these things should cause problems. How exactly is your delegate declared? Here's an example from one of my programs, if it helps:
>
> class priceDelegate: public QItemDelegate
> {
> Q_OBJECT
> public:
> void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
> private:
> QWidget *createEditor( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;
> };
>
> And it is used as such:
>
> purchaseTable->setItemDelegateForColumn(dataModel->fieldIndex("total_price"),&moneyDelegate);
>
> where purchaseTable is a QTableView (actually, a subclass thereof, but same diff) from my .ui file, and moneyDelegate is the priceDelegate, declared as an object (not a pointer) in my class. In another place I have fillDelegate declared as a member pointer in my class, which I then instantiate and use at runtime as so:
>
> fillDelegate=new autoFillDelegate(this);
> purchaseTable->setItemDelegateForColumn(dataModel->fieldIndex("item"),fillDelegate);
>
> I'm not sure why I have it different - perhaps I need to go back and look at my code more closely to decide which I really want to do. I also don't know if there is any reason that I declared the objects/pointer as class members rather than just calling new in the function and passing it to the table, or if it makes any difference. This code is a bit old :-). Hope this helps. Good luck.
>
Well, it looks like the main differences are I call setItemDelegate(), not setItemDelegateForColumn(). I also don't implement updateEditorGeometry(). I'll play around with your code as see what I get.
More information about the Qt-interest-old
mailing list