[Qt-interest] QTableView doesn't use delegate

Israel Brewster israel at frontierflying.com
Fri Dec 11 18:35:24 CET 2009


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.

>
> Thanks!
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest

-----------------------------------------------
Israel Brewster
Computer Support Technician II
Frontier Flying Service Inc.
5245 Airport Industrial Rd
Fairbanks, AK 99709
(907) 450-7250 x293
-----------------------------------------------

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Israel Brewster.vcf
Type: text/directory
Size: 417 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091211/4631c9fa/attachment.bin 
-------------- next part --------------




More information about the Qt-interest-old mailing list