'Qt: Item Delegate With Different Height And Width

I'm currently working with listview and delegates. For better understanding, please look at the illustration below.

enter image description here

I'm trying to recreate this by using the paint method of the delegate, but I'm stuck at how to size the rectangles in accordance to the texts. The shorter the text is, the rectangle's width will become smaller and vice versa. The same applies with the text's number of lines.

The texts are retrieved from MYSQL database. How do I input the data to the DB? I'm using QTextEdit for that and pass the content (textEdit->toPlainText()) to the DB. In short, it's a QString. However, here comes another problem. I don't know how to determine the number of lines the QString has and if the text is split into three lines as in rectangle 3 (the one which says "Hello Oh Yeah! Great!"), how can I determine the longest sentence/phrase as the point of reference for the rectangle width to be calculated later on?

BTW, I'm still new to Qt's delegates and stuff, so I sincerely ask for the earnest guidance. Thanks.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source