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. Subclassing QComboBox to have Checkboxes
Qt 6.11 is out! See what's new in the release blog

Subclassing QComboBox to have Checkboxes

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.3k 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.
  • HunterMetcalfeH Offline
    HunterMetcalfeH Offline
    HunterMetcalfe
    wrote on last edited by
    #1

    As the title states I'd like to have the items in my QComboBox to have check boxes. I've subclassed QStandardItemModel in order to do so, but it isn't working. I get the items, but no checkboxes. Normally when I want checkboxes in a QTableView I reimplement the flags function. I've seen where individuals override the addItem function of QCombox and then set the QComboBox to be editable, but that isn't desired as I don't want to give the user the ability to add items into the combo box and I would think, as with other Qt Model/View classes, this would work similarly.

    Header:

    #ifndef CHECKABLECOMBOBOX_HPP
    #define CHECKABLECOMBOBOX_HPP
    
    #include <QStandardItemModel>
    #include <QComboBox>
    
    class CCheckableComboBoxModel : public QStandardItemModel
    {
       Q_OBJECT
    public :
       CCheckableComboBoxModel( QObject * parent = nullptr ) ;
       ~CCheckableComboBoxModel() ;
    
    protected :
       virtual QVariant data( const QModelIndex &index, int role ) const ;
       virtual Qt::ItemFlags flags( const QModelIndex & index ) const ;
       virtual bool setData( const QModelIndex & index, const QVariant & value, int role ) ;
    
    signals :
       void CheckStateChanged( bool checked, const QString & item ) ;
    } ;
    
    class CCheckableComboBox : public QComboBox
    {
       Q_OBJECT
    public :
       CCheckableComboBox( QWidget * parent = nullptr ) ;
       ~CCheckableComboBox() ;
    
    protected :
       CCheckableComboBoxModel * m_model ;
    } ;
    
    #endif // CHECKABLECOMBOBOX_HPP
    
    

    Source

    #include "CheckableComboBox.hpp"
    
    CCheckableComboBoxModel::CCheckableComboBoxModel( QObject * parent ) :
       QStandardItemModel( parent )
    {
    
    }
    
    CCheckableComboBoxModel::~CCheckableComboBoxModel()
    {
    
    }
    
    QVariant CCheckableComboBoxModel::data( const QModelIndex & index, int role ) const
    {
       QVariant retval ;
       if( !index.isValid() )
       {
          return retval ;
       }
    
       QStandardItem * standardItem = item( index.row(), index.column() ) ;
    
       printf( "Item is checkable %d!\n", standardItem->isCheckable() ) ;
    
       if( standardItem != nullptr )
       {
          switch( role )
          {
             case Qt::CheckStateRole :
                return standardItem->checkState() ;
             case Qt::DisplayRole :
                return standardItem->text() ;
             default :
                break ;
          }
       }
    
       return retval ;
    }
    
    Qt::ItemFlags CCheckableComboBoxModel::flags( const QModelIndex & index ) const
    {
       Q_UNUSED( index ) ;
       return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable ;
    }
    
    bool CCheckableComboBoxModel::setData( const QModelIndex & index, const QVariant & value, int role )
    {
       bool retval = false ;
    
       if( index.isValid() && role == Qt::CheckStateRole )
       {
          //get the item
          QStandardItem * standardItem = item( index.row(), index.column() ) ;
    
          Qt::CheckState checkState = ( Qt::CheckState ) value.toInt() ;
          standardItem->setCheckState( checkState ) ;
          emit CheckStateChanged( ( checkState == Qt::Checked ), standardItem->text() ) ;
    
          retval = true ;
       }
    
       return retval ;
    }
    
    CCheckableComboBox::CCheckableComboBox( QWidget * parent ) : QComboBox( parent )
    {
       m_model = new CCheckableComboBoxModel( this ) ;
       setModel( m_model ) ;
    
       addItem( "Hello" ) ;
       addItem( "Bob" ) ;
    }
    
    CCheckableComboBox::~CCheckableComboBox()
    {
    
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      For the items, do you set
      item->setData(Qt::Unchecked, Qt::CheckStateRole);
      as else the state is undefined and it it might not render the checkbox or
      something like that.

      HunterMetcalfeH 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        For the items, do you set
        item->setData(Qt::Unchecked, Qt::CheckStateRole);
        as else the state is undefined and it it might not render the checkbox or
        something like that.

        HunterMetcalfeH Offline
        HunterMetcalfeH Offline
        HunterMetcalfe
        wrote on last edited by
        #3

        @mrjj hmmm so that would require me to overload the addItem function in QComboBox I don't think that's the ideal solution as the model should tell the view all of that information. In the past when I've added items to a QTableView/QListView, I've never had to do that for checkboxes. Since I've reimplemented the flags function it automatically sets all items added to have a CheckStateRole ( in the aforementioned views ) - or at least I thought it worked that way.

        mrjjM 1 Reply Last reply
        0
        • HunterMetcalfeH HunterMetcalfe

          @mrjj hmmm so that would require me to overload the addItem function in QComboBox I don't think that's the ideal solution as the model should tell the view all of that information. In the past when I've added items to a QTableView/QListView, I've never had to do that for checkboxes. Since I've reimplemented the flags function it automatically sets all items added to have a CheckStateRole ( in the aforementioned views ) - or at least I thought it worked that way.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @HunterMetcalfe
          hi
          Hmm i must agree since i used a QStandardItemModel
          but you do have the flag function/custom model so
          I would also assume that should be enough.

          Is the code complete enough to run ?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Please see also https://bugreports.qt.io/browse/QTBUG-60310

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2

            • Login

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