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
{
horizontalLayout->setSpacing(0);
horizontalLayout->setObjectName("horizontalLayout");
m_pathLineEdit->setObjectName("pathLineEdit");
browseToolButton->setObjectName("browseToolButton");
browseToolButton->setText("...");
horizontalLayout->addWidget(m_pathLineEdit);
horizontalLayout->addWidget(browseToolButton);
this->setLayout(horizontalLayout);
}
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));
}
To copy to clipboard, switch view to plain text mode
And here's the delegate code:
{
}
{
// return CBrowseWidget(parent); THIS DOES NOT WORK!!
return new QPushButton("test", parent
);
// THIS WORKS!! }
{
editor->setGeometry(option.rect);
}
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);
}
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...
Bookmarks