0
我有一个对象:添加孩子定制QStandardItem
class Mando : public QObject, public QStandardItem {
Q_OBJECT
public:
std::string getDescription() const;
std::string getName() const;
explicit Mando(boost::filesystem::path filen);
private:
boost::filesystem::path file;
};
Q_DECLARE_METATYPE(Mando *)
Mando::Mando(boost::filesystem::path filen) {
file= filen; }
std::string Mando::getName() const {
return file.filename().string(); }
std::string Mando::getDescription() const {
return std::string("XxxxX"); }
和型号实现:
ContentModel::ContentModel() : QStandardItemModel() {
QStringList headers;
headers << tr("Name") << tr("Description");
setHorizontalHeaderLabels(headers);
setColumnCount(headers.count()); }
QVariant ContentModel::data(const QModelIndex &index, int role) const {
if(role == Qt::DisplayRole)
switch(index.column()) {
case 0: return static_cast<Mando*>(item(index.row()))->getName().c_str();
case 1: return static_cast<Mando*>(item(index.row()))->getDescription().c_str(); }
return QStandardItemModel::data(index, role); }
我添加对象是这样的:
model->appendRow(new Mando(boost::filesystem::path("/home/user/riri.ini")));
model->appendRow(new Mando(boost::filesystem::path("/home/user/fifi.ini")));
model->appendRow(new Mando(boost::filesystem::path("/home/user/loulou.ini")));
view= new QTreeView();
view.setModel(model);
一切工作正常,我得到:
riri.ini XxxxX
fifi.ini XxxxX
loulou.ini XxxxX
但是,在某些情况下,我需要添加一个孩子。
riri.ini XxxxX
-> riri's child YyyyY
fifi.ini XxxxX
loulou.ini XxxxX
-> loulou's child YyyyY
如果我在Mando的情况下,添加一个简单的一行,就像这样:
Mando::Mando(boost::filesystem::path filen) {
file= filen;
appendRow(new QStandardItem() /* or */ QList<QStandardItem*>); }
如何区分孩子的ContentModel ::数据父亲?