Results 1 to 6 of 6

Thread: How to center an item editor created by a custom item delegate?

  1. #1
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default How to center an item editor created by a custom item delegate?

    As the title says... i have a qitemdelgate which creates a QCheckBox as editor for some items. how can i center this editor in the cell of the qtableview where the editor shows up.

    here`s the method i wrote so far

    Qt Code:
    1. QWidget* QmitkDataStorageDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option
    2. , const QModelIndex &index) const
    3. {
    4. if (index.column() == m_VisibilityColumnIndex)
    5. {
    6. //option.decorationAlignment = Qt::AlignCenter;
    7. //option.decorationAlignment = Qt::AlignCenter;
    8.  
    9. QCheckBox* visibilityCheckBox = new QCheckBox(parent);
    10. connect(visibilityCheckBox, SIGNAL(editingFinished()),
    11. this, SLOT(commitAndCloseEditor()));
    12. return visibilityCheckBox;
    13. }
    14. else
    15. {
    16. return QItemDelegate::createEditor(parent, option, index);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to center an item editor created by a custom item delegate?

    Have a look at QStyledItemDelegate::updateEditorGeometry or QItemDelegate::updateEditorGeometry based on what u are using.

    You can place your check box based on the rect u get in option.rect

  3. #3
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to center an item editor created by a custom item delegate?

    i tried to change updateEditorGeometry().
    Qt Code:
    1. void MyDelegate::updateEditorGeometry( QWidget * editor, const QStyleOptionViewItem option, const QModelIndex & index ) const
    2. {
    3. QRect rect = option.rect;
    4.  
    5. if (index.column() == m_VisibilityColumnIndex)
    6. {
    7. rect.setX(rect.x() + 10);
    8. }
    9.  
    10. editor->setGeometry(rect);
    11. }
    To copy to clipboard, switch view to plain text mode 
    but obviously the method never gets called (i set a breakpoint there). so what can i do?

  4. #4
    Join Date
    Mar 2009
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to center an item editor created by a custom item delegate?

    btw: in my code example i just tried to move the checkbox 10 pixels to the right (instead of centering it)-> just for demonstration

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to center an item editor created by a custom item delegate?

    Does ur createEditor function gets called ?
    If not check if the item is set as editable or not. updateEditorGeometry must be called if the editor is created...

  6. #6
    Join Date
    Aug 2009
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: How to center an item editor created by a custom item delegate?

    The only way i found is to set a QMyStyle to the QTableView

    m_pMyTableView->setStyle(new QMyStyle());

    In QMyStyle you should overload subElementRect(...) Method as follow :

    QRect QMyStyle::subElementRect(SubElement sr, const QStyleOption *opt, const QWidget *widget) const
    {

    //die CheckBox mittig ausrichten
    if (sr == QStyle::SE_ItemViewItemCheckIndicator || sr == QStyle::SE_CheckBoxIndicator)
    {
    QRect r = QWindowsVistaStyle::subElementRect(sr, opt, widget);
    int center = opt->rect.center().x();
    return QRect(QPoint(center - r.width() / 2, r.y()), QPoint(center + r.width() / 2, r.bottom()));
    }

    return QWindowsVistaStyle::subElementRect(sr, opt, widget);
    }


    regards,
    Max

Similar Threads

  1. Item Delegate Painting
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2008, 07:37
  2. QMovie from inside a custom item delegate
    By Crazy_Hopper in forum Qt Programming
    Replies: 3
    Last Post: 7th May 2008, 15:18

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.