QTableView with QStandardItemModel: How to perform live updates during editing a cell?

假如想象 提交于 2019-12-25 17:10:13

问题


Recently, I made the switch to QT. It has taken some time, but I am starting to find my way around. However, one issue remains:

I want to port a program, that responds to every key press while editing a cell in a table view (QTableView with QStandardItemModel). The idea is to show and update a list of possibilities on a separate form while the user is entering a text in a table view's cell. After every key stroke, the list needs to be updated according to the current text in the edit field of some cell.

Using QTableView::installEventFilter and QEvent::KeyPress, I can get every key press while the table view is in focus, but the cell text / model is only updated after editing, which prohibits live updates of the list.

The model's dataChanged signal is only emitted after editing has finished and not during the user's input.

Any ideas on how to solve this? Should I use a QItemDelegate? Or should a QLineEdit be connected to a cell somehow and can this be done without it visually being apparent, so the user still appears to be working directly inside a cell?

Thank you for any help


回答1:


It works (that is, until the main window is resized...)

Perhaps not the best solution, but at least i found a way to make it work. I put the slot in the source file that contains the main window, because that is what I have always been used to (C++ Builder)...

GLiveEdit.H - Subclass QStyledItemDelegate

#ifndef GLIVEEDIT_H
#define GLIVEEDIT_H

#include <QStyledItemDelegate>


class GLiveEdit : public QStyledItemDelegate {

  public:
    GLiveEdit (QObject *_ParentWindow = 0, const char *_Slot = 0);

  protected:
    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;


  protected:
    mutable QLineEdit *Editor;
    const char *Slot;
    QWidget *ParentWindow;
};

#endif // GLIVEEDIT_H

GLiveEdit.CPP

#include "gliveedit.h"
#include <QLineEdit>

GLiveEdit::GLiveEdit (QObject *_ParentWindow, const char *_Slot)
  : QStyledItemDelegate (_ParentWindow)

{
  Editor = 0;
  Slot = _Slot;
  ParentWindow = (QWidget *) _ParentWindow;
}


QWidget *GLiveEdit::createEditor(QWidget *parent,
                                    const QStyleOptionViewItem &option,
                                    const QModelIndex &index) const
{
  Editor = (QLineEdit *) QStyledItemDelegate::createEditor (parent, option, index);
  connect (Editor, SIGNAL (textChanged (const QString &)), ParentWindow, Slot);
  return Editor;
}

MainWindow.CPP - Install Subclassed QStyledItemDelegate ("GLiveEdit")

void MainWindow::SetUp()

{
  // Instantiate subclassed QStyledItemDelegate "GLiveEdit" as "Live"
  // Also pass the slot "OnEditChanged" that is to be called during editing
  Live = new GLiveEdit (this, SLOT (OnEditChanged (const QString &)));

  // Tell the table about the instantiated subclassed delegate "Live"
  ui->tvOverview->setItemDelegate (Live);
}


void MainWindow::OnEditChanged (const QString &NewText)

{
  // NewText contains the up to date text that is currently being edited
}

Anyone any ideas on having it also work after the window has been resized? Calling the SetUp function from within QMainWindow::resizeEvent does not seem to work, unfortunately.

Also, I suppose QTableView deletes the item delegate from memory itself?

Edit: The item delegate only stops working if the window is resized during the editing... It keeps functioning if the edit is finished first.




回答2:


It works! Problems after resizing were due to a bug of mine :-) (located elsewhere in the source code) Any suggestions for improvement still welcome!



来源:https://stackoverflow.com/questions/45264097/qtableview-with-qstandarditemmodel-how-to-perform-live-updates-during-editing-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!