它可以工作(也就是说,直到调整主窗口的大小......)
也许不是最好的解决方案,但至少我找到了让它发挥作用的方法。
我把槽放在包含主窗口的源文件中,因为这是我一直习惯的(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 会从内存中删除项目委托?
编辑:只有在编辑过程中调整窗口大小时,项目委托才会停止工作......如果首先完成编辑,它会继续工作。