Results 1 to 3 of 3

Thread: delegate isn't work with QSqlTableModel?

  1. #1
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default delegate isn't work with QSqlTableModel?

    Hi,

    I have:
    Qt Code:
    1. model->setTable( "table" );
    2. model->select();
    3. model->setHeaderData( 0, Qt::Horizontal, QObject::tr("Annual Pay") );
    4. model->setHeaderData( 1, Qt::Horizontal, QObject::tr("First Name") );
    5. model->setHeaderData( 2, Qt::Horizontal, QObject::tr("Last Name") );
    6. model->removeColumn( 0 );
    7. TableResult->setModel( model );
    8. BarDelegate delegate;
    9. TableResult->setItemDelegateForColumn(1, &delegate);
    10. TableResult->show();
    To copy to clipboard, switch view to plain text mode 

    and in Delegate class i've:

    Qt Code:
    1. class BarDelegate : public QAbstractItemDelegate
    2. {
    3. public:
    4. BarDelegate( QObject *parent = 0 );
    5.  
    6. void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
    7. QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
    8.  
    9. QWidget *createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
    10. void setEditorData( QWidget *editor, const QModelIndex &index ) const;
    11. void setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const;
    12. void updateEditorGeometry( QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
    13. };
    To copy to clipboard, switch view to plain text mode 

    In createEditor:
    Qt Code:
    1. QWidget *BarDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    2. {
    3. QMessageBox::information( 0, "window1", "test" );
    4. QLineEdit *lineEdit = new QLineEdit( parent );
    5. lineEdit->setAutoFillBackground( true );
    6. lineEdit->installEventFilter( const_cast<BarDelegate*>(this) );
    7. return lineEdit;
    8. }
    To copy to clipboard, switch view to plain text mode 

    When I press on a cell TableResult, there is no QMessageBox :/

    Similar with:
    Qt Code:
    1. void BarDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
    2. {
    3. QMessageBox::information( 0, "window1", "test2" );
    4. QString value = index.model()->data( index, Qt::DisplayRole ).toString();
    5. static_cast<QLineEdit*>( editor )->setText( value );
    6. }
    To copy to clipboard, switch view to plain text mode 
    and :/
    Qt Code:
    1. void BarDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
    2. {
    3. QMessageBox::information( 0, "window1", "test3" );
    4. model->setData( index, static_cast<QLineEdit*>( editor )->text() );
    5. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: delegate isn't work with QSqlTableModel?

    You cannot instantiate the delegate on the stack. You must instantiate it on the heap instead:

    Qt Code:
    1. BarDelegate *delegate = new BarDelegate(this);
    2. TableResult->setItemDelegateForColumn(1, delegate);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to victor.fernandez for this useful post:

    TomASS (3rd August 2009)

  4. #3
    Join Date
    Apr 2009
    Posts
    75
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: delegate isn't work with QSqlTableModel?

    Thanks! Work!

Similar Threads

  1. QSqlTableModel, QTableView and complex queries
    By Walter in forum Qt Programming
    Replies: 8
    Last Post: 15th November 2009, 02:20
  2. QTreeView Delegate
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 23rd June 2009, 10:10
  3. Problems with allocating QSqlTableModel on heap
    By kosa in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2009, 18:56
  4. Delegate for a certain item?
    By somebody in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 23:55
  5. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 02:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.