0

I need to use the old Qt pieMenu, I know it's deprecated, But I've found the source code here, and the installations instructions here(they are in the zip file as well)

I've successfully compiled and ran the examples, but my problem is that I need to add spaces and manipulate the font in the text of the pie menu

for example in the editor.cpp example, these lines insert items to the pie menu with the first parameter as the displayed text :

    markMenu->insertItem("Cut", this, SLOT(cut()));
    markMenu->insertItem("Del", this, SLOT(del()));
    markMenu->insertItem("Copy", this, SLOT(copy()));

I tried setting the lines like this as a test :

    markMenu->insertItem("item cut", this, SLOT(cut()));
    markMenu->insertItem(tr("Del item"), this, SLOT(del()));
    markMenu->insertItem("test on item", this, SLOT(copy()));

and the results are like in the photo(everything after the space character is not displayed) enter image description here

then I used the '\t' instead of using space directly, it worked as one space instead of a tab.

    markMenu->insertItem("test\ton\titem", this, SLOT(copy()));

but when trying to add a new line to the text things didn't work right. I tried using '\n' but didn't work for me.

Another issue, I was trying to change the font settings of the menu, I tried :

    markMenu->setFont(QFont("Arial", 9, 5, true)); // size 9, weight 5, italic

but this didn't affect the font, I tried styleSheets as well but no success.

Any ideas how can I display the new liens and adjust the font of the text?

  • I just realized the _manipulate the font in the text of the pie menu_. If this should be read as "rich text required" then `QPainter::drawText()` is the wrong choice. The answer to [SO: Draw rich text with QPainter](https://stackoverflow.com/a/29894245/7478597) shows a sufficient alternative. – Scheff's Cat Aug 14 '18 at 12:10
  • I downloaded the source code and had a look. 1st The text measurement with `QFontMetrics` is already there (in `paintEvent()`). 2nd The text measurement uses `font()` for font metrics **but** I cannot find any call of [`QPainter::setFont()`](http://doc.qt.io/qt-5/qpainter.html#setFont). (Or am I blind?) This could be an issue. Insert `qPainter.setFont(font());` before `qPainter.drawText();` and check whether your set font becomes effective then. – Scheff's Cat Aug 14 '18 at 12:58
  • 1
    I am not sure whether the construction of `QPainter` (in `SectorMenu::paintEvent()` it is the 1st line `QPainter painter(this);`) is responsible to take current font of the widget. In this case, `qPainter.setFont(font());` would not be necessary (but, on the other hand, it wouldn't do something wrong.) In Qt doc., I couldn't find a note about this. I looked in the [source code on woboq](https://code.woboq.org/qt5/qtbase/src/gui/painting/qpainter.cpp.html#_ZN8QPainterC1EP12QPaintDevice) but I couldn't find the actual proof there as well. – Scheff's Cat Aug 14 '18 at 13:07
  • Thanks !, I'll try this as well. (: – Tamim Boubou Aug 14 '18 at 13:12
  • u were right, qPainter.setFont(font()); isn't necessary and removing it didn't do anything wrong All I need to is to understand how the paintEvent() in qtpiemenu.cpp is working (about 400 lines of code) . I've already started editing some parts and I'm starting to get some good results !., Thanks a lot. – Tamim Boubou Aug 15 '18 at 11:53

1 Answers1

1

In the link provided by OP, scrolling to bottom, the source code can be found:

        painter.drawText(center.x() + x
                         - metrics.width(itemText(i)) / 2,
                         center.y() + y, itemText(i));

Now, a look into the Qt doc. of QPainter::drawText() provides the explanation:

This function does not handle the newline character (\n), as it cannot break text into multiple lines, and it cannot display the newline character. Use the QPainter::drawText() overload that takes a rectangle instead if you want to draw multiple lines of text with the newline character, or if you want the text to be wrapped.

...and a possible fix as well. (Emphasize by me.)

As the source code is available, this can be fixed.

To achieve a proper pre-calculation of boundary rectangle, QFontMetrics can be used.

The answer to How to automatically increase/decrease text size in label in Qt shows an example for text boundary determining with QFontMetrics which even considers automatic word-wrapping.

Scheff's Cat
  • 16,517
  • 5
  • 25
  • 45
  • Thanks for the answer, I'll try and inform you with the updates I've edited the question and added a smaller issue I have been facing, is there any chance that you may know the solution as well? – Tamim Boubou Aug 14 '18 at 12:49