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

QCombobox alignment



  • Screenshot from 2020-07-13 16-17-55.png

    I can't seem to get this widget aligned, no matter what I do the 'size' combobox always goes left.
    Here is the relevant pieces of code, first creating the box and later the widget

    sizeBox = new QComboBox;
    QStringList sizeCommands = { "Size - Huge (175%)",
     "Size - Larger (150%)",
     "Size - Large (120%)",
     "Size - Normal (100%)",
     "Size - Small (75%)",
     "Size - Smallest (50%)"};
    sizeBox->addItems(sizeCommands);
    sizeBox->setEditable(true);
    sizeBox->lineEdit()->setReadOnly(true);
    sizeBox->lineEdit()->setAlignment(Qt::AlignCenter);
    for (int i = 0 ; i < sizeBox->count() ; ++i) {
    		sizeBox->setItemData(i, Qt::AlignCenter, Qt::TextAlignmentRole);
    }
    
    sizeBox->setFixedHeight(BUTH*1.3);
    sizeBox->setMaximumWidth(WIDTH-140);
    sizeBox->setStyleSheet("QComboBox {border: 1px solid gray; border-radius: 10px; padding: 1px 18px 1px 3px; min-width: 6em;} QComboBox::down-arrow { image: url(:/Media/arrow.png); border-left-color: #262739;} QComboBox::down-arrow:on {top: 1px; left: 1px;}");
    sizeBox->setEditable(true);
    QLineEdit *edit = sizeBox->lineEdit();
    edit->setReadOnly(true);
    QFont font("Myriad Pro", 15, QFont::Bold);
    edit->setFont(font);
    sizeBox->setFont(font);
    sizeBox->setCurrentIndex(3);
    displayConfigPageLay = new QVBoxLayout(displayConfigPage);
    displayRate->setStyleSheet("border-radius:20px;");
    initButton(listButton, "Browse...");
    listButton->setFixedSize(WIDTH/3.5, BUTH*2);
    listButton->setFont(QFont("Myriad Pro", 16, QFont::Bold));
    listText->setFixedSize(WIDTH - WIDTH/1.7 , BUTH*1.3);
    listWidget->setFixedWidth(WIDTH - 100);
    listText->setStyleSheet("border-radius:10px;");
    displayConfigDoneButton = new QPushButton();
    initButton(displayConfigDoneButton, "Apply");
    sizeBox->setParent(displayConfigPage);
    displayConfigPageLay->setSpacing(5);
    displayConfigPageLay->setAlignment(Qt::AlignCenter);
    displayConfigPageLay->addWidget(rateWidget);
    displayConfigPageLay->addWidget(sizeBox);
    displayConfigPageLay->addWidget(listWidget);
    displayConfigPageLay->addWidget(practiceButton);
    displayConfigPageLay->addWidget(displayConfigDoneButton);
    displayConfigPage->setLayout(displayConfigPageLay);
    

    Any hints on what I'm doing wrong?


  • Moderators

    @Jonathan-Levanon said in QCombobox alignment:

    padding: 1px 18px 1px 3px;

    IIRC this is top, right, bottom, left so you give it a padding of 18 pixels to the right.

    My guess is, this is the problem :D



  • Sadly this doesn't help :(
    Screenshot from 2020-07-13 16-45-48.png


  • Moderators

    @Jonathan-Levanon

    seems like your width is bigger than the max width of your combobox + spacing/margin

    so the layout aligns it to the left.

    You can move it to its own QHBoxLayout and add QSpacers to the left and right. That should center the combobox for all width's



  • Screenshot from 2020-07-13 18-31-08.png

    Thanks, wrapped it with a widget and now it's much better!


Log in to reply