-1

I'm just getting started with QT, so please exercise a little patience... I've an editable QTableWidget (actually a subclassing), and need to implement the following behavior. When the user types a non acceptable value I would like: 1) to restore the original value; 2) to keep the focus in the cell and set it in edit mode.

I'm currently using the itemChanged SIGNAL, and a subclassing of QTableWidgetItem. Which one is the best way to get what I need? Any tip, suggestion or reference is really welcome.

If you think it as useful I can post some code.

Ciao Alf.

Alfredo
  • 5
  • 4
  • Possible duplicate of [How to use a validator with QTableWidgetItem?](http://stackoverflow.com/questions/18309715/how-to-use-a-validator-with-qtablewidgetitem) – Aurel Branzeanu Jan 12 '16 at 22:18

1 Answers1

0

I'm currently using the itemChanged SIGNAL...

You should subclass QStyledItemDelegate

class CustomTableDelegate : public QStyledItemDelegate
{
    Q_OBJECT

    public:
        CustomTableDelegate (QObject * parent = 0);
        QWidget * createEditor (QWidget *, const QStyleOptionViewItem &, const QModelIndex &) const;
        bool editorEvent (QEvent *, QAbstractItemModel *, const QStyleOptionViewItem &, const QModelIndex &);
        void setEditorData (QWidget *, const QModelIndex &) const;
        void setModelData  (QWidget *, QAbstractItemModel *, const QModelIndex &) const;
};

And implement validation inside setModelData.

To use custom delegate you need set it for your QTableWidget:

table->setItemDelegate (new CustomTableDelegate () );
Gluttton
  • 5,739
  • 3
  • 31
  • 58