【问题标题】:QTableView with QStandardItemModel: How to perform live updates during editing a cell?QTableView 与 QStandardItemModel:如何在编辑单元格期间执行实时更新?
【发布时间】:2017-07-23 10:33:51
【问题描述】:

最近,我切换到 QT。这花了一些时间,但我开始找到自己的方式。但是,仍然存在一个问题:

我想移植一个程序,该程序在编辑表格视图中的单元格时响应每次按键操作(带有 QStandardItemModel 的 QTableView)。这个想法是在用户在表格视图的单元格中输入文本时,在单独的表单上显示和更新可能性列表。每次击键后,列表需要根据某个单元格的编辑字段中的当前文本进行更新。

使用 QTableView::installEventFilter 和 QEvent::KeyPress,我可以在表格视图处于焦点时获得每次按键,但单元格文本/模型仅在编辑后更新,这禁止实时更新列表。

模型的 dataChanged 信号仅在编辑完成后发出,而不是在用户输入期间发出。

关于如何解决这个问题的任何想法? 我应该使用 QItemDelegate 吗? 或者 QLineEdit 是否应该以某种方式连接到单元格,并且可以在视觉上不明显的情况下完成此操作,因此用户似乎仍然直接在单元格内工作?

感谢您的帮助

【问题讨论】:

    标签: qt events signals qtableview qabstractitemmodel


    【解决方案1】:

    它可以工作(也就是说,直到调整主窗口的大小......)

    也许不是最好的解决方案,但至少我找到了让它发挥作用的方法。 我把槽放在包含主窗口的源文件中,因为这是我一直习惯的(C++ Builder)...

    GLiveEdit.H - 子类 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 - 安装子类 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
    }
    

    在调整窗口大小后,任何人有任何想法让它也可以工作吗? 不幸的是,从 QMainWindow::resizeEvent 中调用 SetUp 函数似乎不起作用。

    另外,我想 QTableView 会从内存中删除项目委托?

    编辑:只有在编辑过程中调整窗口大小时,项目委托才会停止工作......如果首先完成编辑,它会继续工作。

    【讨论】:

      【解决方案2】:

      有效!调整大小后的问题是由于我的一个错误:-)(位于源代码的其他地方) 任何改进建议仍然欢迎!

      【讨论】:

        猜你喜欢
        • 2012-01-18
        • 1970-01-01
        • 1970-01-01
        • 2012-12-05
        • 1970-01-01
        • 1970-01-01
        • 2021-10-10
        • 1970-01-01
        • 2017-01-10
        相关资源
        最近更新 更多