Can't set QComboBox/QSlider margins
-
I'm creating a property editor with content placed in a QTreeView and I want my editors to have padding from the edge of the widget. I also use a delegate to create the editors dynamically. I used editorPtr->setContentsMargins and in case of QLineEdit it works as expected. But other editors seem to ignore the margins. How to properly set margins for these widgets?
-
@bibasmall put the layout on a dummy container widget e.g.
QWidget* container = new QWidget(parent); QHBoxLayout* pLayout = new QHBoxLayout(container); QLineEdit* pLineEdit = new QLineEdit(); pLayout->addWidget(pLineEdit); pLayout->setContentsMargins(0, 0, 5, 0); pEditor = container;
-
Hi
so editorPtr is pointing to a layout ?All widgets should honor margins in its layout
Can you show the CreateEditor code for your Delegate ?
-
Hello. No, editorPtr is pointing to the editor. Here is my createEditor code:
QWidget* CEditorDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex& index) const
{auto editorType = CPropertiesTreeModel::GetEditorType(index); QWidget* pEditor = nullptr; if (editorType == EditorType::LineEdit) { QLineEdit* pLineEdit = new QLineEdit(parent); //LineEdit is filled with data here pEditor = pTextEditor; } else if (editorType == EditorType::Combobox) { QComboBox* pQComboBox = new QComboBox(parent); //ComboBox is filled with data here pEditor = pQComboBox; } else if (editorType == EditorType::Slider) { auto optData = CPropertiesTreeModel::GetIndexPossibleValues(index); if (optData) { auto optEType = CPropertiesTreeModel::GetEditorValueType(index); if (optEType == EType::EInt) { CExtendedSlider* pExtSlider = new CExtendedSlider(Qt::Horizontal, parent); //ExtSlider is filled with data here pEditor = pExtSlider; } else if (optEType == EType::EFloat) { CFloatSlider* pFloatSlider = new CFloatSlider(Qt::Horizontal, parent); //FloatSlider is filled with data here pEditor = pFloatSlider; } } } if (pEditor) pEditor->setContentsMargins(0, 0, 5, 0); return pEditor;
}
-
@bibasmall
Hi
Ahh, QWidgets has setContentsMargins.Seems Only LineEdit cares about it in its drawing/handling.
So Slider and Combo do not honor/use it.I think you have to use a layout and its margins to make it work for all types of widgets.
-
Could you please tell me how should I add the layout in my code?
If I write:QHBoxLayout* layout = new QHBoxLayout(parent); layout->addWidget(pEditor); layout->setContentsMargins(0, 0, 5, 0);
The layout will obviously not be placed in the corresponding cell, nor be deleted with the editor. If I change the layout's parent to pEditor, this code does nothing — even QLineEdit ignores margins in this case.
-
@bibasmall put the layout on a dummy container widget e.g.
QWidget* container = new QWidget(parent); QHBoxLayout* pLayout = new QHBoxLayout(container); QLineEdit* pLineEdit = new QLineEdit(); pLayout->addWidget(pLineEdit); pLayout->setContentsMargins(0, 0, 5, 0); pEditor = container;
-
@Chris-Kawa thank you, it helped to set the margins!