QCheckBox as item of QComboBox
Hello.
I bumped into a problem of adding an item QCheckBox into QComboBox. What I`ve found is the code for creating ItemDelegate for QComboBox, but it makes all of items in QComboBox as QCheckBox. My task is to add just one check box into the list of common combobox`s items...
Here is a code I found:
Code:
{
Q_OBJECT;
public:
virtual ~CheckBoxList();
private:
};
Code:
{
public:
CheckBoxListDelegate
(QObject *parent
) {
;
}
{
//Get item data
bool value = index.data(Qt::UserRole).toBool();
QString text
= index.
data(Qt
::DisplayRole).
toString();
// fill style options with item data
opt.
state |
= QStyle::State_Enabled;
opt.text = text;
opt.rect = option.rect;
// draw item data as CheckBox
style
->drawControl
(QStyle::CE_CheckBox,
&opt,painter
);
}
{
// create check box as our editor
return editor;
}
void setEditorData
(QWidget *editor,
{
//set editor data
QCheckBox *myEditor
= static_cast<QCheckBox
*>
(editor
);
myEditor->setText(index.data(Qt::DisplayRole).toString());
myEditor->setChecked(index.data(Qt::UserRole).toBool());
}
{
//get the value from the editor (CheckBox)
QCheckBox *myEditor
= static_cast<QCheckBox
*>
(editor
);
bool value = myEditor->isChecked();
//set model data
QMap<int,QVariant> data;
data.insert(Qt::DisplayRole,myEditor->text());
data.insert(Qt::UserRole,value);
model->setItemData(index,data);
}
};
CheckBoxList
::CheckBoxList(QWidget *widget
){
// set delegate items view
view()->setItemDelegate(new CheckBoxListDelegate(this));
// Enable editing on items view
}
CheckBoxList::~CheckBoxList()
{
;
}
{
painter.
setPen(palette
().
color(QPalette::Text));
// draw the combobox frame, focusrect and selected etc.
initStyleOption(&opt);
// if no display text been set , use "..." as default
if(m_DisplayText.isNull())
opt.currentText = "...";
else
opt.currentText = m_DisplayText;
painter.
drawComplexControl(QStyle::CC_ComboBox, opt
);
// draw the icon and text
painter.
drawControl(QStyle::CE_ComboBoxLabel, opt
);
}
void CheckBoxList
::SetDisplayText(QString text
) {
m_DisplayText = text;
}
QString CheckBoxList
::GetDisplayText() const {
return m_DisplayText;
}
Is anyone have thoughts how to improve this code to get possibility for adding into QComboBox 2 types of items: QCheckBox and common QComboBox items?
Or there is some other ways to achieve such behaviour?
Re: QCheckBox as item of QComboBox
Should be simple if you use QModelIndex to track items
For example if you want only item at index 3 to be QCheckBox, and rest to be standard QComboBox items, then you need to modify this way
Code:
{
if(index.row() == 2) // Check for row 3 //Add this
{
// create check box as our editor
return editor;
}
return 0; //Add this, for default
}
Re: QCheckBox as item of QComboBox
Thanks! It works! But next questions is how to check that index.row() is the last row in combo box?
Re: QCheckBox as item of QComboBox
Code:
if(index.row() == index.model()->rowCount() - 1)
//then last row
Re: QCheckBox as item of QComboBox
Sorry for my inattention in reading documentation. I`ve did it already the same way )
Let this topic be opened. Coz I just studying that part of Qt (QModelIndex, QItemDelegate ant etc.) and I`m sure that new questions would appear.
The next thing I would try to do is to get (and set too) data from(to) checkbox.
Re: QCheckBox as item of QComboBox
I`ve done it.
I just added ne set/get functions:
Code:
{
Q_OBJECT;
public:
virtual ~CheckBoxList();
bool GetAutoCalcValue();
void SetAutoCalcValue(bool checked);
};
And the implentation:
Code:
bool CheckBoxList::GetAutoCalcValue()
{
QModelIndex index
= model
()->index
(model
()->rowCount
() - 1,
0);
return index.data(Qt::UserRole).toBool();
}
void CheckBoxList::SetAutoCalcValue(bool checked)
{
QModelIndex index
= model
()->index
(model
()->rowCount
() - 1,
0);
QString text
= index.
data(Qt
::DisplayRole).
toString();
QMap<int,QVariant> data;
data.insert(Qt::DisplayRole,text);
data.insert(Qt::UserRole,checked);
model()->setItemData(index,data);
}
Re: QCheckBox as item of QComboBox
Wouldn't it be easier to just use a model with the combobox and return the ItemIsUserCheckable flag for the items you want checkable?