[英]QTableWidgetItem text() property during editing
我對 QTableWidget 或 QTableWidgetItem 有以下問題:我想在編輯/鍵入期間分析單元格中的文本,例如作為對 KeyReleaseEvent 的反應。
但是, QTableWidgetItem::text() 屬性僅在單元格編輯完成后(焦點已離開單元格)才會更改。
我怎樣才能克服這種行為? 當然,可以分析 KeyReleaseEvent 中的按鈕鍵,但是使用 text() 屬性會容易得多......
一種可能的解決方案是通過委托建立一個自定義 QLineEdit 作為編輯器:
#include <QtWidgets>
class LineEdit: public QLineEdit{
public:
using QLineEdit::QLineEdit;
protected:
void keyReleaseEvent(QKeyEvent *event) {
QLineEdit::keyPressEvent(event);
qDebug() << text();
}
};
class StyledItemDelegate: public QStyledItemDelegate{
public:
using QStyledItemDelegate::QStyledItemDelegate;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const{
LineEdit *editor = new LineEdit(parent);
return editor;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QTableWidget w(10, 10);
w.setItemDelegate(new StyledItemDelegate(&w));
w.show();
return a.exec();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.