Hi,
I solved almost this problem.
The code is:
In MainWindow I have:
for( int r=0; r<5; r++ ){
SubQStandardItem
*item
= new SubQStandardItem
( QString("Row:%1").
arg(r
) );
item->setEditable(false);
for( int i=0; i<3; i++ ){
SubQStandardItem
*subItem
= new SubQStandardItem
( QString("Item %1").
arg(i
) );
subItem->setCheckable(true);
subItem->setEditable(false);
item->appendRow(subItem);
}
model->setItem(r, item);
}
view->setModel(model);
view->show();
}
void MainWindow::startTree(){ QStandardItemModel *model = new QStandardItemModel();
for( int r=0; r<5; r++ ){
SubQStandardItem *item = new SubQStandardItem( QString("Row:%1").arg(r) );
item->setEditable(false);
for( int i=0; i<3; i++ ){
SubQStandardItem *subItem = new SubQStandardItem( QString("Item %1").arg(i) );
subItem->setCheckable(true);
subItem->setEditable(false);
item->appendRow(subItem);
}
model->setItem(r, item);
}
view = new QTreeView(centralWidget);
view->setModel(model);
view->show();
}
To copy to clipboard, switch view to plain text mode
SubQStandardItem.h:
/* * SubQStandardItem.h
*
* Created on: 23/09/2011
* Author: Sergio Madrazo Giménez
*/
#ifndef SUBQSTANDARDITEM_H_
#define SUBQSTANDARDITEM_H_
#include <QStandardItem>
{
public:
virtual ~SubQStandardItem();
private:
void setData
(const QVariant & value,
int role
);
};
#endif /* SUBQSTANDARDITEM_H_ */
/* * SubQStandardItem.h
*
* Created on: 23/09/2011
* Author: Sergio Madrazo Giménez
*/
#ifndef SUBQSTANDARDITEM_H_
#define SUBQSTANDARDITEM_H_
#include <QStandardItem>
class SubQStandardItem : public QStandardItem
{
public:
SubQStandardItem(QString);
virtual ~SubQStandardItem();
private:
void setData(const QVariant & value, int role);
void uncheckBranchItems(QStandardItem * parent_item);
};
#endif /* SUBQSTANDARDITEM_H_ */
To copy to clipboard, switch view to plain text mode
SubQStandardItem.cpp:
/* * SubQStandardItem.cpp
*
* Created on: 23/09/2011
* Author: smadrazo
*/
#include "SubQStandardItem.h"
SubQStandardItem
::SubQStandardItem(QString qs
)
{
// TODO Auto-generated constructor stub
}
SubQStandardItem::~SubQStandardItem() {
// TODO Auto-generated destructor stub
}
void SubQStandardItem
::setData(const QVariant & value,
int role
) {
if (Qt::CheckStateRole == role){
if (value.isValid() && (Qt::Checked == static_cast<Qt::CheckState>(value.toInt())))
uncheckBranchItems(model()->invisibleRootItem());
}
}
void SubQStandardItem
::uncheckBranchItems(QStandardItem * parent_item
){ int count=0;
for (int i = 0; i < parent_item->rowCount(); ++i)
{
if (Qt::Checked == child_item->checkState())
count++;
uncheckBranchItems(child_item);
}
if(count>1){
for (int i = 0; i < parent_item->rowCount(); ++i){
child_item->setCheckState(Qt::Unchecked);
}
}
}
/* * SubQStandardItem.cpp
*
* Created on: 23/09/2011
* Author: smadrazo
*/
#include "SubQStandardItem.h"
SubQStandardItem::SubQStandardItem(QString qs)
: QStandardItem(qs)
{
// TODO Auto-generated constructor stub
}
SubQStandardItem::~SubQStandardItem() {
// TODO Auto-generated destructor stub
}
void SubQStandardItem::setData(const QVariant & value, int role)
{
QStandardItem::setData(value, role);
if (Qt::CheckStateRole == role){
if (value.isValid() && (Qt::Checked == static_cast<Qt::CheckState>(value.toInt())))
uncheckBranchItems(model()->invisibleRootItem());
}
QStandardItem::setData(value, role);
}
void SubQStandardItem::uncheckBranchItems(QStandardItem * parent_item){
int count=0;
for (int i = 0; i < parent_item->rowCount(); ++i)
{
QStandardItem * child_item = parent_item->child(i);
if (Qt::Checked == child_item->checkState())
count++;
uncheckBranchItems(child_item);
}
if(count>1){
for (int i = 0; i < parent_item->rowCount(); ++i){
QStandardItem * child_item = parent_item->child(i);
child_item->setCheckState(Qt::Unchecked);
}
}
}
To copy to clipboard, switch view to plain text mode
This code checks only one item of the row.
I hope it help you!
Bookmarks