Patrick
Patrick

Reputation: 2544

How to set the column text format of QTableWidget?

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.

Upvotes: 2

Views: 3329

Answers (1)

László Papp
László Papp

Reputation: 53225

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);

Upvotes: 3

Related Questions