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. How to align QComboBox dropdown menu
Qt 6.11 is out! See what's new in the release blog

How to align QComboBox dropdown menu

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 3.0k Views 1 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.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    I used QLineEdit to align QCombobox texts to center, but the dropdown menu is still aligned to the left. How can I align it to center?
    Annotation 2020-05-06 103842.png

    2nd question. After using QLineEdit, clicking on the text will not popup dropdown menu. I have to click that down-arrow to call the dropdown. Is there a way to pop it up by just clicking the text?

    1 Reply Last reply
    0
    • eyllanescE Offline
      eyllanescE Offline
      eyllanesc
      wrote on last edited by eyllanesc
      #2

      If you want to align the texts of a QComboBox to the center, it is not necessary to use a QLineEdit since a QProxyStyle is enough. In the case of the popup, a custom delegate is sufficient:

      #include <QtWidgets>
      
      
      class AlignDelegate: public QStyledItemDelegate{
      public:
          AlignDelegate(Qt::Alignment alignment, QObject *parent = nullptr):
              QStyledItemDelegate(parent), m_alignment(alignment)
          {
      
          }
      protected:
          void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
          {
              QStyledItemDelegate::initStyleOption(option, index);
              option->displayAlignment = m_alignment;
          }
          Qt::Alignment m_alignment;
      };
      
      class AlignComboBoxProxy: public QProxyStyle{
      public:
          using QProxyStyle::QProxyStyle;
          void drawItemText(QPainter *painter,
                            const QRect &rect,
                            int /*flags*/,
                            const QPalette &pal,
                            bool enabled,
                            const QString &text,
                            QPalette::ColorRole textRole) const
          {
      
              QProxyStyle::drawItemText(painter, rect, Qt::AlignCenter, pal, enabled, text, textRole);
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          QComboBox w;
          w.setStyle(new AlignComboBoxProxy(w.style()));
          for(int i=0; i < 10; ++i){
              w.addItem(QString("item-%1").arg(i));
          }
          w.setItemDelegate(new AlignDelegate(Qt::AlignCenter, &w));
          w.show();
          return a.exec();
      }
      

      If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

      M 1 Reply Last reply
      3
      • eyllanescE eyllanesc

        If you want to align the texts of a QComboBox to the center, it is not necessary to use a QLineEdit since a QProxyStyle is enough. In the case of the popup, a custom delegate is sufficient:

        #include <QtWidgets>
        
        
        class AlignDelegate: public QStyledItemDelegate{
        public:
            AlignDelegate(Qt::Alignment alignment, QObject *parent = nullptr):
                QStyledItemDelegate(parent), m_alignment(alignment)
            {
        
            }
        protected:
            void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
            {
                QStyledItemDelegate::initStyleOption(option, index);
                option->displayAlignment = m_alignment;
            }
            Qt::Alignment m_alignment;
        };
        
        class AlignComboBoxProxy: public QProxyStyle{
        public:
            using QProxyStyle::QProxyStyle;
            void drawItemText(QPainter *painter,
                              const QRect &rect,
                              int /*flags*/,
                              const QPalette &pal,
                              bool enabled,
                              const QString &text,
                              QPalette::ColorRole textRole) const
            {
        
                QProxyStyle::drawItemText(painter, rect, Qt::AlignCenter, pal, enabled, text, textRole);
            }
        };
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            QComboBox w;
            w.setStyle(new AlignComboBoxProxy(w.style()));
            for(int i=0; i < 10; ++i){
                w.addItem(QString("item-%1").arg(i));
            }
            w.setItemDelegate(new AlignDelegate(Qt::AlignCenter, &w));
            w.show();
            return a.exec();
        }
        
        M Offline
        M Offline
        MasterBlade
        wrote on last edited by
        #3

        @eyllanesc said in How to align QComboBox dropdown menu:

        If you want to align the texts of a QComboBox to the center, it is not necessary to use a QLineEdit since a QProxyStyle is enough. In the case of the popup, a custom delegate is sufficient:

        #include <QtWidgets>
        
        
        class AlignDelegate: public QStyledItemDelegate{
        public:
            AlignDelegate(Qt::Alignment alignment, QObject *parent = nullptr):
                QStyledItemDelegate(parent), m_alignment(alignment)
            {
        
            }
        protected:
            void initStyleOption(QStyleOptionViewItem *option, const QModelIndex &index) const
            {
                QStyledItemDelegate::initStyleOption(option, index);
                option->displayAlignment = m_alignment;
            }
            Qt::Alignment m_alignment;
        };
        
        class AlignComboBoxProxy: public QProxyStyle{
        public:
            using QProxyStyle::QProxyStyle;
            void drawItemText(QPainter *painter,
                              const QRect &rect,
                              int /*flags*/,
                              const QPalette &pal,
                              bool enabled,
                              const QString &text,
                              QPalette::ColorRole textRole) const
            {
        
                QProxyStyle::drawItemText(painter, rect, Qt::AlignCenter, pal, enabled, text, textRole);
            }
        };
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            QComboBox w;
            w.setStyle(new AlignComboBoxProxy(w.style()));
            for(int i=0; i < 10; ++i){
                w.addItem(QString("item-%1").arg(i));
            }
            w.setItemDelegate(new AlignDelegate(Qt::AlignCenter, &w));
            w.show();
            return a.exec();
        }
        

        This is perfect! Thank you so much!

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