Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListView in IconMode with custom QStyledItemDelegate. Editor implementation not working.
QtWS25 Last Chance

QListView in IconMode with custom QStyledItemDelegate. Editor implementation not working.

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 653 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kevfu
    wrote on last edited by
    #1

    Hello!

    I have overridden setEditorData, createEditor, updateEditoryGeometry, and setModelData and have my own editor (essentially just a QLineEdit) but I have 2 problems:

    • the commitData signal is getting emitted immediately after I call listView.edit(modelIndex);

    • the editor is not visibly getting painted on screen anywhere. I was thinking it might paint it at 0,0 of the edited items tile in the QListView but I don't see it.

    I am trying to make a new directory, have it render in the listview then have the focus directed to the editor's QLineEdit so that they can rename it. Then when the rename is done the directory is saved to my database and the view can refresh based on a QSqlQuery.

    Any help very very much appreciated.

    QWidget *GridItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        qDebug() << Q_FUNC_INFO;
    
        TileEditor *editor = new TileEditor(parent);
        //QLineEdit *editor = new QLineEdit(parent);
        connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
        return editor;
    }
    
    void GridItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        qDebug() << Q_FUNC_INFO;
    
        TileEditor *tileEditor = qobject_cast<TileEditor *>(editor);
        //QLineEdit *tileEditor = qobject_cast<QLineEdit *>(editor);
        //tileEditor->setText("YOYOYO");
        TileContent tc = qvariant_cast<TileContent>(index.data());
        tileEditor->setLine1Text(index.sibling(index.row(), 1).data().toString());
    
    }
    
    void GridItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        qDebug() << Q_FUNC_INFO;
    
        TileEditor *tileEditor = qobject_cast<TileEditor *>(editor);
        model->setData(index.sibling(index.row(), 1), tileEditor->getLine1Text());
    }
    
    void GridItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
    {
        qDebug() << "Update geometry: " << option.rect;
        editor->setGeometry(option.rect);
    }
    
    
    void GridItemDelegate::commitAndCloseEditor()
    {
        qDebug() << Q_FUNC_INFO;
    
        TileEditor *tileEditor = qobject_cast<TileEditor *>(sender());
        emit doneEditing(tileEditor->getLine1Text());
        emit commitData(tileEditor);
        emit closeEditor(tileEditor);
    }```
    
    Thank you,
    k
    1 Reply Last reply
    0
    • giupignaG Offline
      giupignaG Offline
      giupigna
      wrote on last edited by
      #2

      Try to do this:

      QWidget *GridItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
      {
          qDebug() << Q_FUNC_INFO;
      if (!index.isvalid())
          return QStyledItemDelegate::createEditor(parent, option, index);
      
      return
          TileEditor *editor = new TileEditor(parent);
          //QLineEdit *editor = new QLineEdit(parent);
          connect(editor, SIGNAL(editingFinished()), this, SLOT(commitAndCloseEditor()));
          return editor;
      }
      
      void GridItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
      {
          qDebug() << Q_FUNC_INFO;
          if (!index.isvalid())
              return QStyledItemDelegate::setEditorData(editor, index);
          
          TileEditor *tileEditor = qobject_cast<TileEditor *>(editor);
          //QLineEdit *tileEditor = qobject_cast<QLineEdit *>(editor);
          //tileEditor->setText("YOYOYO");
          TileContent tc = qvariant_cast<TileContent>(index.data());
          tileEditor->setLine1Text(index.sibling(index.row(), 1).data().toString());
      
      }
      
      void GridItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
      {
          qDebug() << Q_FUNC_INFO;
          if (!index.isvalid())
              return QStyledItemDelegate::setModelData(editor, model, index);
          
          TileEditor *tileEditor = qobject_cast<TileEditor *>(editor);
          model->setData(index.sibling(index.row(), 1), tileEditor->getLine1Text());
      }
      
      void GridItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
      {
          qDebug() << "Update geometry: " << option.rect;
          if (!index.isvalid())
              return QStyledItemDelegate::updateEditorGeometry(editor, option, index);
          editor->setGeometry(option.rect);
      }
      
      
      void GridItemDelegate::commitAndCloseEditor()
      {
          qDebug() << Q_FUNC_INFO;
      
          TileEditor *tileEditor = qobject_cast<TileEditor *>(sender());
          emit doneEditing(tileEditor->getLine1Text());
          emit commitData(tileEditor);
          emit closeEditor(tileEditor);
      }
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved