简体   繁体   中英

QT C++ QTableWidget(Item) partially italicize (emphasize) text

Problem

I'm using QTableWidget (Item) for presentation of some data. For that i need to italicize or set bold some parts of text. Problem: I don't want to italicize the whole cell/box, just parts of text in a cell.

Ideas

: Using the QTableWidgetItem refering to the correspondent cell and trying to modify the style (eg setFont(font)). But this operations always seem to alter the whole cell?

I also found this which doesn't help me for my QTableWidget problem.

Wanted

: To Have a QTableWidget with eg 2 rows and collumns containing cells like:

Exa mple Cell Text

(Nice Notation like Exa<b>mple</b> Cell <i>Text</i> is only a secondary aspect)

Note: Example should be one word.

Solved

I didn't see the wood for the trees. Thanks to Salvatore Avanzo, he is totally right.

All Item Widget classes provide functionality to render/paint something custom in each cell.

QTableWidget provides setCellWidget(.. QWidget..) ; QListWidget and QTreeWidget provide setItemWidget(..QWidget..) . Therefore you can use any Subclass of QWidget (base of all ui objects) to paint something custom. For my aim i needed the capability to set a custom partially emphasized/italicized text. The Refering Subclass of QWidget for painting texts is QLabel (It itself is also a subclass of QPainDevice). A QWidget/Subclass of QWidget is drawed when paintEvent(..) is invoked (QLabel reimplemented that). QLabel also supports RichText functionality (flag is used by default), eg you can use <b>foo</b> to get foo .

So that's perfectly fits my problem and i will now use this solution.

Forecast for better structured solution

The unattractive aspect of the above mentioned solution is, that design and data get totally mixed. Editing Text in a QLabel is possible if you set the refering Interaction tags. But then you always need to use QLabel to get the text. When additionally setting an QTableWidgetItem in the Cell which also uses QLabel, they overlay ugly. So one could use QTableView. Here is a link .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM