Есть две модели данных;
ObjectModelBrowser
TreeModelBrowser
в ObjectModelBrowser отображается общая модель
в TreeModelBrowser отображается часть модели от ObjectModelBrowser
C++ (Qt)
QVariant ObjectBrowserModel::data(const QModelIndex &index, int role) const
{
QObject*obj;
if(!index.isValid())
obj = root;
else
obj = reinterpret_cast<QObject*>(index.internalPointer());
if(role == Qt::DisplayRole)
{
if(index.column() == 0 && QVariant(obj->metaObject()->className()).toString() == "Base")
{
Base*b = dynamic_cast<Base*>(obj);
return b->str;
}
if(index.column() == 1 && QVariant(obj->metaObject()->className()).toString() == "Station")
{
Station*s = dynamic_cast<Station*>(obj);
return s->str;
}
else if(index.column() == 2 && QVariant(obj->metaObject()->className()).toString() == "Device")
{
Device*d = dynamic_cast<Device*>(obj);
return d->str;
}
else if(index.column() == 3)
return "0x"+ QString::number((long)(obj), 16) + " " + obj->metaObject()->className();
}
else if(role == Qt::DecorationRole)
{
if(index.column() == 0 && QVariant(obj->metaObject()->className()).toString() == "Base")
{
static QIcon icon("D:/images/Database.png");
return QVariant::fromValue<QIcon>(icon);
}
else if(index.column() == 1 && QVariant(obj->metaObject()->className()).toString() == "Station")
{
static QIcon icon("D:/images/networkee.png");
return QVariant::fromValue<QIcon>(icon);
}
else if(index.column() == 2 && QVariant(obj->metaObject()->className()).toString() == "Device")
{
static QIcon icon("D:/images/device.png");
return QVariant::fromValue<QIcon>(icon);
}
}
return QVariant();
}
C++ (Qt)
QVariant TreeModelBrowser::data(const QModelIndex &index, int role) const
{
QObject*obj;
if(!index.isValid())
obj = root;
else
obj = reinterpret_cast<QObject*>(index.internalPointer());
if(role == Qt::DisplayRole)
{
if(index.column() == 0 && QVariant(obj->metaObject()->className()).toString() == "Station")
{
Station*s = dynamic_cast<Station*>(obj);
return s->str;
}
if(index.column() == 0 && QVariant(obj->metaObject()->className()).toString() == "Device")
{
Device*d = dynamic_cast<Device*>(obj);
return d->str;
}
else if (index.column() == 1)
return obj->objectName();
else if(index.column() == 2)
return "0x"+ QString::number((long)(obj), 16);
}
else if(role == Qt::DecorationRole)
{
if(index.column() == 0 && QVariant(obj->metaObject()->className()).toString() == "Station")
{
static QIcon icon("D:/images/networkee.png");
return QVariant::fromValue<QIcon>(icon);
}
if(index.column() == 0 && QVariant(obj->metaObject()->className()).toString() == "Device")
{
static QIcon icon("D:/images/device.png");
return QVariant::fromValue<QIcon>(icon);
}
}
return QVariant();
}
C++ (Qt)
rootModel*rm = new rootModel;
Base *base = new Base(rm);
Station *st1 = new Station(base);
base->stationList.append(st1);
Station *st2 = new Station(base);
base->stationList.append(st2);
Device *dev1 = new Device(st1);
st1->deviceList.append(dev1);
Device *dev2 = new Device(st1);
st1->deviceList.append(dev2);
Device *dev3 = new Device(st2);
st2->deviceList.append(dev3);
QTreeView*Tview = new QTreeView;
connect(Tview,SIGNAL(clicked(QModelIndex)),this,SLOT(sl_clicked_Tview(QModelIndex)));
Tview->resize(400,600);
Tview->show();
ObjectBrowserModel*model = new ObjectBrowserModel(rm);
Tview->setModel(model);
Tview->setEditTriggers(QAbstractItemView::DoubleClicked );
Tview->setItemDelegate(mydelegate);
QTreeView*Tview1 = new QTreeView;
Tview1->resize(400,600);
Tview1->show();
Tmodel = new TreeModelBrowser(rm);
Tview1->setModel(Tmodel);
Почему в окне ObjectModelBrowser отображается элемент TreeModelBrowser?
Все понял
C++ (Qt)
Tmodel = new TreeModelBrowser(parent);
Tview1->setModel(Tmodel);
Он отображаетcя как дочерний объект
Извините за тупой вопрос :)