QCombobox header item

From QT Wikipedia

Jump to: navigation, search

Contents

Header item in qcombobox

Just simple example how to make header item in qcombobox list. This method should also adopted for qcombobox.

How it's implemented

I had to extend two classes QItemDelegate and QStandardItemModel

QItemDelegate extended class

Class header

 
#ifndef DELEGATE_H
#define DELEGATE_H
 
#include <QItemDelegate>
 
// Delegate reimplamentation
class HeaderComboboxBoxDelegate : public QItemDelegate
{
    Q_OBJECT
 
public:
    HeaderComboboxBoxDelegate(QObject *parent = 0);
	void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
};
 
#endif
 

Class implementation

 
#include <QModelIndex>
#include <QPainter>
 
#include "headerdelegate.h"
 
// Constructor
HeaderComboboxBoxDelegate::HeaderComboboxBoxDelegate(QObject *parent)
    : QItemDelegate(parent)
{
	//qDebug("Constructor delegate called");
}
 
 
// Paint reimplementation to show it as header item
void HeaderComboboxBoxDelegate::paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
{
 
	// Current value text
	QString valueString = index.model()->data(index, Qt::DisplayRole).toString();
 
 
	// If item is disabled draw it as header item
	if ( !(index.model()->flags(index) & Qt::ItemIsEnabled))
	{
		// Make bold italic font like in html
		QFont *fontBold = new QFont();
		fontBold->setBold(true);
		fontBold->setItalic(true);
		painter->setFont(*fontBold);		
 
		// Fill background grey color
		QPalette::ColorGroup cg = QPalette::Disabled;
		cg = QPalette::Inactive;
        painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
 
		// Draw it
		painter->drawText(option.rect, Qt::AlignLeft | Qt::TextSingleLine,valueString);
 
	}	
	else
	QItemDelegate::paint (painter, option, index);
}
 
 
 

QStandardItemModel extended class

class header

 
#ifndef HEADER_MODEL_H
#define HEADER_MODEL_H
 
#include <QAbstractItemModel>
#include <QStandardItemModel>
 
/**
* Reimplement StandartItemModel with disable item (header item) feature
*/
class HeaderModel : public QStandardItemModel
{
public:
    HeaderModel(QObject *parent = 0);
    Qt::ItemFlags flags(const QModelIndex & index) const;   
};
#endif
 

Class implementation

 
 
#include "headermodel.h"
 
HeaderModel::HeaderModel(QObject *parent) : QStandardItemModel(parent)
{
	//qDebug("Constructor model called called");
}
 
/**
* Lets change some flags to disable group box item in combobox.
*/
Qt::ItemFlags HeaderModel::flags(const QModelIndex & index) const
{
        Qt::ItemFlags flags = QStandardItemModel::flags(index);
 
       if (!index.isValid())
            return flags;
 
        // I do not now is it the best way to do it, but for me it works.
		QString value =	index.data(Qt::UserRole).toString();
		if (value == "groupbox")
		{
			flags &= ~Qt::ItemIsSelectable;
			flags &= ~Qt::ItemIsEnabled;
		}
 
        return flags;
 }
 
 

Main file

 
#include <QApplication>
#include <QHBoxLayout>
#include <QComboBox>
 
//Delegate and model
#include "headerdelegate.h"
#include "headermodel.h"
 
 
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
 
    QWidget *window = new QWidget;
    window->setWindowTitle("Header combobox");
 
    QHBoxLayout *layout = new QHBoxLayout;    
	QComboBox *box = new QComboBox();
 
	HeaderComboboxBoxDelegate delegate;
	box->setModel(new HeaderModel());
	box->setItemDelegate(&delegate);
 
	/******************/
	box->addItem("Normal item");
	box->addItem("Header item","groupbox");
	box->addItem("normal item");
	box->addItem("normal item");
	box->addItem("not group box");
 
	//Make item group box
	box->setItemData(4,"groupbox");
 
	layout->addWidget(box);
    window->setLayout(layout);
    window->show();
 
    return app.exec();
}
 

Source code zip

Source code zipped


Some screen shots

Qcombobox live example

 It shows how looks like our new qcombobox.
It shows how looks like our new qcombobox.

Personal tools
Toolbox