Event filter for QtabWidget (PySide6)
-
wrote on 14 Nov 2024, 03:07 last edited by
Hello good evening ,I am creating an event filter so that when a number entered to a Qspinbox exceeds the number 10,you can not access the tabs of a QtabWidget.
The problem I have is that if the application is minimized when the value of the QSpinbox exceeds the number 10 the tabs disappear and until this value decreases and I pass the mouse over where the tabs should be, they are not shown.
This is the code I have so far:import sys from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QSpinBox, QTabWidget, QWidget class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setFixedSize(400, 200) a = QWidget() b = QWidget() self.tab = QTabWidget(self) self.tab.addTab(a, "Hola") self.tab.addTab(b, "Saludos") self.setCentralWidget(self.tab) self.tab.tabBar().installEventFilter(self) self.boton1 = QSpinBox(a) self.boton1.setGeometry(20, 20, 130, 30) self.boton1.setValue(5) self.label = QLabel("Now you see it", b) self.label.setGeometry(140, 40, 80, 60) def eventFilter(self, object, event): if object == self.tab.tabBar() and self.boton1.value() > 10: return True else: return False app = QApplication(sys.argv) window = MainWindow() window.show() app.exec()
I hope you can help me
-
wrote on 16 Nov 2024, 00:28 last edited by
Thank you so much ,it works that way ,on the other hand ,i dont limit the values because it is needed that way
-
Hello good evening ,I am creating an event filter so that when a number entered to a Qspinbox exceeds the number 10,you can not access the tabs of a QtabWidget.
The problem I have is that if the application is minimized when the value of the QSpinbox exceeds the number 10 the tabs disappear and until this value decreases and I pass the mouse over where the tabs should be, they are not shown.
This is the code I have so far:import sys from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QSpinBox, QTabWidget, QWidget class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setFixedSize(400, 200) a = QWidget() b = QWidget() self.tab = QTabWidget(self) self.tab.addTab(a, "Hola") self.tab.addTab(b, "Saludos") self.setCentralWidget(self.tab) self.tab.tabBar().installEventFilter(self) self.boton1 = QSpinBox(a) self.boton1.setGeometry(20, 20, 130, 30) self.boton1.setValue(5) self.label = QLabel("Now you see it", b) self.label.setGeometry(140, 40, 80, 60) def eventFilter(self, object, event): if object == self.tab.tabBar() and self.boton1.value() > 10: return True else: return False app = QApplication(sys.argv) window = MainWindow() window.show() app.exec()
I hope you can help me
wrote on 14 Nov 2024, 04:17 last edited by Pl45m4Hi and welcome to the forum,
The main problem is, that you block EVERY event that is coming from or going to the tabBar when
value > 10
...
so also events, that are required to make the widget/app run and get drawn smoothly.def eventFilter(self, object, event): if object == self.tab.tabBar() and self.boton1.value() > 10: return True else: return False
Actually you don't need an eventFilter at all.
I think using a signal from yourQSpinBox
to disable theQTabWidget
(orQTabBar
) is a better approach.class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setFixedSize(400, 200) a = QWidget() b = QWidget() self.tab = QTabWidget(self) self.tab.addTab(a, "Hola") self.tab.addTab(b, "Saludos") self.setCentralWidget(self.tab) self.boton1 = QSpinBox(a) self.boton1.setGeometry(20, 20, 130, 30) self.boton1.setValue(5) self.label = QLabel("Now you see it", b) self.label.setGeometry(140, 40, 80, 60) self.boton1.valueChanged.connect(self.handleSpinBoxValue) def handleSpinBoxValue(self, value): if value > 10: self.tab.tabBar().setDisabled(True) else: self.tab.tabBar().setDisabled(False) app = QApplication(sys.argv) window = MainWindow() window.show() app.exec()
Works like this:
I did it in C++, but the Python code above should work.
(Note: when other people should use your app, you should inform the user somehow that a value greater than 10 disables the other tabs)While writing this:
Why don't you limit the maximum value of your spinBox? So that it's never above 10?!self.boton1.setRange(0, 10) self.boton1.setValue(5)
Or should higher numbers still be possible?
-
wrote on 16 Nov 2024, 00:28 last edited by
Thank you so much ,it works that way ,on the other hand ,i dont limit the values because it is needed that way
-
1/3