Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Centering QComboBox in table cell
Qt 6.12 LTS is out! See what's new in the release blog

Centering QComboBox in table cell

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 2 Posters 5.7k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    My QComboBox, which is created by my custom delegate, is placed in a table. Currently it is aligned to the top of the cell. How can I center the QComboBox in the cell? The current code is:

    QWidget* WhatFixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
    
      Q_UNUSED(option);
      Q_UNUSED(index);
      QComboBox* whatComboEditor = new QComboBox(parent);
      whatComboEditor->setFrame(true);
      whatComboEditor->setStyleSheet("background-color:rgb(255,217,229)");
      whatComboEditor->setEditable(true);
      whatComboEditor->setMaximumWidth (100);
      whatComboEditor->setMinimumWidth (100);
      whatComboEditor->setMaximumHeight (20);
      whatComboEditor->setMinimumHeight (20);
    
      QSqlQuery queryLoadWhat("SELECT What FROM  What_Table ORDER BY What asc", db);
    
      while(queryLoadWhat.next()) {
        QString whatItem = queryLoadWhat.value(0).toString();
    
        whatComboEditor->addItem(whatItem);
      }
      return whatComboEditor;
    }
    

    Thank you.

    raven-worxR 1 Reply Last reply
    0
    • G gabor53

      Hi,
      My QComboBox, which is created by my custom delegate, is placed in a table. Currently it is aligned to the top of the cell. How can I center the QComboBox in the cell? The current code is:

      QWidget* WhatFixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
      
        Q_UNUSED(option);
        Q_UNUSED(index);
        QComboBox* whatComboEditor = new QComboBox(parent);
        whatComboEditor->setFrame(true);
        whatComboEditor->setStyleSheet("background-color:rgb(255,217,229)");
        whatComboEditor->setEditable(true);
        whatComboEditor->setMaximumWidth (100);
        whatComboEditor->setMinimumWidth (100);
        whatComboEditor->setMaximumHeight (20);
        whatComboEditor->setMinimumHeight (20);
      
        QSqlQuery queryLoadWhat("SELECT What FROM  What_Table ORDER BY What asc", db);
      
        while(queryLoadWhat.next()) {
          QString whatItem = queryLoadWhat.value(0).toString();
      
          whatComboEditor->addItem(whatItem);
        }
        return whatComboEditor;
      }
      

      Thank you.

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @gabor53
      easiest would be to wrap the combobox within a separate QWIdget with a layout where you add the combobox centered.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      G 1 Reply Last reply
      2
      • raven-worxR raven-worx

        @gabor53
        easiest would be to wrap the combobox within a separate QWIdget with a layout where you add the combobox centered.

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        @raven-worx
        I added the following:

         QComboBox* whatComboEditor = new QComboBox(parent);
        
          whatComboEditor->setFrame(true);
          whatComboEditor->setStyleSheet("background-color:rgb(255,217,229);"
                                        );
          whatComboEditor->setEditable(true);
          whatComboEditor->setMaximumWidth (100);
          whatComboEditor->setMinimumWidth (100);
          whatComboEditor->setMaximumHeight (30);
          whatComboEditor->setMinimumHeight (30);
        
          whatComboEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
        
          QGridLayout* whatLayout = new QGridLayout(parent);
        
          whatLayout->setSpacing (0);
          whatLayout->setContentsMargins (0, 30, 0, 0);
        
          whatLayout->setAlignment (whatLayout, Qt::AlignCenter);
          whatLayout->addItem (new QSpacerItem(100, 30, QSizePolicy::Preferred, QSizePolicy::Expanding), 0, 0);
          whatLayout->addWidget (whatComboEditor, 1, 1, Qt::AlignBottom);
        

        Right now the QComboBox is floating over another cell. How can I move it to the right position (in the middle of the appropriate cell)?

        raven-worxR 1 Reply Last reply
        0
        • G gabor53

          @raven-worx
          I added the following:

           QComboBox* whatComboEditor = new QComboBox(parent);
          
            whatComboEditor->setFrame(true);
            whatComboEditor->setStyleSheet("background-color:rgb(255,217,229);"
                                          );
            whatComboEditor->setEditable(true);
            whatComboEditor->setMaximumWidth (100);
            whatComboEditor->setMinimumWidth (100);
            whatComboEditor->setMaximumHeight (30);
            whatComboEditor->setMinimumHeight (30);
          
            whatComboEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
          
            QGridLayout* whatLayout = new QGridLayout(parent);
          
            whatLayout->setSpacing (0);
            whatLayout->setContentsMargins (0, 30, 0, 0);
          
            whatLayout->setAlignment (whatLayout, Qt::AlignCenter);
            whatLayout->addItem (new QSpacerItem(100, 30, QSizePolicy::Preferred, QSizePolicy::Expanding), 0, 0);
            whatLayout->addWidget (whatComboEditor, 1, 1, Qt::AlignBottom);
          

          Right now the QComboBox is floating over another cell. How can I move it to the right position (in the middle of the appropriate cell)?

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          @gabor53 said in Centering QComboBox in table cell:

          Right now the QComboBox is floating over another cell. How can I move it to the right position (in the middle of the appropriate cell)?

          I dont understand?
          What are you dong with whatLayout? Do you set it to any widget?
          Also i do not see the wrapper widget in your code.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          G 1 Reply Last reply
          0
          • raven-worxR raven-worx

            @gabor53 said in Centering QComboBox in table cell:

            Right now the QComboBox is floating over another cell. How can I move it to the right position (in the middle of the appropriate cell)?

            I dont understand?
            What are you dong with whatLayout? Do you set it to any widget?
            Also i do not see the wrapper widget in your code.

            G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            @raven-worx
            Now I have this:

             QComboBox* whatComboEditor = new QComboBox(parent);
            
              whatComboEditor->setFrame(true);
              whatComboEditor->setStyleSheet("background-color:rgb(255,217,229);"
                                            );
              whatComboEditor->setEditable(true);
              whatComboEditor->setMaximumWidth (100);
              whatComboEditor->setMinimumWidth (100);
              whatComboEditor->setMaximumHeight (30);
              whatComboEditor->setMinimumHeight (30);
            
              QGridLayout* whatLayout = new QGridLayout;
            
              whatComboEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
            
              whatLayout->addWidget (whatComboEditor, 1, 0, 1, 1, Qt::AlignCenter);
            
              QWidget* w = new QWidget();
              w->setLayout (whatLayout);
            

            No error message but no QComboBox in the field either. Do I need to bind widget w to the table cell? If so how? Thank you.

            raven-worxR 1 Reply Last reply
            0
            • G gabor53

              @raven-worx
              Now I have this:

               QComboBox* whatComboEditor = new QComboBox(parent);
              
                whatComboEditor->setFrame(true);
                whatComboEditor->setStyleSheet("background-color:rgb(255,217,229);"
                                              );
                whatComboEditor->setEditable(true);
                whatComboEditor->setMaximumWidth (100);
                whatComboEditor->setMinimumWidth (100);
                whatComboEditor->setMaximumHeight (30);
                whatComboEditor->setMinimumHeight (30);
              
                QGridLayout* whatLayout = new QGridLayout;
              
                whatComboEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
              
                whatLayout->addWidget (whatComboEditor, 1, 0, 1, 1, Qt::AlignCenter);
              
                QWidget* w = new QWidget();
                w->setLayout (whatLayout);
              

              No error message but no QComboBox in the field either. Do I need to bind widget w to the table cell? If so how? Thank you.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

              @gabor53
              you need to set the parent widget (the passed in parameter) to the widget (w) you return

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              G 1 Reply Last reply
              0
              • raven-worxR raven-worx

                @gabor53
                you need to set the parent widget (the passed in parameter) to the widget (w) you return

                G Offline
                G Offline
                gabor53
                wrote on last edited by
                #7

                @raven-worx
                I made the following changes:

                QWidget* WhatFixViewDelegate::createEditor(QWidget* w, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                
                  Q_UNUSED(option);
                  Q_UNUSED(index);
                
                    QComboBox* whatComboEditor = new QComboBox(w);
                  QGridLayout* whatLayout = new QGridLayout;
                  whatComboEditor->setFrame(true);
                  whatComboEditor->setStyleSheet("background-color:rgb(255,217,229);"
                                                );
                  whatComboEditor->setEditable(true);
                
                  whatComboEditor->setMaximumWidth (100);
                  whatComboEditor->setMinimumWidth (100);
                  whatComboEditor->setMaximumHeight (30);
                  whatComboEditor->setMinimumHeight (30);
                
                  whatComboEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                
                  whatLayout->addWidget (whatComboEditor, 1, 0, 1, 1, Qt::AlignCenter);
                 w->setLayout (whatLayout);
                
                  QSqlQuery queryLoadWhat("SELECT What FROM  What_Table ORDER BY What asc", db);
                
                  while(queryLoadWhat.next()) {
                    QString whatItem = queryLoadWhat.value(0).toString();
                
                    whatComboEditor->addItem(whatItem);
                  }
                  return (w);
                }
                

                Now the QComboBox appears, but at the bottom of the cell above. Is there anything else I need to change?

                raven-worxR 1 Reply Last reply
                0
                • G gabor53

                  @raven-worx
                  I made the following changes:

                  QWidget* WhatFixViewDelegate::createEditor(QWidget* w, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                  
                    Q_UNUSED(option);
                    Q_UNUSED(index);
                  
                      QComboBox* whatComboEditor = new QComboBox(w);
                    QGridLayout* whatLayout = new QGridLayout;
                    whatComboEditor->setFrame(true);
                    whatComboEditor->setStyleSheet("background-color:rgb(255,217,229);"
                                                  );
                    whatComboEditor->setEditable(true);
                  
                    whatComboEditor->setMaximumWidth (100);
                    whatComboEditor->setMinimumWidth (100);
                    whatComboEditor->setMaximumHeight (30);
                    whatComboEditor->setMinimumHeight (30);
                  
                    whatComboEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                  
                    whatLayout->addWidget (whatComboEditor, 1, 0, 1, 1, Qt::AlignCenter);
                   w->setLayout (whatLayout);
                  
                    QSqlQuery queryLoadWhat("SELECT What FROM  What_Table ORDER BY What asc", db);
                  
                    while(queryLoadWhat.next()) {
                      QString whatItem = queryLoadWhat.value(0).toString();
                  
                      whatComboEditor->addItem(whatItem);
                    }
                    return (w);
                  }
                  

                  Now the QComboBox appears, but at the bottom of the cell above. Is there anything else I need to change?

                  raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  @gabor53 said in Centering QComboBox in table cell:

                  Now the QComboBox appears, but at the bottom of the cell above. Is there anything else I need to change?

                  then you are probably opening it on the wrong index?
                  Please check the index which is passed to the createEditor() method if it's correct.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  G 1 Reply Last reply
                  0
                  • raven-worxR raven-worx

                    @gabor53 said in Centering QComboBox in table cell:

                    Now the QComboBox appears, but at the bottom of the cell above. Is there anything else I need to change?

                    then you are probably opening it on the wrong index?
                    Please check the index which is passed to the createEditor() method if it's correct.

                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by gabor53
                    #9

                    @raven-worx
                    I checked the index:

                     qDebug() << "Entered WhatFixViewDelegate createEditor. ";
                      qDebug() << "whatfixviewdelegate index: " << index;
                    

                    and it is correct.
                    The QComboBox is not exactly on the bottom of the cell above, a small portion is on the top of the correct cell.
                    I painted w blue:

                     w->setStyleSheet ("background-color: blue");
                    

                    I learned, that widget w is always in the right cell, but the QComboBox is always in between the 2 specific cells no matter, where w is. What did I do incorrectly?

                    raven-worxR 1 Reply Last reply
                    0
                    • G gabor53

                      @raven-worx
                      I checked the index:

                       qDebug() << "Entered WhatFixViewDelegate createEditor. ";
                        qDebug() << "whatfixviewdelegate index: " << index;
                      

                      and it is correct.
                      The QComboBox is not exactly on the bottom of the cell above, a small portion is on the top of the correct cell.
                      I painted w blue:

                       w->setStyleSheet ("background-color: blue");
                      

                      I learned, that widget w is always in the right cell, but the QComboBox is always in between the 2 specific cells no matter, where w is. What did I do incorrectly?

                      raven-worxR Offline
                      raven-worxR Offline
                      raven-worx
                      Moderators
                      wrote on last edited by
                      #10

                      @gabor53
                      try:

                      whatLayout->setSpacing(0);
                      whatLayout->setContentsMargins(0,0,0,0);
                      

                      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                      If you have a question please use the forum so others can benefit from the solution in the future

                      G 1 Reply Last reply
                      1
                      • raven-worxR raven-worx

                        @gabor53
                        try:

                        whatLayout->setSpacing(0);
                        whatLayout->setContentsMargins(0,0,0,0);
                        
                        G Offline
                        G Offline
                        gabor53
                        wrote on last edited by
                        #11

                        @raven-worx
                        I tried, but the result is the same.

                        raven-worxR 1 Reply Last reply
                        0
                        • G gabor53

                          @raven-worx
                          I tried, but the result is the same.

                          raven-worxR Offline
                          raven-worxR Offline
                          raven-worx
                          Moderators
                          wrote on last edited by
                          #12

                          @gabor53
                          then i have no idea left.
                          Can you post a screenshot, maybe this clears something up.

                          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                          If you have a question please use the forum so others can benefit from the solution in the future

                          G 1 Reply Last reply
                          1
                          • raven-worxR raven-worx

                            @gabor53
                            then i have no idea left.
                            Can you post a screenshot, maybe this clears something up.

                            G Offline
                            G Offline
                            gabor53
                            wrote on last edited by
                            #13

                            @raven-worx
                            Here is the image!0_1506394137513_Capture.JPG image url)
                            The QComboBox is supposed to be centered in the middle of the "girl" field.

                            raven-worxR 1 Reply Last reply
                            0
                            • G gabor53

                              @raven-worx
                              Here is the image!0_1506394137513_Capture.JPG image url)
                              The QComboBox is supposed to be centered in the middle of the "girl" field.

                              raven-worxR Offline
                              raven-worxR Offline
                              raven-worx
                              Moderators
                              wrote on last edited by raven-worx
                              #14

                              @gabor53
                              now i see, you should create a wrapper widget. Not reuse the passed widget pointer (which is actually the parent widget = viewport widget).
                              What you actually have already done in the code before... but again left out in the next post of yours?!?

                              QWidget* WhatFixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                Q_UNUSED(index);
                              
                                 QWidget* w = new QWidget( parent );
                              
                                  QComboBox* whatComboEditor = new QComboBox(w);
                                whatComboEditor->setFrame(true);
                                whatComboEditor->setStyleSheet("background-color:rgb(255,217,229);" );
                                whatComboEditor->setEditable(true);
                              
                                whatComboEditor->setMaximumWidth (100);
                                whatComboEditor->setMinimumWidth (100);
                                whatComboEditor->setMaximumHeight (30);
                                whatComboEditor->setMinimumHeight (30);
                              
                                whatComboEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                              
                                QVBoxLayout* whatLayout = new QVBoxLayout;
                                whatLayout->addWidget (whatComboEditor, 0, Qt::AlignCenter);
                              
                                w->setLayout (whatLayout);
                              
                                 QSqlQuery queryLoadWhat("SELECT What FROM  What_Table ORDER BY What asc", db);
                              
                                 while(queryLoadWhat.next())
                                 {
                                     QString whatItem = queryLoadWhat.value(0).toString();
                                     whatComboEditor->addItem(whatItem);
                                 }
                              
                                 w->setGeometry( option.rect ); // don't know if this is really needed - try with and without it 
                              
                                 return w;
                              }
                              

                              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                              If you have a question please use the forum so others can benefit from the solution in the future

                              G 1 Reply Last reply
                              1
                              • raven-worxR raven-worx

                                @gabor53
                                now i see, you should create a wrapper widget. Not reuse the passed widget pointer (which is actually the parent widget = viewport widget).
                                What you actually have already done in the code before... but again left out in the next post of yours?!?

                                QWidget* WhatFixViewDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const {
                                  Q_UNUSED(index);
                                
                                   QWidget* w = new QWidget( parent );
                                
                                    QComboBox* whatComboEditor = new QComboBox(w);
                                  whatComboEditor->setFrame(true);
                                  whatComboEditor->setStyleSheet("background-color:rgb(255,217,229);" );
                                  whatComboEditor->setEditable(true);
                                
                                  whatComboEditor->setMaximumWidth (100);
                                  whatComboEditor->setMinimumWidth (100);
                                  whatComboEditor->setMaximumHeight (30);
                                  whatComboEditor->setMinimumHeight (30);
                                
                                  whatComboEditor->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
                                
                                  QVBoxLayout* whatLayout = new QVBoxLayout;
                                  whatLayout->addWidget (whatComboEditor, 0, Qt::AlignCenter);
                                
                                  w->setLayout (whatLayout);
                                
                                   QSqlQuery queryLoadWhat("SELECT What FROM  What_Table ORDER BY What asc", db);
                                
                                   while(queryLoadWhat.next())
                                   {
                                       QString whatItem = queryLoadWhat.value(0).toString();
                                       whatComboEditor->addItem(whatItem);
                                   }
                                
                                   w->setGeometry( option.rect ); // don't know if this is really needed - try with and without it 
                                
                                   return w;
                                }
                                
                                G Offline
                                G Offline
                                gabor53
                                wrote on last edited by gabor53
                                #15

                                @raven-worx
                                It's much better now without

                                   w->setGeometry( option.rect );
                                

                                It looks like this now:

                                0_1506455648756_Capture.JPG
                                I used

                                whatLayout->setMargin (2);
                                

                                to center.
                                Thank you for your help.

                                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
                                • Unsolved