Results 1 to 2 of 2

Thread: Custom widget won't size properly in a table cell

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

    Default Re: Custom widget won't size properly in a table cell

    Hello.

    I'm trying to use a custom widget as an editor in a QTableView's cell, but for some very obscure reason (to me), the custom widget won't size properly. However, when I replace my custom widget with a simple QPushButton, the component is now sized as it should, filling the whole cell. See the attachments.

    Here's the important code:

    This is how I create my custom widget
    Qt Code:
    1. CBrowseWidget::CBrowseWidget(QWidget* parent) : QWidget(parent)
    2. {
    3. QHBoxLayout* horizontalLayout = new QHBoxLayout();
    4. horizontalLayout->setSpacing(0);
    5. horizontalLayout->setObjectName("horizontalLayout");
    6.  
    7. m_pathLineEdit = new QLineEdit(this);
    8. m_pathLineEdit->setObjectName("pathLineEdit");
    9.  
    10. QToolButton* browseToolButton = new QToolButton(this);
    11. browseToolButton->setObjectName("browseToolButton");
    12. browseToolButton->setText("...");
    13.  
    14. horizontalLayout->addWidget(m_pathLineEdit);
    15. horizontalLayout->addWidget(browseToolButton);
    16.  
    17. this->setLayout(horizontalLayout);
    18. this->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
    19. }
    To copy to clipboard, switch view to plain text mode 

    And here's the delegate code:
    Qt Code:
    1. CPathItemDelegate::CPathItemDelegate() : QItemDelegate()
    2. {
    3. }
    4.  
    5. QWidget * CPathItemDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    6. {
    7. // return CBrowseWidget(parent); THIS DOES NOT WORK!!
    8. return new QPushButton("test", parent); // THIS WORKS!!
    9. }
    10.  
    11. void CPathItemDelegate::updateEditorGeometry ( QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    12. {
    13. editor->setGeometry(option.rect);
    14. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas?


    Added after 12 minutes:


    Answering to myself: horizontalLayout->setContentsMargins(0, 0, 0, 0); did the trick...
    Attached Images Attached Images
    Last edited by zeroknowledge; 10th May 2011 at 23:49.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,716
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Custom widget won't size properly in a table cell

    try setting the margins:
    setContentsMargins()
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 3
    Last Post: 5th April 2010, 21:20
  2. Replies: 0
    Last Post: 2nd October 2009, 17:57
  3. Replies: 4
    Last Post: 4th February 2008, 06:16
  4. Simple custom widget won't size properly
    By MrGarbage in forum Qt Tools
    Replies: 2
    Last Post: 9th August 2007, 13:12
  5. Replies: 11
    Last Post: 7th September 2006, 23:15

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.