QCheckComboBox使用
2013-11-26 02:23
459 查看
class QCheckComboBox : public QComboBox { Q_OBJECT public: QCheckComboBox(QWidget *parent = 0); ~QCheckComboBox(); void appendItem(const QString &text,bool bChecked); void hidePopup(); protected: void mousePressEvent(QMouseEvent *e); private: void updateIndexStatus(int index); signals: void checkedStateChange(int index, bool bChecked); };
实现文件:
QCheckComboBox::QCheckComboBox(QWidget *parent)
: QComboBox(parent)
{
setMinimumSize(QSize(100, 27));
setMaximumSize(QSize(16777215, 25));
}
void QCheckComboBox::appendItem( const QString &text,bool bChecked )
{
QIcon icon;
if(bChecked)
icon.addFile(QString::fromUtf8("./Resources/check.png"));
else
icon.addFile(QString::fromUtf8("./Resources/uncheck.png"));
addItem(icon,text,bChecked);
}
void QCheckComboBox::mousePressEvent( QMouseEvent *e )
{
int x= e->pos().x();
int iconWidth= iconSize().width();
if(x<= iconWidth)
{
int index=currentIndex();
updateIndexStatus(index);
}
else
{
QComboBox::mousePressEvent(e);
}
}
void QCheckComboBox::updateIndexStatus( int index )
{ ///previous state
bool bChecked= itemData(index).toBool();
if(bChecked) ///先前已经是选中状态
{
if(index==0) ///全选
{
for(int i=0; i<this->count(); i++)
{
setItemIcon(i,QIcon(QString::fromUtf8("./Resources/uncheck.png")));
setItemData(i,!bChecked);
}
}
else setItemIcon(index, QIcon(QString::fromUtf8("./Resources/uncheck.png")));
}
else
{
if(index==0)
{
for(int i=0; i<this->count(); i++)
{
setItemIcon(i,QIcon(QString::fromUtf8("./Resources/check.png")));
setItemData(i,!bChecked);
}
}
else setItemIcon(index, QIcon(QString::fromUtf8("./Resources/check.png")));
}
setItemData(index,!bChecked); //previous state取反
int i;
for(i=1; i<this->count(); i++)
{
if(itemData(i).toBool()==false)
{
setItemIcon(0, QIcon(QString::fromUtf8("./Resources/uncheck.png")));
setItemData(0,false);
break;
}
}
if(i==this->count())
{
setItemIcon(0, QIcon(QString::fromUtf8("./Resources/check.png")));
setItemData(0,true);
}
emit checkedStateChange(index,!bChecked);
}
void QCheckComboBox::hidePopup()
{
int iconW = iconSize().width();
int x = QCursor::pos().x() - mapToGlobal(geometry().topLeft()).x() + geometry().x();
int index = view()->selectionModel()->currentIndex().row();
if (x >= 0 && x <= iconW)
{
updateIndexStatus(index);
}
else
{
QComboBox::hidePopup();
}
}
示例如下:
void CCTest::checkChaged( int index,bool bChecked )
{
if(bChecked)
qDebug()<<index<<" is checked";
else
qDebug()<<index<<" is unchecked";
QString strout;
for(int i=0; i<ccbox->count(); i++)
{
if(ccbox->itemData(i).toBool()==true)
strout+=ccbox->itemText(i);
}
qDebug()<<strout;
}
转:http://blog.csdn.net/rabinsong/article/details/9007283
相关文章推荐
- easyui使用combobox报错Uncaught TypeError: row[opts.textField].toLowerCase is not a function
- easyui1.3X版本使用combobox,ie渲染不出效果
- wincc中combobox的使用
- [导入]一篇关于在COMBOBOX中使用SENDMESSAGE的实例
- WPF中ComboBox的SelectionChanged方法的使用注意
- DevExpress 学习使用之 ComboBoxEdit
- C# ComboBox控件使用
- MFC ComboBox 使用方法(转载)
- C# 程序中如何向(DEV)ComboBoxEdit控件中添加下拉列表的值不使用数据绑定,就是使用代码添加
- Extjs comboBox的使用,本地的加载数据
- ComboBox 使用数据绑定时 Sorted 属性的bug
- EasyUI的combobox控件使用onchange 问题
- ComboBox的使用
- Easyui笔记4:实现表格内combobox数据的动态切换(getEditor的使用)
- WPF中ComboBox使用
- Easyui Combobox 使用例子
- easyui中combobox仅允许使用当前数据值处理
- 关于.net的combobox的一点使用心得
- 【转】DataGridViewComboBoxColumn的使用
- MVVM WPF 使用 datagrid 绑定 comboBox