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

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 (e.g. 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 e.g. 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), e.g. 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.

Related

How can I create a QPushButton with a custom (triangular) shape in Qt 5.5?

How can I create a triangular pushbutton in Qt? What is the most simplest way of executing this? I use the designer to create buttons and not code.
Also, I read somewhere that shapes may be changed as long as the frame of the button is still rectangular but I want the frame to adjust according to the shape as well. How can I achieve this?
More detail: I want to place lots of small triangular buttons next to each other with every other triangle flipped. Each triangle button has it's own function, etc (no overlapping borders accepted). Can anyone give me a descriptive explanation for how I might go about this?
The geometry on a QWidget is always a rectangle.
It would be possible to create a QPushButton derivative, override its paintevent and do some nasty painting considering its neighborhood etc. but it would be really a pain...
it is much easier to use a QGraphicsView, QGraphicsScene and add appropriate QGraphicsItem (maybe the QGraphicsPolygonItem?), add them and use their signals/slots or create a derived class for your purposes.
It is not that hard to override the mouseevents to recognize clicks and you can even use the QStyleSheets to let the "button" look like it gets pressed.

Qt GUI: Select multiple QLabels with mouse

I would like to enable mouse-selection of the text of several QLabels arranged in a grid layout in a Qt GUI.
A QLabel has textInteractionFlags like TextSelectableByMouse which enables this behaviour for one object, but a selection across several QLabel widgets does not seem to work.
Is there a way around this that does not require a lot of mouse "tracking" or reimplementing a layout?
I fear there's no simple method to get what you want. The first problem would be what you'd expect to find in the paste buffer after selecting some rectangular section of your table. How should the label texts be delimited, should they be organized by row or colum?
You may say that you want them row-wise, columns separated by blanks and rows ending with a \n, but that doesn't need to be what the next person needs.
You may want to spend some time considering QTableView or QTableWidget.

Qt - dynamically add QLineEdit in a panel

this is a question for programming with Qt/C++. I have a combo box with two items. If current index for selection is 0, then no QLineEdit should be displayed in layout below the combo box. If it is 1, a QLineEdit should appear. It should disappear again if index is 0 again.
Notably, other elements in the layout should not be affected by the change. Values already entered by user in other QineEdit should remain in place.
Is it possible to dynamically modify widget? How did you procede?
Kind regards.
All QWidget objects have a function called hide().
You can attach a signal to the currentIndexChanged signal of the combo box, and in that function you implement whatever logic you have in mind and invoke the method hide of your QLineEdit.
The only problem with this approach is that a Qt Widget, when hidden, doesn't occupy any space on the screen, and this can lead to layout changes (depending on how you've programmed your layout, some other widgets can move a bit, for example). To prevent that you can make another Widget appear where the QLineEdit were (perhaps invoking the show() function, and placing the 'placeholder' on the same container that the LineEdit was), only to occupy its space and keep it there, or you can use a QStackedWidget add the two Widgets there and change its index.
I would recommend that you read the following example, it has some useful insight on dynamically changing things: Qt Extension Example.
Also, when in doubt, take a look in the other examples, they are really well documented and cover a lot of important topics on Qt.
Good luck with your code :)

QListWidget that resizes instead of scrolls

How do you change the behavior of a QListWidget so that it resizes its height instead of choosing a (seemingly arbitrary) height and adding scrollbars? See screenshot:
The QListView's should fill up as much space horizontally as they can (creating as many "columns," if you will.) Then they wrap and make as many rows as necessary to fit all the items. These calculations should be adjusted as the window is resized. This is all working fine.
However, what I want to happen is that instead of the height staying the same, the QListView should grow or shrink vertically and never need any scrollbars. The scrolling, if necessary, will be handled on the parent QWidget that hosts all of the labels and lists. It seems like once the height of the QListWidget is established (not sure where its default is coming from), it never changes. It is too big in some cases (see second "Test" list above) and too small in others (see first "blank maps" list above.)
The layout above is nothing surprising: two QLabel's and two QListWidget's in a QVBoxLayout. Here are the properties I have set on the QListWidget's:
setMovement(QListView::Static);
setResizeMode(QListView::Adjust);
setViewMode(QListView::IconMode);
setIconSize(QSize(128, 128));
(I already tried setting the horizontal and vertical scrollbar policies, but that just turns the scrollbars off, clipping the content. Not what I want.)
Maybe you could this without using QListWidget. The Qt's examples contain a new layout class, QFlowLayout, which could be useful. With the following kind of widget hierarchy you could get multiple groups with labels and they all would be inside one QScrollArea.
QScrollBox
QVBoxLayout
QLabel "Blank maps"
QWidget
QFlowLayout
your own widgets showing map images and labels
QLabel "Text"
QWidget
QFlowLayout
your own widgets
The problem is that this kind of solution would create much more widgets than QListWidget based solution. So if you have hundreds of items in your list, this might not be the best solution.
There is a protected member function called contentsSize() in QListView. It is used to calculate the required minimum(), maximum(), and pageStep() for the scrollbars (as mentioned here).
Can you subclass the QListView class and make use of that information? I suggest you recalculate the size of your widget in the same function where you add contents to it. While somewhat lacking elegance, this appears to be a pretty reliable solution.

How to draw a progress bar inside a list widget in Qt

I want to have a list of items that need to be processed in a QListWidget. Similar to Windows Media Player CD import, there should be a progress bar for every item in the list.
Now there seems to be a way to do this by creating a regular progress bar, using QPixmap::grabWidget() to save its appearance in a QPixmap and then adding this QPixmap as Icon to the QListWidgetItem via QListWidgetItem::setIcon().
However, this seems to be horribly wacky.
Do you know a more elegant way to achieve a progress bar inside a list widget?
Each item in a QListWidget can be represented by a QWidget of your choice, rather than the default rendering (text). You can set this by calling QListWidget::setItemWidget(). In this case, I'd recommend using QProgressBar as the rendering widget -- you should get the desired result.
From the documentation of QListWidget::setItemWidget():
This function should only be used to
display static content in the place of
a list widget item. If you want to
display custom dynamic content or
implement a custom editor widget, use
QListView and subclass QItemDelegate
instead.
You could do it by converting your list widget into a model/view/delegate combo. Then you can set a delegate on the list view that overrides the paint functions and draws the progress bar wherever you want it. I don't know how easy it would be to get an actual QProgressBar widget into the drawing area, however.
Alternately, you could consider making your own list-widget like container that knows about the progress bars.