Home > Enterprise >  How to Fix QStatusBar that stops to work if placed it in Layout?
How to Fix QStatusBar that stops to work if placed it in Layout?

Time:11-16

I succeeded to moved the QStatusBar to a specific location (repositioning), but when I hover I don't see the tips anymore. I tried QStatusBar.show() or .setVisible(True) but doesn't still work. How would you approach this. Thanks

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <widget name="__qt_fake_top_level">
  <widget  name="greetBTN">
   <property name="geometry">
    <rect>
     <x>70</x>
     <y>10</y>
     <width>75</width>
     <height>24</height>
    </rect>
   </property>
   <property name="statusTip">
    <string>Greeting people,...</string>
   </property>
   <property name="text">
    <string>greet</string>
   </property>
  </widget>
  <widget  name="gridLayoutWidget">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>40</y>
     <width>241</width>
     <height>80</height>
    </rect>
   </property>
   <layout  name="gridLayout_status"/>
  </widget>
  <widget  name="closeBTN">
   <property name="geometry">
    <rect>
     <x>160</x>
     <y>10</y>
     <width>75</width>
     <height>24</height>
    </rect>
   </property>
   <property name="statusTip">
    <string>About to close</string>
   </property>
   <property name="text">
    <string>close</string>
   </property>
  </widget>
 </widget>
 <resources/>
</ui>

converted to Python


# -*- coding: utf-8 -*-

from PySide5.QtCore import *
from PySide5.QtGui import *
from PySide5.QtWidgets import *

class Ui_AppMainWindow(object):
    def setupUi(self, AppMainWindow):
        if not AppMainWindow.objectName():
            AppMainWindow.setObjectName(u"AppMainWindow")
        AppMainWindow.resize(303, 190)
        self.centralwidget = QWidget(AppMainWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.greetBTN = QPushButton(self.centralwidget)
        self.greetBTN.setObjectName(u"greetBTN")
        self.greetBTN.setGeometry(QRect(70, 10, 75, 24))
        self.closeBTN = QPushButton(self.centralwidget)
        self.closeBTN.setObjectName(u"closeBTN")
        self.closeBTN.setGeometry(QRect(160, 10, 75, 24))
        self.gridLayoutWidget = QWidget(self.centralwidget)
        self.gridLayoutWidget.setObjectName(u"gridLayoutWidget")
        self.gridLayoutWidget.setGeometry(QRect(30, 40, 241, 80))
        self.gridLayout_status = QGridLayout(self.gridLayoutWidget)
        self.gridLayout_status.setObjectName(u"gridLayout_status")
        self.gridLayout_status.setContentsMargins(0, 0, 0, 0)
        AppMainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QStatusBar(AppMainWindow)
        self.statusbar.setObjectName(u"statusbar")
        AppMainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(AppMainWindow)

        QMetaObject.connectSlotsByName(AppMainWindow)
    # setupUi

    def retranslateUi(self, AppMainWindow):
        AppMainWindow.setWindowTitle(QCoreApplication.translate("AppMainWindow", u"MainWindow", None))
#if QT_CONFIG(statustip)
        self.greetBTN.setStatusTip(QCoreApplication.translate("AppMainWindow", u"Greeting people,...", None))
#endif // QT_CONFIG(statustip)
        self.greetBTN.setText(QCoreApplication.translate("AppMainWindow", u"greet", None))
#if QT_CONFIG(statustip)
        self.closeBTN.setStatusTip(QCoreApplication.translate("AppMainWindow", u"About to close", None))
#endif // QT_CONFIG(statustip)
        self.closeBTN.setText(QCoreApplication.translate("AppMainWindow", u"close", None))
    # retranslateUi


from PyQt5 import QtWidgets, uic
from PyQt5.QtWidgets import *
import sys

from PySide6.QtCore import *
from PySide6.QtGui import *
from PySide6.QtWidgets import *

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('Xuntitled.ui', self)
        

        self.statusbar.setVisible(True)
        self.statusbar.setStyleSheet('Background:red;')

        self.statusbar.setParent(self)
        #self.statusbar.showMessage('sqddsfdsfd') # works but if I hover, nothing !
        self.statusbar.move(50, 25)

        self.gridLayout_status.addWidget(self.statusbar, 1, 1)
        
        self.show()
        

app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()

CodePudding user response:

The expected behavior of a status bar is to be shown on the bottom margin of the window, allowing temporary messages and also providing an internal layout for "extra widgets" displayed in it.

If you want to show the status tip somewhere else, then you probably don't need a QStatusBar at all, and a basic QLabel will suffice.

The only requirement is to override the event() function of the window and check for StatusTip events.

In the following example I assumed that you don't really need the default status bar (so you should remove it from Designer and update the generated uic file accordingly if you use that approach).

class Ui(QMainWindow):
    def __init__(self):
        super(Ui, self).__init__()
        uic.loadUi('Xuntitled.ui', self)
        self.fakeStatusBar = QLabel()
        self.gridLayout_status.addWidget(self.fakeStatusBar)
        
    def event(self, event):
        if event.type() == event.StatusTip:
            self.fakeStatusBar.setText(event.tip())
            return True
        return super().event(event)
  • Related