1

I have a QTableWidget with four columns. I want the user to be able to insert only integers in the first three and a double in the fourth.

I believe that this can be done with delegate, but I have not found relevant examples (only some with QDateTimeEdit).

What should I do?

Mel
  • 5,837
  • 10
  • 37
  • 42
Giuseppe
  • 625
  • 1
  • 9
  • 15

1 Answers1

3

Look at the documentation for QItemDelegate; it provides a pretty good description on how it can be used.

Since with a delegate, you'll be able to provide your own custom editor, I would suggest that you use a QLineEdit with a validator set using setValidator(). I believe the classes QIntValidator and QDoubleValidator will be perfect in this situation.

swongu
  • 2,229
  • 3
  • 19
  • 24
  • Thanks. One question only: how do I set the validator for a column? I did it for a QLineEdit, but never for columns. – Giuseppe Nov 05 '09 at 20:22
  • The validator is set onto the editor `QLineEdit`. Since the delegate handles creating editors for cells, you can set different delegates for different columns - just use `QAbstractItemView::setItemDelegateForColumn()`. – swongu Nov 06 '09 at 00:58
  • 2
    I would suggest the possibility of using a spin box and double spin box for the delegate editors instead. They can still be typed in, already have the validators, and additionally allow the user to quickly increment/decrement the value. – Caleb Huitt - cjhuitt Nov 06 '09 at 15:02