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. QCombobox alignment
Qt 6.11 is out! See what's new in the release blog

QCombobox alignment

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.7k Views
  • 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.
  • Jonathan LevanonJ Offline
    Jonathan LevanonJ Offline
    Jonathan Levanon
    wrote on last edited by
    #1

    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?

    J.HilkJ 1 Reply Last reply
    0
    • Jonathan LevanonJ Jonathan Levanon

      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?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • Jonathan LevanonJ Offline
        Jonathan LevanonJ Offline
        Jonathan Levanon
        wrote on last edited by
        #3

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

        J.HilkJ 1 Reply Last reply
        0
        • Jonathan LevanonJ Jonathan Levanon

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

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @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


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • Jonathan LevanonJ Offline
            Jonathan LevanonJ Offline
            Jonathan Levanon
            wrote on last edited by
            #5

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

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

            1 Reply Last reply
            1

            • Login

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