Centering QComboBox in table cell



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


  • Moderators

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



  • @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)?


  • Moderators

    @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.



  • @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.


  • Moderators

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



  • @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?


  • Moderators

    @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.



  • @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?


  • Moderators

    @gabor53
    try:

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


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


  • Moderators

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



  • @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.


  • Moderators

    @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;
    }
    


  • @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.


Log in to reply
 

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