Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QComboBox, why the gap in the border?



  • I'm working on an application that will take the properties and configuration for each widget from an XML file. I have something odd going on with a QComboBox, in my XML file I have:

    <combo id="cbo1" x="150" y="10" width="80">
         <item text="Item 1" value="123"/>
         <item text="Item 2" value="456"/>
         <item text="Item 3" value="789"/>
    </combo>
    

    The window properties are controlled by another XML definition, here is a screenshot of how this appears:
    0_1542912260500_Screen Shot 2018-11-22 at 18.36.43.png
    I don't understand why the border at the bottom of the combo has a section missing, I've tried just about everything I can think of but it still remains missing.

    The geometry for this widget is:

        x1: 150, y1:10, x2:229, y2:39
    

    There are no additional flags being specified for this widget...any ideas?

    Here is the code that creates the combo box:

            QComboBox* pcboNew = new QComboBox(pobjWin);
            lstOfNodes lstComboChildNodes = pobjChild->lstGetChildNodes();
    
            foreach( clsXMLnode* pobjCboChild, lstComboChildNodes ) {
                QString strCboChildName = pobjCboChild->strGetNodeName();
    
                if ( strCboChildName.compare(clsXMLnode::mscszNodeItem) == 0 ) {
                    QString strText = pobjCboChild->strGetAttr(clsXMLnode::mscszAttrText)
                           ,strValue = pobjCboChild->strGetAttr(clsXMLnode::mscszAttrValue);
                    if ( strValue.isEmpty() != true ) {
                        pcboNew->addItem(strText, QVariant(strValue));
                    } else {
                        pcboNew->addItem(strText);
                    }
                } else {
                    pobjCboChild->intConnectSubscribers(static_cast<void*>(pcboNew));
                }
            }
            QRect rctGeom = pcboNew->geometry();
    
            if ( strX.isEmpty() != true ) {
                rctGeom.moveLeft(strX.toInt());
            }
            if ( strY.isEmpty() != true ) {
                rctGeom.moveTop(strY.toInt());
            }
            if ( strWidth.isEmpty() != true ) {
                rctGeom.setWidth(strWidth.toInt());
            }
            if ( strHeight.isEmpty() != true ) {
                rctGeom.setHeight(strHeight.toInt());
            }
            if ( rctGeom.isValid() == true ) {
                pcboNew->setGeometry(rctGeom);
            }

  • Lifetime Qt Champion

    HI
    Are you sure that effect does not come from the stylesheet?



  • I don't have any styles specified for the combo, however I will check now.


  • Lifetime Qt Champion

    @SPlatten
    But it looks very styled ?
    I mean its not looking like the normal Qt one so i assumed you had use a stylesheet of sorts?



  • I just checked by inserting:

        const QString strStyles = pcboNew->styleSheet();
    

    After the combo is created, the string returned is empty and there are no styles added, could the styles be inherited from the window?


  • Lifetime Qt Champion

    @SPlatten
    Yes, stylesheets affects all children too.
    If stylesheet have a broad selector like
    QWidget, then combobox would also be affected.

    you can try

    ui->pcboNew->setStyleSheet(QLatin1String("QComboBox {\n"
                                              "    border: 1px solid gray;\n"
                                              "    border-radius: 3px;\n"
                                              "    padding: 1px 18px 1px 3px;\n"
                                              "    min-width: 6em;\n"
                                              "}\n"
                                              ""));
        
    

    and see if line is still broken.



  • Any suggestion on how I find out whats effecting it?

    Perhaps call styleSheet on the window?


  • Lifetime Qt Champion

    @SPlatten
    Try for test apply the showed stylesheet.
    Also where do you apply a stylesheet ?
    and how does it look ?



  • @mrjj said in QComboBox, why the gap in the border?:

    ui->pcboNew->setStyleSheet(QLatin1String("QComboBox {\n"
    " border: 1px solid gray;\n"
    " border-radius: 3px;\n"
    " padding: 1px 18px 1px 3px;\n"
    " min-width: 6em;\n"
    "}\n"
    ""));

    I just copied and applied your style sheet and that fixed it, still puzzled why it is broken without that insert.


  • Lifetime Qt Champion

    @SPlatten
    Oh, it was not a fix. just a test.
    Something in the master stylesheet is affecting the combobox as well.
    Maybe some text margin setting that makes an item cover the line or similar.
    Its often a result of use too broad selectors in the sheet so other widgets than the intended is affected.
    http://doc.qt.io/qt-5/stylesheet-examples.html



  • Works great thank you for your help, I've modified the XML parser to parse type specific CSS and add to the object:

        <combo id="cbo1" x="150" y="10" width="80"
               properties="QComboBox {
                               border: 1px solid black;
                               border-radius: 2px;
                               padding: 1px 18px 1px 3px;
                               min-width: 6em}">
            <item text="Item 1" value="123"/>
            <item text="Item 2" value="456"/>
            <item text="Item 3" value="789"/>
        </combo>
    

    The appearance now:
    0_1542921015793_Screen Shot 2018-11-22 at 21.09.37.png


  • Lifetime Qt Champion

    @SPlatten
    Ok, super. You might not need all tags. (like min-width: 6em)
    but if you are fine with look. no harm to it.
    Please use the Topic Tools button to mark as Solved.



  • The idea is to make it as flexible as possible, the look of this is just a demonstration.


  • Lifetime Qt Champion

    @SPlatten
    Ok. if main app always have stylesheet then i see no harm.
    However, if no style sheet is used in main app. it might make combobox look odd
    for an un-styled app.


Log in to reply