Russian Qt Forum
Октябрь 02, 2024, 13:28 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: 1 [2]   Вниз
  Печать  
Автор Тема: [Решено]Обновление модели  (Прочитано 10003 раз)
demaker
Птица говорун
*****
Offline Offline

Сообщений: 960


Просмотр профиля
« Ответ #15 : Сентябрь 17, 2013, 13:57 »

Сделал вот так
Код
C++ (Qt)
void TreeModelBrowser::updateModel1(QModelIndex index)
{
   int row =  rowCount(index);
   row++;
   beginInsertRows(index,row,row);
   CommonElement *ce = reinterpret_cast<CommonElement*>(index.internalPointer());
   ce->appendChild();
   endInsertRows();
}
 

но тот же эффект... Непонимающий
Записан
mutineer
Гость
« Ответ #16 : Сентябрь 17, 2013, 14:02 »

наверно внутри ce->appendChild(); скрыта какая-то магия, недоступная нам
Записан
demaker
Птица говорун
*****
Offline Offline

Сообщений: 960


Просмотр профиля
« Ответ #17 : Сентябрь 17, 2013, 14:03 »

наверно внутри ce->appendChild(); скрыта какая-то магия, недоступная нам

оч смешно
Записан
mutineer
Гость
« Ответ #18 : Сентябрь 17, 2013, 14:03 »

смешно то, что ты почему-то думаешь, что мы все знаем что же происходит внутри ce->appendChild();

Ну и reinterpret_cast это очень опасно в таком случае, потому что никаких проверок при этом не происходит
« Последнее редактирование: Сентябрь 17, 2013, 14:07 от mutineer » Записан
demaker
Птица говорун
*****
Offline Offline

Сообщений: 960


Просмотр профиля
« Ответ #19 : Сентябрь 17, 2013, 14:11 »

Код:
class CommonElement: public QObject
{
    Q_OBJECT

public:
    explicit CommonElement(QObject*parent = 0);
    virtual ~CommonElement();

    virtual CommonElement* appendChild() = 0;
    virtual void removeChild(CommonElement*) = 0;
    virtual void function() = 0;

public:
    QString str;
    QList<CommonElement*> listChildren;
};



Код:
class Station: public CommonElement
{
    Q_OBJECT

public:
    explicit Station(CommonElement *parent = 0);
    virtual ~Station();

    CommonElement* appendChild();
    void removeChild(CommonElement *);
    void function();
};




Station::Station(CommonElement *parent):
  CommonElement(parent)
{
    str = "MyStantion";
}

Station::~Station()
{
}

CommonElement* Station::appendChild()
{
    Device *device = new Device(this);
    listChildren.append(device);
    return device;
}

void Station::removeChild(CommonElement *device)
{
    listChildren.removeOne(device);
    delete device;
}

void Station::function()
{
    qDebug()<<"Station->str = " + str;
}


« Последнее редактирование: Сентябрь 17, 2013, 14:13 от demaker » Записан
mutineer
Гость
« Ответ #20 : Сентябрь 17, 2013, 14:26 »

С использованием resetModel у тебя это вот добавление работает?
Записан
demaker
Птица говорун
*****
Offline Offline

Сообщений: 960


Просмотр профиля
« Ответ #21 : Сентябрь 17, 2013, 14:32 »

Да
Записан
mutineer
Гость
« Ответ #22 : Сентябрь 17, 2013, 14:34 »

Покажи код, который добавляет элемент и выполняет resetModel
Записан
demaker
Птица говорун
*****
Offline Offline

Сообщений: 960


Просмотр профиля
« Ответ #23 : Сентябрь 17, 2013, 14:44 »

Покажи код, который добавляет элемент и выполняет resetModel

Код
C++ (Qt)
...
connect(treeview,SIGNAL(clicked(QModelIndex)),this,SLOT(clickedTreeView(QModelIndex)));
connect(this,SIGNAL(appendChild(QModelIndex)),treemodel,SLOT(updateModel(QModelIndex)));
...
void MainWindow::appendElement()
{
   qDebug()<<"appendElement";
   if(currentElement != NULL)
   {
       Q_EMIT appendChild(currentModelIndex);
   }
}
 
void MainWindow::clickedTreeView(QModelIndex index)
{
   if(index.isValid())
   {
       currentModelIndex = index;
       currentElement = reinterpret_cast<CommonElement*>(currentModelIndex.internalPointer());
       listmodel->setRoot(currentElement);
       setPathLabel(currentElement);
   }
}
 
 

Код
C++ (Qt)
void TreeModelBrowser::updateModel(QModelIndex index)
{
   if(index.isValid())
   {    
       beginResetModel();
       CommonElement *ce = reinterpret_cast<CommonElement*>(index.internalPointer());
       ce->appendChild();
       endResetModel();
   }
}
 
« Последнее редактирование: Сентябрь 17, 2013, 16:16 от demaker » Записан
demaker
Птица говорун
*****
Offline Offline

Сообщений: 960


Просмотр профиля
« Ответ #24 : Сентябрь 18, 2013, 10:53 »

Как код? Правильный или нет???
« Последнее редактирование: Сентябрь 19, 2013, 11:15 от demaker » Записан
mutineer
Гость
« Ответ #25 : Сентябрь 18, 2013, 10:59 »

Если добавление работает корректно, то по идее достаточно заменить beginResetModel()/endResetModel() на beginInsertRows()/endInsertRows()
Записан
Страниц: 1 [2]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.176 секунд. Запросов: 22.