Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QComboBox and HeaderItem



  • Dear all, 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.

    [1] To be more precise, running the code below leads to the following output:
    "One" (as the header) + "One" (with a checkbox).

    [2] What I'm looking for is as follows:
    "Header" (as the header) + "One" (with a checkbox).

    I tried the function "model.setHorizontalHeaderItem()" but it leads to the same as [1] above.
    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();
    }@



  • See QComboBox::setView , QComboBox uses QListWidget as view by default and there's no header. Set QTreeWidget as QComboBox view and there you can set header.


Log in to reply