Reputation: 51
I have a QComboBox
with a QStandardItemModel
, which contains a single item named One
.
I want the QComboBox
to have an header (I’m not sure this is the correct technical term …) which will always be the same.
The following image depicts exactly what I want. Instead of having "One" next to the button that push down the list, I want "Header" printed (which is not an element of the list).
Important, the checkbox are mandatory (which is why I'm using QComboBox).
I tried the function model.setHorizontalHeaderItem()
but it does not work (see the code below).
Please, help me.
#include <QApplication>
#include <QComboBox>
#include <QStandardItemModel>
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QComboBox* comboBox = new QComboBox();
QStandardItemModel model( 1, 1 );
QStandardItem *item = new QStandardItem( QString("One") );
item->setFlags( Qt::ItemIsUserCheckable | Qt::ItemIsEnabled );
item->setData ( Qt::Unchecked, Qt::CheckStateRole );
model.setItem(0, 0, item);
model.setHorizontalHeaderItem( 0, new QStandardItem( "Header" ) );
comboBox->setModel( &model );
comboBox->show();
return app.exec();
}
Upvotes: 0
Views: 1505
Reputation: 8994
You can do something like this:
QTreeView *view = new QTreeView();
QStandardItemModel *model = new QStandardItemModel();
ui->comboBox->setModel( model );
ui->comboBox->setView( view );
for ( int i = 0; i < 10; i++ )
{
QStandardItem *item = new QStandardItem();
const QString text = QString( "Item: %1" ).arg( i + 1 );
item->setText( text );
model->appendRow( item );
}
model->setHorizontalHeaderLabels( QStringList() << "It's a column" );
You will get something like this:
Now you can do all customization like with a standard QTreeView
.
Upvotes: 1