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

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

Страниц: [1]   Вниз
  Печать  
Автор Тема: QItemDelegate обновления данных  (Прочитано 3498 раз)
CJ1
Гость
« : Октябрь 22, 2012, 18:15 »

Вот содал я QItemDelegate QCombobox
Работает.

Но как изменить данные Combobox?


Код
C++ (Qt)
ComboboxDelegate::ComboboxDelegate(QStringList & stringList, QObject *parent)
    : QItemDelegate(parent)
{
   this->stringList = stringList;
}
 
 
 
QWidget *ComboboxDelegate::createEditor(QWidget *parent,
   const QStyleOptionViewItem & /*option */,
   const QModelIndex &/* index */) const
{
   QComboBox *editor = new QComboBox(parent);
   this->combobox = editor;
   editor->addItems(this->stringList);
   return editor;
}
 
void ComboboxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
   QString value = index.model()->data(index, Qt::EditRole).toString();
   QComboBox *Combobox = static_cast<QComboBox*>(editor);
   int res = Combobox->findText(value);
   if (res == -1) res = 0;
   Combobox->setCurrentIndex(res );
}
 
void ComboboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                  const QModelIndex &index) const
{
   QComboBox *Combobox = static_cast<QComboBox*>(editor);
   model->setData(index, Combobox->currentText(), Qt::EditRole);
}
 
 
void ComboboxDelegate::updateEditorGeometry(QWidget *editor,
   const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
   editor->setGeometry(option.rect);
}
 
 
Записан
CJ1
Гость
« Ответ #1 : Октябрь 22, 2012, 18:26 »

Только конченный дебил мог придумать поставить тут const
Записан
mutineer
Гость
« Ответ #2 : Октябрь 22, 2012, 18:28 »

где тут?
Записан
CJ1
Гость
« Ответ #3 : Октябрь 22, 2012, 18:30 »

void ComboboxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
Записан
mutineer
Гость
« Ответ #4 : Октябрь 22, 2012, 18:31 »

Что не так с конст в этом месте?
Записан
CJ1
Гость
« Ответ #5 : Октябрь 22, 2012, 18:44 »

Не дает чтото изменить


Код
C++ (Qt)
class ComboboxDelegate : public QItemDelegate
{
    Q_OBJECT
 
 
 
public:
    ComboboxDelegate(QStringList &stringList, QObject *parent = 0);
 
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                          const QModelIndex &index) const;
 
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model,
                      const QModelIndex &index) const;
 
    void updateEditorGeometry(QWidget *editor,
        const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void reset(QStringList &stringList);
 
 
 
private:
    bool reset_data;
    QStringList stringList;
 
};

Видешь тут  reset_data

так вот пишу
Код
C++ (Qt)
void ComboboxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
 
   QString value = index.model()->data(index, Qt::EditRole).toString();
   QComboBox *Combobox = static_cast<QComboBox*>(editor);
 
   int res = Combobox->findText(value);
   if (res == -1) res = 0;
   Combobox->setCurrentIndex(res );
 
   if (reset_data)
   {
       Combobox->clear();
       Combobox->addItems(stringList);
       reset_data = false; // Вот тут
   }
}
 

Все не компилится так как из за const нельзя менять данные!
Записан
kambala
Джедай : наставник для всех
*******
Offline Offline

Сообщений: 4744



Просмотр профиля WWW
« Ответ #6 : Октябрь 22, 2012, 18:48 »

если позарез нужно изменять переменную внутри конст метода, то используй mutable, хотя скорее всего в твоём контексте этот подход неправильный
Записан

Изучением C++ вымощена дорога в Qt.

UTF-8 has been around since 1993 and Unicode 2.0 since 1996; if you have created any 8-bit character content since 1996 in anything other than UTF-8, then I hate you. © Matt Gallagher
mutineer
Гость
« Ответ #7 : Октябрь 22, 2012, 18:49 »

Если у тебя настолько острое желание менять это поле в этом методе, то используй mutable
Записан
CJ1
Гость
« Ответ #8 : Октябрь 22, 2012, 18:50 »

kambala как вообще изменить данные комбобокса после его создания?
Записан
CJ1
Гость
« Ответ #9 : Октябрь 22, 2012, 18:54 »

Спасибо! Не знал о существовании mutable
И заодно еще о многом узнал!
Записан
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  


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