2

I want to show some data in table form. I took QTableWidget for it having multiple columns. One column of it will contain time(hh:mm format).

I also want user to edit any item of table but with corresponding format.

I was able to add data in QTableWidget but i couldn't set text format of time column.

This i want to achieve so that user can edit time only in hh:mm format.

If possible please write your answer code in python.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Patrick
  • 2,464
  • 3
  • 30
  • 47
  • Are you using [QDateTimeEdit](http://doc-snapshot.qt-project.org/qt5-5.4/qdatetimeedit.html)? Just set the display format if so. – László Papp Dec 07 '14 at 13:56
  • @lpapp Actually i have added `QDateTimeEdit` before this table where user sets the time and it gets added in `QTableWidget` but in this table i want to allow user to edit this *time data* but with same format of `QDateTimeEdit` – Patrick Dec 07 '14 at 14:07
  • @lpapp I have to maintain a list of data in Table form. Data includes name, time, day, temperature etc. User will configure those using corresponding widgets and then on clicking a button those will get added to the Table. And he can add mutliple datas – Patrick Dec 07 '14 at 14:15
  • @lpapp Both comments convey same thing. In First comment i talked of only time widget and in second comment i told you about all contents of data – Patrick Dec 07 '14 at 14:20

1 Answers1

2

Since you want the user enter time date, I would suggest to reuse the already existing QDateTimeEdit class in the following way:

dateTime = QDateTimeEdit();
dateTime.setDisplayFormat("hh:mm");
dateTime.setFrame(False);
myTableWidget.setCellWidget(row, column, dateTime);

The user will be able to edit the "time data" this way in your table widget. Moreover, it will be also convenient due to the steps that can be applied.

If you really insist on reinventing this yourself, you can use a QLineEdit with custom validator againt the desired hh::mm format.

dateTime = QLineEdit();
dateTime.setValidator(QRegExpValidator(QRegExp("^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$")));
myTableWidget.setCellWidget(row, column, dateTime);
Patrick
  • 2,464
  • 3
  • 30
  • 47
László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Just to inform you that i want time in `QTW` not `date time`. By mistake i wrote QDTE in comment – Patrick Dec 07 '14 at 14:32
  • First code worked fine but second one gives error: `dateTime.setValidator(QRegExpValidator("^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$")) TypeError: 'PySide.QtGui.QRegExpValidator' called with wrong argument types: PySide.QtGui.QRegExpValidator(str)` – Patrick Dec 07 '14 at 14:48
  • @Patrick: Try QRegExpValidator(QRegExp("...")) as they made the QRegExp ctor explicit. – László Papp Dec 07 '14 at 15:21