Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't set QComboBox/QSlider margins
Forum Updated to NodeBB v4.3 + New Features

Can't set QComboBox/QSlider margins

Scheduled Pinned Locked Moved Solved General and Desktop
c++ qtmargins
7 Posts 3 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • bibasmallB Offline
    bibasmallB Offline
    bibasmall
    wrote on last edited by bibasmall
    #1

    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?
    a98ffce0-20bf-4a5c-a7db-230ec9d642c4-image.png

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #6

      @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;
      
      1 Reply Last reply
      2
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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 ?

        1 Reply Last reply
        0
        • bibasmallB Offline
          bibasmallB Offline
          bibasmall
          wrote on last edited by bibasmall
          #3

          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;
          

          }

          mrjjM 1 Reply Last reply
          0
          • bibasmallB bibasmall

            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;
            

            }

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @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.

            1 Reply Last reply
            1
            • bibasmallB Offline
              bibasmallB Offline
              bibasmall
              wrote on last edited by bibasmall
              #5

              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.

              1 Reply Last reply
              0
              • Chris KawaC Offline
                Chris KawaC Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @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;
                
                1 Reply Last reply
                2
                • bibasmallB Offline
                  bibasmallB Offline
                  bibasmall
                  wrote on last edited by
                  #7

                  @Chris-Kawa thank you, it helped to set the margins!

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved