Python Forum
[PyQt] QAction bold part of the string argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QAction bold part of the string argument
#1
My knowledge of PyQt6 is limited. I can't figure how to bold part of a string passed to a QAction. Other widgets support rich text, so it's easy with them, but QAction does not. It's like I want to pass "only part of this string should be bolded" to a QAction.

Can that be done? I got nothing on how to do it.

EDIT:
In the spirit of rubber duck debugging, I had an idea. Can I pass a QLabel (which does support rich text) to a QMenu as an action? I think so via QWidgetAction. I'm gonna try. Let me know if I'm wrong.
Reply
#2
Well, something's going on. This
widget = W.QWidgetAction(W.QLabel('Allow content from <b>HERE</b>'))
action = self.remote_menu.addAction(widget)
gives this error:
RuntimeError: wrapped C/C++ object of type QWidgetAction has been deleted
I don't understand that one bit.
EDIT:
Alright. The error was because Python garbage collected the label. Assigning it to a variable fixed that. That leads to a problem I've had before with addAction(). addAction(QAction) never shows in a menu for me. addAction(str) works fine (sorta why I went with my original question). Sorry to post sorta step by step here, but I like to see if I get help as I try to help myself. Double up.
EDIT AGAIN:
I'm digging some and the action added via addAction(QAction) is included in the QMenu as returned by actions(). Buut, it doesn't show/display. It's there, I just can't see it in the menu.
Reply
#3
Good to go. Things I've found when passing a QAction to addAction: the QAction needs a parent most times to show in the menu. So, I passed the QMenu to QWidgetAction, then call setDefaultWidget() on that widget. It shows, and using a QLabel allows for selective bolding, etc. This solution does what I want.

I've found QActions are indented by default. I guess to allow for check boxes or icons, and QWidgetAction is not indented. I'm all ears if anyone has a solution to that before I find the solution.

Hopefully this can help someone else.
Reply
#4
A few thoughts on that. You put a lot of effort into small things. Ask yourself the question: do I really need this?

It doesn't really matter whether part of a menuitem is in bold or not.
Reply
#5
(Sep-12-2022, 04:55 PM)Axel_Erfurt Wrote: A few thoughts on that. You put a lot of effort into small things. Ask yourself the question: do I really need this?

It doesn't really matter whether part of a menuitem is in bold or not.

Yeah, I mean, I understand. I just want a little flair in my project. If nothing else, I'm practicing writing code and building my skill set. I'm kinda green and want to get better. What you ask makes sense, and I wasn't gonna do it, but I just feel like writing code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation [PyQt] Setting icon on QAction from outside QGuiApplication gradlon93 3 1,746 Jan-04-2023, 11:37 AM
Last Post: gradlon93
  PyQt6 QAction with icon and string malonn 2 1,723 Sep-12-2022, 11:59 AM
Last Post: malonn
  Syntax Error: Positional argument follows keyword argument Rama02 3 4,133 Feb-09-2021, 06:10 PM
Last Post: deanhystad
  How to make button text bold in Tkinter? scratchmyhead 2 12,104 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [Tkinter] changing title text to bold in tkinter Kumarkv 2 8,788 May-09-2020, 10:41 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020