Bug 235126 - KWidgetItemDelegate problem when dynamically removing a row
Summary: KWidgetItemDelegate problem when dynamically removing a row
Status: RESOLVED DUPLICATE of bug 187820
Alias: None
Product: kdelibs
Classification: Frameworks and Libraries
Component: kdeui (show other bugs)
Version: 4.4
Platform: openSUSE Linux
: NOR normal
Target Milestone: ---
Assignee: kdelibs bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-04-23 00:44 UTC by Damien Tardy-Panis
Modified: 2010-04-23 01:02 UTC (History)
0 users

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Damien Tardy-Panis 2010-04-23 00:44:51 UTC
Version:            (using KDE 4.4.2)
Compiler:          gcc 4.4.1 
OS:                Linux
Installed from:    openSUSE RPMs

I tried to subclass KWidgetItemDelegate (using model/view programming) for one of my project and I found a bug with that class (or maybe I'm not using it the right way...).

I would like to remove dynamically a row by clicking on a button located in that row, but there is a problem with the view which is not updated correctly :
rows are updated, but widgets I added with kwidgetItemDelegate::createItemsWidget() no.

You can try this minimalist code (adapted from kwidgetitemdelegatetest)

#include <QtGui/QApplication>
#include <QtGui/QAbstractItemView>
#include <QtGui/QListView>
#include <QtGui/QStringListModel>
#include <QtGui/QMainWindow>
#include <QtGui/QPainter>
#include <kapplication.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <kpushbutton.h>
#include <kwidgetitemdelegate.h>

class MyDelegate
  : public KWidgetItemDelegate
{
  Q_OBJECT

  public:
  MyDelegate(QAbstractItemView *itemView, QObject *parent = 0)
    : KWidgetItemDelegate(itemView, parent)
  {
    m_model = static_cast<QStringListModel*>(itemView->model());
    m_listView = static_cast<QListView*>(itemView);
  }

  QSize sizeHint(const QStyleOptionViewItem &option,
       const QModelIndex &index) const
  {
    Q_UNUSED(option);
    Q_UNUSED(index);

    return sizeHint();
  }

  QSize sizeHint() const
  {
    return QSize(150, 40);
  }

  void paint(QPainter *painter, const QStyleOptionViewItem &option,
        const QModelIndex &index) const
  {
    Q_UNUSED(index);

    painter->save();

    itemView()->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, 0);

    if (option.state & QStyle::State_Selected)
      painter->setPen(option.palette.highlightedText().color());

    painter->restore();
  }

  QList<QWidget*> createItemWidgets() const
  {
    KPushButton *button = new KPushButton();
    connect(button, SIGNAL(clicked(bool)), this, SLOT(mySlot()));
    return QList<QWidget*>() << button;
  }

  void updateItemWidgets(const QList<QWidget*> widgets,
          const QStyleOptionViewItem &option,
          const QPersistentModelIndex &index) const
  {
    Q_UNUSED(option);
    Q_UNUSED(index);

    QPushButton *button = static_cast<QPushButton*>(widgets[0]);
    button->setText("Remove me");
    button->resize(100, 30);
    button->move(10, 5);
  }

private Q_SLOTS:
  void mySlot()
  {
    int row = m_listView->currentIndex().row();
    m_model->removeRow(row);
  }

private: 
  QStringListModel* m_model;
  QListView* m_listView;
};

int main(int argc, char **argv)
{
  KAboutData aboutData("goyaTest",
             0,
             ki18n("Goya Test"),
             "1.0",
             ki18n("Goya Test"),
             KAboutData::License_LGPL_V3,
             ki18n("(c) 2008 Rafael Fernández López and Kevin Ottens "),
             ki18n("Goya Test"),
             "http://www.kde.org");

  KCmdLineArgs::init( argc, argv, &aboutData );
  KApplication app;

  QMainWindow *mainWindow = new QMainWindow();
  mainWindow->setMinimumSize(200, 250);
  QListView *listView = new QListView();
  QStringListModel *model = new QStringListModel();

  model->insertRows(0, 5);
  listView->setModel(model);
  MyDelegate *myDelegate = new MyDelegate(listView);
  listView->setItemDelegate(myDelegate);

  mainWindow->setCentralWidget(listView);

  mainWindow->show();

  return app.exec();
}

#include "kwidgetitemdelegatetest.moc"
Comment 1 Christoph Feck 2010-04-23 01:02:34 UTC

*** This bug has been marked as a duplicate of bug 187820 ***