PDA

View Full Version : Custom widget won't size properly in a table cell



zeroknowledge
11th May 2011, 00:49
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


CBrowseWidget::CBrowseWidget(QWidget* parent) : QWidget(parent)
{
QHBoxLayout* horizontalLayout = new QHBoxLayout();
horizontalLayout->setSpacing(0);
horizontalLayout->setObjectName("horizontalLayout");

m_pathLineEdit = new QLineEdit(this);
m_pathLineEdit->setObjectName("pathLineEdit");

QToolButton* browseToolButton = new QToolButton(this);
browseToolButton->setObjectName("browseToolButton");
browseToolButton->setText("...");

horizontalLayout->addWidget(m_pathLineEdit);
horizontalLayout->addWidget(browseToolButton);

this->setLayout(horizontalLayout);
this->setSizePolicy(QSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred));
}


And here's the delegate code:


CPathItemDelegate::CPathItemDelegate() : QItemDelegate()
{
}

QWidget * CPathItemDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
// return CBrowseWidget(parent); THIS DOES NOT WORK!!
return new QPushButton("test", parent); // THIS WORKS!!
}

void CPathItemDelegate::updateEditorGeometry ( QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
editor->setGeometry(option.rect);
}


Any ideas?

Added after 12 minutes:

Answering to myself: horizontalLayout->setContentsMargins(0, 0, 0, 0); did the trick... :rolleyes:

high_flyer
11th May 2011, 17:32
try setting the margins:
setContentsMargins()