Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to style QWidget descendants when parent is being hovered?
Qt 6.11 is out! See what's new in the release blog

How to style QWidget descendants when parent is being hovered?

Scheduled Pinned Locked Moved Unsolved General and Desktop
pyqt6cssstylesheetsstylingqwidget
2 Posts 1 Posters 72 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • creativegamer03C Offline
    creativegamer03C Offline
    creativegamer03
    wrote last edited by
    #1

    Hello. I'm new to this forum, and I'd like to ask how this can be achieved.

    My current project is more of made for fun, and it's a custom client I'm making for Revolt chat and Discord. Currently, I'm able to layout the whole chat window to display server-specific chats and interactions. Now, I switched from QListWidget to QScrollArea due to the former's limitations (especially for auto-adjusting QLabels in my custom widget when added as an item). However, I do miss the highlighting effect that QListWidget provides for its items, and I am currently trying to mimic its effect, albeit more simple if possible.

    Initially, I used stylesheets to style the ChatMessage's descendants. (Provided stylesheets was for testing purposes before actual styling)

    ChatMessage:hover * {
        background-color: cyan;
    }
    

    Unfortunately, it seems it doesn't follow usual CSS rules considering that it should, theoretically, apply the styles to its descendants when a ChatMessage is only being hovered; instead, it applied the styling regardless. If there's a way to actually do this kind of conditional styling (e.g. style descendants when parent is being hovered), please let me know. :)

    Some additional information if you need:
    The ChatMessage custom widget code (simplified):

    class ChatMessage(QtWidgets.QWidget):
        def __init__(self, parent = None):
            super(ChatMessage, self).__init__(parent)
            self.reactions = {}
            self.edited = False
            ...
            self.content = QtWidgets.QLabel()
            self.content.setWordWrap(True)
            ...
            message_header = QtWidgets.QWidget(self)
            message_header_layout = QtWidgets.QHBoxLayout()
            
            self.author = QtWidgets.QLabel()
            self.author.setTextFormat(QtCore.Qt.TextFormat.RichText)
            ...
            self.timestamp = QtWidgets.QLabel()
            ...
            self.message_toolbar = QtWidgets.QToolBar(self)
            ...
            # quick react bar test
            self.message_toolbar.addActions(self.reaction_actions)
            ...
            message_header_layout.addWidget(self.author)
            message_header_layout.addWidget(self.timestamp)
            message_header_layout.addStretch()
            message_header_layout.addWidget(self.message_toolbar)
            message_header.setLayout(message_header_layout)
            ...
            layout = QtWidgets.QVBoxLayout()
            layout.setSpacing(0)
            layout.addWidget(message_header)
            layout.addWidget(self.content)
            ...
            self.reaction_bar = QtWidgets.QToolBar(self)
            ...
            layout.addWidget(self.reaction_bar)
            ...
            self.setLayout(layout)
            ...
        # The rest of the actual code is mostly custom hover
        # event overrides to briefly show the quick react bar
        # on hover, a few helper methods, and slots to actions.
    

    Screenshots to demo the behavior:
    5429fc71-057c-4517-a6f3-5cea3fe2e8ec-image.png

    1 Reply Last reply
    0
    • creativegamer03C Offline
      creativegamer03C Offline
      creativegamer03
      wrote last edited by
      #2

      Update...

      So I was able to just use the overridden hover event methods to apply the stylesheets as I wanted. But I do feel that there might be a better way to do this.

      For now, I'll be using this method instead as it works as I wanted.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved