Hello good morning,I am creating some signals that help me to control the flow of a program,but I am a bit stuck.I made a small application where I isolate the problem I am facing.
What I want to do basically is that every time a condition is not met between the QSpinbox 1(self.botón1) and 2(self.botón2) ,the focus is kept on the one where the value that made the condition not met was changed and also a warning message should be displayed.
The condition to fulfill is that the value of the QSpinBox 2 is greater than the QSpinBox 1.
With the code I have so far works, but the warning messages are displayed twice, either when the condition is not met and click on the Saludos tab of the QtabWidget or when the condition is not met and press the Enter key on the QSpinBox 1 or QSpinBox 2.
import sys
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow,QSpinBox,QHBoxLayout,QMessageBox,QTabWidget,QWidget
from PySide6.QtCore import Qt,QEvent
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setFixedSize(400,200)
a=QWidget()
b=QWidget()
self.tab=QTabWidget(self)
self.tab.setStyleSheet((":disabled { color: black}"))
self.tab.tabBarClicked.connect(self.bloq_tab)
self.tab.addTab(a,"Hola")
self.tab.addTab(b,"Saludos")
self.setCentralWidget(self.tab)
self.botón1=QSpinBox(a)
self.botón1.setPrefix("In: ")
self.botón1.setGeometry(20,20,130,30)
self.botón1.setValue(5)
self.botón1.editingFinished.connect(self.bloq_wid1)
self.botón2=QSpinBox(a)
self.botón2.setGeometry(20,80,130,30)
self.botón2.setPrefix("LRA: ")
self.botón2.setValue(15)
self.botón2.editingFinished.connect(self.bloq_wid2)
self.botón3=QSpinBox(a)
self.botón3.setGeometry(190,80,130,30)
self.botón3.setPrefix("X: ")
self.botón3.setValue(15)
self.label=QLabel("Now you see it",b)
self.label.setGeometry(140,40,80,60)
def bloq_wid1(self):
if self.botón1.value()>self.botón2.value():
self.botón2.blockSignals(True)
self.message1()
else:
self.botón2.blockSignals(False)
def message1(self):
self.mensaje_error=QMessageBox()
self.mensaje_error.setIcon(QMessageBox.Warning)
self.mensaje_error.setWindowTitle("Warning")
self.mensaje_error.setText("In must be less than LRA")
self.mensaje_error.exec()
self.botón1.setFocus()
self.botón1.selectAll()
def bloq_wid2(self):
if self.botón1.value()>self.botón2.value():
self.botón1.blockSignals(True)
self.message2()
else:
self.botón1.blockSignals(False)
def message2(self):
self.mensaje_error=QMessageBox()
self.mensaje_error.setIcon(QMessageBox.Warning)
self.mensaje_error.setWindowTitle("Warning")
self.mensaje_error.setText("LRA must be greater than In")
self.mensaje_error.exec()
self.botón2.setFocus()
self.botón2.selectAll()
def bloq_tab(self):
if self.botón1.value()>self.botón2.value():
if self.botón1.hasFocus()==True:
self.tab.setTabEnabled(1,False)
self.message1()
elif self.botón2.hasFocus()==True:
self.tab.setTabEnabled(1,False)
self.message2()
else:
self.tab.setTabEnabled(1,True)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()