Python Forum
[PyQt] QWidgetAction of QFrame not showing in menu
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] QWidgetAction of QFrame not showing in menu
#1
I am trying to set up a QPushButton with menu that has a horizontal frame in it, but it won't show. The button menu has several labels-based QWidgetActions that all work fine, but a frame won't show. Any ideas why?
self.remote_but = QPushButton('Blocked Content')
self.remote_menu = QMenu(self.remote_but)
self.remote_but.setMenu(self.remote_menu)
lab = QLabel(f'Allow content from <b>{from_addr}</b>')
widget = QWidgetAction(self.remote_menu)
widget.setDefaultWidget(lab)
self.remote_menu.addAction(widget)
frame = QFrame()
frame.setFrameShape(QFrame.Shape.HLine)
widget = QWidgetAction(self.remote_menu)
widget.setDefaultWidget(frame)
self.remote_menu.addAction(widget)
That's just an excerpt there's more to the code (multiple labels and configuring). Why isn't the frame showing? I ran actions() on the menu, and it includes the frame, however it doesn't show/display in the menu.
Reply
#2
see: https://stackoverflow.com/a/41068447
Reply
#3
Yeah, with past apps, I have been able to draw a line outside of a QMenu. That's no problem. It's when it's in a QMenu that I cannot draw a line. I don't know if it has to do with a menu object, or what...
Reply
#4
This uses QVSeperationLine widget from Larz60's link. I modified to make it horizontal. Works fine with PySide6.

from PySide6.QtWidgets import (QSizePolicy, QApplication, QVBoxLayout, QFrame, QWidget, QPushButton, QMenu, QLabel, QWidgetAction)

class QSeperationLine(QFrame):
    def __init__(self):
        super().__init__()
        self.setMinimumWidth(1)
        self.setFixedHeight(5)
        self.setFrameShape(QFrame.HLine)
        self.setFrameShadow(QFrame.Sunken)
        self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Minimum)

class Window(QWidget):
    def __init__(self):
        super().__init__()

        button = QPushButton('Blocked Content')
        menu = QMenu(button)
        button.setMenu(menu)

        label = QLabel("Label A")
        widget = QWidgetAction(menu)
        widget.setDefaultWidget(label)
        menu.addAction(widget)

        line = QSeperationLine()
        widget = QWidgetAction(line)
        widget.setDefaultWidget(line)
        menu.addAction(widget)


        label = QLabel("Label B")
        widget = QWidgetAction(menu)
        widget.setDefaultWidget(label)
        menu.addAction(widget)

        layout = QVBoxLayout(self)
        layout.addWidget(button)

app = QApplication()
window = Window()
window.show()
app.exec()
But why not use QMenu.addSeparator()? And for that matter why not use addAction(text) to add the labels?
Reply
#5
Thanks, @deanhystad. The only thing I can see that your code does mine did not is the minwidth and size policy. But, I've since went a different route and have been able to do most of what I wanted with a QMenu stylesheet. I achieved this by using a traditional actions and addSeparator(). Works good, requires less code, and satisfies.

I just wish I could partially manipulate the strings I use with the QActions to change parts of them to be bold or italic. That is a perk to using a QLabel and QWidgetAction. But, that's okay.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Help: Replace widgets in a QFrame on Combobox change mart79 2 3,536 May-14-2020, 05:21 AM
Last Post: mart79
  Menu bar not showing in gui window mlh 2 10,574 Sep-09-2018, 10:27 PM
Last Post: mlh
  [Tkinter] Tkinter optionmenu child menu position showing 0,0 thatguy14 2 4,669 Jun-15-2018, 10:42 AM
Last Post: thatguy14

Forum Jump:

User Panel Messages

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