Results 1 to 6 of 6

Thread: Delegate's editor won't launch in a custom inserted column

  1. #1
    Join Date
    May 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Delegate's editor won't launch in a custom inserted column

    Hola,
    spent whole night trying to figure out this problem. No luck.

    I'm using a QSqlRelationalTableModel-derived class to handle a database table with a QTableView. However, I have to insert one custom column to the model for displaying additional info.

    In app's constructor:

    Qt Code:
    1. static CMyTableModel model; // Derived from QSqlRelationalTableModel
    2. model.setTable("BookTable"); // 2 fields: ID and Name
    3. model.select(); // Populate from database
    4. model.insertColumn(2); // Insert column for additional data
    To copy to clipboard, switch view to plain text mode 

    I set up the QTableView:

    Qt Code:
    1. QTableView* tv = ui.tableview;
    2. tv->setModel(&model); // Set model
    3. tv->hideColumn(0); // Hide ID
    4. tv->setItemDelegateForColumn(2, new CMyDelegate()); // Set our delegate to custom column
    5. tv->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
    To copy to clipboard, switch view to plain text mode 

    I have overriden data to handle the additional column:

    Qt Code:
    1. QVariant CMyTableModel::data(const QModelIndex &item, int role) const
    2. {
    3. if(item.column() == 2) // Our column
    4. {
    5. if(role == Qt::DisplayRole)
    6. {
    7. return QString(...);
    8. }
    9.  
    10. if(role == Qt::EditRole)
    11. {
    12. return QString(...);
    13. }
    14. }
    15.  
    16. // Default
    17. return QSqlRelationalTableModel::data(item, role);
    18. }
    To copy to clipboard, switch view to plain text mode 

    And here's the delegate's most important methods:

    Qt Code:
    1. void CMyDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    2. {
    3. // !!! PAINTS JUST FINE
    4. }
    5.  
    6. QWidget * CMyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    7. {
    8. return new QPushButton("test"); // !!! NEVER CALLED
    9. }
    To copy to clipboard, switch view to plain text mode 

    Whatever I try, I just can't get the inserted column to enter edit mode. What's missing? Would REALLY appreciate some help here. Thanks.

  2. #2
    Join Date
    Jun 2009
    Posts
    37
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delegate's editor won't launch in a custom inserted column

    Did you try with QLineEdit???

    Qt Code:
    1. QWidget * CMyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    2. {
    3. QVariant value = index.data(Qt::EditRole);
    4. QLineEdit *lineEdit = new QLineEdit(parent);
    5. lineEdit->setText(value.toString());
    6. return lineEdit;
    7. }
    To copy to clipboard, switch view to plain text mode 

    And also... the additional Column should be also in your underlying model!!!

  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Delegate's editor won't launch in a custom inserted column

    If it is never called this means that your model is not editable! Do you return proper flags for: QAbstractItemModel::flags?


    Added after 9 minutes:


    Quote Originally Posted by JoZCaVaLLo View Post
    Did you try with QLineEdit???

    Qt Code:
    1. QWidget * CMyDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    2. {
    3. QVariant value = index.data(Qt::EditRole);
    4. QLineEdit *lineEdit = new QLineEdit(parent);
    5. lineEdit->setText(value.toString());
    6. return lineEdit;
    7. }
    To copy to clipboard, switch view to plain text mode 

    And also... the additional Column should be also in your underlying model!!!
    Take a look on documentation! There are other virtual methods designed to set value of editing widget and setting value for data model.
    See QAbstractItemDelegate::setEditorData and QAbstractItemDelegate::setModelData.
    Last edited by MarekR22; 13th May 2011 at 11:21.

  4. The following user says thank you to MarekR22 for this useful post:

    zeroknowledge (13th May 2011)

  5. #4
    Join Date
    Jun 2009
    Posts
    37
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delegate's editor won't launch in a custom inserted column

    I don't think so: he has problem on createEditor... not the other functions...

    I now saw that you used
    Qt Code:
    1. tv->setItemDelegateForColumn(2, new CMyDelegate()); // Set our delegate to custom column
    To copy to clipboard, switch view to plain text mode 

    but if you have the additional column on the model, why don't you use the standard delegate???
    According to doc you have to beware on which delegate will be used:
    Note: If a delegate has been assigned to both a row and a column, the row delegate will take precedence and manage the intersecting cell index.
    for simplicity I would use the standard itemDelegate
    Qt Code:
    1. tv->setItemDelegate(new CMyDelegate());
    2.  
    3. QWidget* CMyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    4. {
    5. if(index.column() == 2) // Our column
    6. {
    7. return new QPushButton("test");
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Delegate's editor won't launch in a custom inserted column

    Does "paints just fine" mean the code in paint() is executed or paint() is empty and regardless of that the content gets drawn?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #6
    Join Date
    May 2011
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Delegate's editor won't launch in a custom inserted column

    Quote Originally Posted by MarekR22 View Post
    If it is never called this means that your model is not editable! Do you return proper flags for: QAbstractItemModel::flags?
    You made my day! Thanks a lot.

    I read the docs several times but somehow managed to completely miss flags(). I've already implemented setEditorData and setModelData (just omitted them from my code). I'll try to be move observant while perusing the docs next time.

Similar Threads

  1. Replies: 1
    Last Post: 10th May 2011, 22:35
  2. How to fill an inserted column with data?
    By croo in forum Qt Programming
    Replies: 3
    Last Post: 12th December 2010, 19:58
  3. Replies: 1
    Last Post: 29th April 2010, 05:21
  4. Replies: 1
    Last Post: 28th October 2009, 18:42
  5. Replies: 5
    Last Post: 10th August 2009, 10:50

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.