QGroupBox Title Position

  • I wanted an enclosed box around several lines of text with at title so I chose a QGroupBox that held a VBox layout which held a QLabel with several lines of text. The problem I have is that the top of the title text is even with the QGroupBox frame (so the title is basically inside the frame, not on top as the examples show) and the title overlaps part of the first line of the label text. So, is there a way to correct this? Thanks,

  • Can you show the code? Without seeing the code for making the box/frame/layout I can't tell what might be the problem.

    My only guess is that it could be a stylesheet issue. Or it could be the OS you are using, different OSes show group boxes differently. But never in a "buggy" way.

  • OK, here is the code. The page has a logo, a large label for a unit name and then two columns for information. This is where the labels with multiple lines go inside the group boxes.
    UnitView::UnitView(QWidget* pParent)
    : QWidget(pParent)
    pLabelSF_Logo = new QLabel;
    pLabelSF_Logo->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    pLayoutLogo = new QHBoxLayout;
    pNameLabel = new QLabel;
    pNameLabel->setText(QString("No Unit Loaded"));
    int PointSize = QApplication::font().pointSize();
    pNameLabel->setFont(QFont("Verdana", (PointSize*4)));

    pLayoutUnitName  = new QHBoxLayout;                                         
    pFileInfoLabel = new QLabel;                                                
    pGroupInfoLeft   = new QGroupBox;                                           
        QLatin1String("QGroupBox{border: 2px solid rgb(127, 127, 127);"         
                      "       border-radius: 3px}"));                           
    pGroupInfoLeft->setTitle("Configuration File");                             
    pGroupInfoRight  = new QGroupBox;                                           
        QLatin1String("QGroupBox{border: 2px solid rgb(127, 127, 127);"         
                      "       border-radius: 3px}"));                           
    pGroupInfoRight->setTitle("Attached Unit");                                 
    pLayoutInfoLeft = new QVBoxLayout;                                          
    pLayoutInfoRight = new QVBoxLayout;                                         
    pLayoutInfoBase = new QHBoxLayout;                                          
    pLayoutMainV = new QVBoxLayout;                                             
    pLayoutMainH = new QHBoxLayout;                                             


  • Lifetime Qt Champion


    Does it also happen if you remove the style sheet ?

    By the way, which version of windows are you running ?

  • I have seen that behavior with bad style sheets before. Like SGaist said comment out the style sheet and see if that fixes it, then you need to tweak your style sheet to make it look right.

    I have had to play with stylesheets a lot to get them to look right with widgets. And usually I need separate sheets for each OS.

  • Thanks for the suggestion. That helped. I am very new to this UI stuff, have written embedded code for 30 years but no UI. I would like a more defined boarder on the group box. Any suggestions on how to accomplish that without getting back in trouble? SGalst, I am running on XT until I finish this then moving all my stuff to Win7.

    !http://i58.tinypic.com/2ppe99u.jpg(No Style Sheet)!

  • Lifetime Qt Champion

    Look at the "Qt Style Sheet Examples" and search for "Customizing QGroupBox" You have there a good base to start with to customize your QGroupBox

  • I have been for about the last 3 hours and it mostly gets worse, not better. Very frustrating. I have managed to get the label basically where I want it but it now completely ignores the boarder setting. The boarder is just the default boarder. If the style sheet was bad I get a notice on the console it cannot be parsed but I am not getting any notice that there is any problem with this. This is what I have in the code:
    QLatin1String("QGroupBox{border: 2px solid rgb(127, 127, 127);"
    " border-radius: 3px;}"));
    QLatin1String("QGroupBox::title{subcontrol-origin: margin;"
    " subcontrol-position: top center;}"));

    Any thoughts on why the boarder specification is being ignored? Thanks,

  • It's because you overwrote the stylesheet with the border with the new one. Calling setStyleSheet completely replaces the stylesheet so what you should do is combine those 2:

    QString ss = "QGroupBox {"
    "border: 2px solid rgb(127,127,17);"
    "border-radius: 3px;"
    "QGroupBox::title {"
    "subcontrol-origin: margin;"
    "subcontrol-position: top center;


    You can of course omit the QString and just put all that straight into the setStyleSheet call.

  • Thanks, I had just figured that out when I got your email. I naively thought that changing a style setting would just update that setting. I also read somewhere that when no styles are set the OS draws the items. When you start setting styles for an object then Qt starts drawing them. So that seems to be why just modifying the boarder width and color affects other items like the title placement. Also figured out I needed to specify a margin-top value to move the text up. Pretty much have the look I want. I had hoped to allow the user to modify the text font and size but that is looking like it will not work very well given the various style tweaks needed to get things to look a certain way.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.