PDA

View Full Version : Java/Jambi: TableView doesn't look as I want to



mw0001
26th February 2010, 15:30
Hi there,

I want to build a table where the User can assign "roles" to an "user".

There shall be three columns:
1. col should be a checkbox, checked if an assignment is done, unchecked if not.
2. col should be the name of the role to assign
3. col should be the description of the role.

To do this, i've set up my own model



public class AssignableRolesModel extends QAbstractTableModel
{
private final List<Boolean> selected = new ArrayList<Boolean>();
private final List<String> name = new ArrayList<String>();
private final List<String> description = new ArrayList<String>();
private final IRoleClient roleClient = new RestRoleClient();
private final IUserClient userClient = new RestUserClient();
private final Set<Role> roles = new HashSet<Role>();

@Override
public int columnCount(QModelIndex arg0)
{
return 3;
}

@Override
public Object data(QModelIndex index, int role)
{
Object returnObject = new Object();

if (role == Qt.ItemDataRole.DisplayRole || role == Qt.ItemDataRole.EditRole)
{
switch (index.column())
{
case 0:
return new QVariant();
case 1:
returnObject = name.get(index.row());
break;
case 2:
returnObject = description.get(index.row());
break;
default:
throw new IndexOutOfBoundsException("Column must be between 0 and 2.");
}

return returnObject;
} else if (role == Qt.ItemDataRole.CheckStateRole)
{
if (index.column() == 0) { return selected.get(index.row()); }

} else if (role == Qt.ItemDataRole.ToolTipRole)
{[...]
}
return super.data(index);
}

@Override
public Object headerData(int section, Orientation orientation, int role)
{
if (role == Qt.ItemDataRole.DisplayRole || role == Qt.ItemDataRole.EditRole)
{
if (orientation == Orientation.Horizontal)
{
String headerName = "";
switch (section)
{
case 0:
headerName = "Zugeordnet";
break;
case 1:
headerName = "Rollenname";
break;
case 2:
headerName = "Beschreibung";
break;
default:
headerName = "";
break;
}
return headerName;
} else
{
return null;
}
} else if (role == Qt.ItemDataRole.ToolTipRole)
{[...]}
return null;
} else
{
return super.headerData(section, orientation, role);
}
}

public void setData(QModelIndex index, QVariant value, int role)
{
if (role == Qt.ItemDataRole.DisplayRole || role == Qt.ItemDataRole.EditRole)
{
if (index.column() == 0)
{
boolean valueToSet = value != null;
selected.set(index.row(), valueToSet);
}
}
}

@Override
public int rowCount(QModelIndex index)
{
return selected.size();
}

@Override
public ItemFlags flags(QModelIndex index)
{
if (index.column() == 0)
{
ItemFlags itemFlags = new ItemFlags(new ItemFlag[]
{ ItemFlag.ItemIsSelectable, ItemFlag.ItemIsEditable, ItemFlag.ItemIsEnabled, ItemFlag.ItemIsUserCheckable });

return itemFlags;
}
return new ItemFlags(0);
}
}

As you see I overwrite columnCount(), rowCount(), data(), headerData(), setData() and flags().

What I get is:
1. all columns have checkboxes (but only the 1st should),
2. the 1st Colum also gives text "com.trolltech.qt.QVariant@(someID) ,
3. the checkbox in Column1 can't be clicked, and
4. the checkbox is unchecked, even when the value is 'true'.

Can you give me some hints how to solve my problem and get the table look like I want it to?
Thanks in advance :)