PDA

View Full Version : Complex text orientation in a QTableWidget cell



Binary91
4th July 2015, 10:24
Hi,

I'm trying to create a custom calendar based on a QTableWidget.
My Problem is, that I need to display text within the cells in different corners. The day number (1 - 31) should be displayed in the top left corner of the cell. That works fine using Qt Alignment to the QTableWidgetItems.
But now I'd also like to add some text in the center of the cells and two or three icons in the top right corner, vertically orientated.

I know, that I can't do this with pure QTableWidgetItems, so I thought of a custom QWidget that holds a separate QTableWidget, so I have maximum options to position my stuff into the cell.

Question 1:
Is that the right way to solve my problem? Maybe using many QWidgets/QTableWidgets inside a QTablewidget needs too much memory and results in bad performance?

Question 2:
Qt documentation says, QItemDelegate is a better way for displaying custom text/edit data inside cells. Can I use QItemDelegate for my purposes and if yes, should I prefer it?

Thank you in anticipation!
Binary

anda_skoa
4th July 2015, 11:39
Qt documentation says, QItemDelegate is a better way for displaying custom text/edit data inside cells. Can I use QItemDelegate for my purposes and if yes, should I prefer it?

Yes and yes.

Cheers,
_

Binary91
5th July 2015, 22:09
Hi anda_skoa,

thank you for your answer. Well, honestly I don't understand QItemDelegate clearly. Can I use it like a QWidget as a container for other widgets?
Is it possible for me to subclass QItemDelegate, give it a QGridLayout and position other stuff like I want to do it?

anda_skoa
6th July 2015, 08:18
thank you for your answer. Well, honestly I don't understand QItemDelegate clearly. Can I use it like a QWidget as a container for other widgets?

No, it is not a widget or a widget container.
It is an interface that a view can delegate drawing of cells to.

A delegate allows for fully customized drawing of cells, the view will ask it for cell dimension and at some point call its paint method.

Drawing multiple texts into different places is just a matter of calling QPainter's drawText() method multiple times.
Drawing icons is just a matter of calling QPainter's drawImage() or drawPixmap() methods.

Cheers,
_

Binary91
6th July 2015, 19:54
Hi again,

thank you. I will take a look at it later, but that seems to be clear...

Cheers,