Sometimes (but not always) This custom info button doesn't display itself in full. Any ideas why?

39 Views Asked by At

I have these 2 classes, one makes a custom button with an icon, some formatting and all that, and the other makes a custom tooltop that displays whenever the user hovers over the button, and it moves with the mouse.

Now sometimes, the tooltip gets cut off and doesnt display properly when i first hover over the info button, but when i then move my mouse somewhere else and re-enter the button, it displays fully.

Not fully displayed

Here is the code:

class InfoButton(QPushButton):
    def __init__(self, string, size: int, parent):
        super().__init__()
        self.string = string
        self.size = size
        self.parent_object = parent

        self.setStyleSheet("background-color: transparent;"
                           "border: 0px solid;"
                           f"border-radius: {self.size}px;"
                           "color: #ffffff;")

        self.setIcon(QIcon('images\\icons\\black\\info48x48.svg'))
        self.setIconSize(QSize(self.size, self.size))

        self.custom_tooltip = CustomTooltip(self.string, self.parent_object)
        self.setToolTipDuration(0)
        self.setMouseTracking(True)

    def mouseMoveEvent(self, e):
        self.custom_tooltip.showAtMouse()

    def leaveEvent(self, a0):
        self.custom_tooltip.hide()
class CustomTooltip(QWidget):
    def __init__(self, text, parent):
        super().__init__(parent)
        self.label = QLabel(self)
        self.text = text
        self.label.setText(self.text)
        self.setWindowFlags(Qt.ToolTip | Qt.FramelessWindowHint)
        self.setStyleSheet("background: #aaaaaa; border: 2px solid black; border-radius: 12px; color: black; padding: 5px;")
        self.setAttribute(Qt.WA_TranslucentBackground)

    def showAtMouse(self):
        cursor_pos = QCursor.pos()
        self.move(cursor_pos.x() - self.width() - 5, cursor_pos.y() - self.height() - 5)
        self.show()

I tried running it through chat gpt, but it spits out utter garbage with syntax that doesn't even work. I have a GUI app with this info button implemented, and there are so far 2 cases where it gets used. On the first case, it always works, and on the second case it always fails the first time, and there is no difference in the usage of the info button, so it doesn't really make sense.

After meddling with the parents of the various objects, it seems that it affects it. But i still can't get it to display correctly.

I hope some of you beautiful people can help me with this :)

0

There are 0 best solutions below