How to move from one QWidget to another QWidget in PyQt5

0

I would like to know how I can go by filling out a form designed in PyQt and pressing the enter key jump to the next QlineEdit

#Clase heredada de QMainWindow (Constructor de ventanas)
class VentanaFresado(QDialog):
#Metodo constructor de la clase
    def __init__(self):
        #Iniciar el objeto QMainWindow
        QDialog.__init__(self)
        #Cargar la configuracion del archivo .ui en el objeto
        uic.loadUi("DialogFresado.ui", self)
        self.setWindowTitle("Calculos Fresado")
        self.btn_calcular.clicked.connect(self.btn_calcular_clicked)

        #evento del boton btn_calcular
    def btn_calcular_clicked(self):
        var_diamherr = float(self.txt_diamhta.text())
        var_rpm = float(self.txt_rpm.text())
        var_vc = float(self.txt_vc.text()) 

        if var_diamherr >= 1 and var_rpm >= 1 and var_vc == 0 :
            vel_corte = ((var_diamherr * 3.1416 * var_rpm)/(1000))
            self.txt_vc.setText (str(round(vel_corte,2)))
        elif var_vc >= 1 and var_diamherr >= 1 and var_rpm == 0 :
            rpm = ((var_vc * 1000)/(3.1416 * var_diamherr))
            self.txt_rpm.setText (str(round(rpm,2)))            


#Instancia para iniciar una aplicacion
app = QApplication(sys.argv)
#Crear un objeto de la clase
ventana2 = VentanaFresado()
#Mostra la ventana
ventana2.show()
#Ejecutar la aplicacion
app.exec_()

DialogFresado.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>DialogFresado</class>
 <widget class="QDialog" name="DialogFresado">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>500</width>
    <height>500</height>
   </rect>
  </property>
  <property name="minimumSize">
   <size>
    <width>500</width>
    <height>500</height>
   </size>
  </property>
  <property name="maximumSize">
   <size>
    <width>500</width>
    <height>500</height>
   </size>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>30</y>
     <width>100</width>
     <height>16</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Arial</family>
     <pointsize>10</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>Diametro Hta</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="txt_diamhta">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>50</y>
     <width>60</width>
     <height>20</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Arial</family>
     <pointsize>8</pointsize>
     <weight>50</weight>
     <italic>true</italic>
     <bold>false</bold>
    </font>
   </property>
   <property name="focusPolicy">
    <enum>Qt::StrongFocus</enum>
   </property>
   <property name="styleSheet">
    <string notr="true">font: italic 8pt &quot;Arial&quot;;
color: rgb(0, 0, 0);
</string>
   </property>
   <property name="text">
    <string>0</string>
   </property>
   <property name="clearButtonEnabled">
    <bool>false</bool>
   </property>
  </widget>
  <widget class="QLabel" name="label_2">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>80</y>
     <width>100</width>
     <height>16</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Arial</family>
     <pointsize>10</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>RPM Husillo</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="txt_rpm">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>100</y>
     <width>60</width>
     <height>20</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Arial</family>
     <pointsize>8</pointsize>
     <weight>50</weight>
     <italic>true</italic>
     <bold>false</bold>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">font: italic 8pt &quot;Arial&quot;;
color: rgb(0, 0, 0);
</string>
   </property>
   <property name="text">
    <string>0</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_3">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>130</y>
     <width>100</width>
     <height>16</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Arial</family>
     <pointsize>10</pointsize>
     <weight>75</weight>
     <bold>true</bold>
    </font>
   </property>
   <property name="text">
    <string>Vel Corte</string>
   </property>
  </widget>
  <widget class="QLineEdit" name="txt_vc">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>150</y>
     <width>60</width>
     <height>20</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Arial</family>
     <pointsize>8</pointsize>
     <weight>50</weight>
     <italic>true</italic>
     <bold>false</bold>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">font: italic 8pt &quot;Arial&quot;;
color: rgb(0, 0, 0);
</string>
   </property>
   <property name="text">
    <string>0</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_4">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>50</y>
     <width>50</width>
     <height>16</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Arial</family>
     <pointsize>8</pointsize>
     <weight>9</weight>
     <italic>false</italic>
     <bold>false</bold>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">font: 75 8pt &quot;Arial&quot;;
</string>
   </property>
   <property name="text">
    <string>mm</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_5">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>100</y>
     <width>50</width>
     <height>16</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Arial</family>
     <pointsize>8</pointsize>
     <weight>9</weight>
     <italic>false</italic>
     <bold>false</bold>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">font: 75 8pt &quot;Arial&quot;;
</string>
   </property>
   <property name="text">
    <string>RPM</string>
   </property>
  </widget>
  <widget class="QLabel" name="label_6">
   <property name="geometry">
    <rect>
     <x>110</x>
     <y>150</y>
     <width>50</width>
     <height>16</height>
    </rect>
   </property>
   <property name="font">
    <font>
     <family>Arial</family>
     <pointsize>8</pointsize>
     <weight>9</weight>
     <italic>false</italic>
     <bold>false</bold>
    </font>
   </property>
   <property name="styleSheet">
    <string notr="true">font: 75 8pt &quot;Arial&quot;;
</string>
   </property>
   <property name="text">
    <string>m/min</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_calcular">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>190</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>Calcular</string>
   </property>
  </widget>
  <widget class="QPushButton" name="btn_borrar">
   <property name="geometry">
    <rect>
     <x>120</x>
     <y>190</y>
     <width>75</width>
     <height>23</height>
    </rect>
   </property>
   <property name="text">
    <string>Borrar</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
    
asked by Hernan Patiño 11.01.2018 в 23:32
source

1 answer

0

One possible solution is to overwrite the keyPressEvent method that is called every time a keyboard button is pressed, we get the key through the method key() . On the other hand we get the widget that has the focus through QApplication::focusWidget() . With the above we manage the logic of changing the focus through the method setFocus() of all widgets.

class VentanaFresado(QDialog):
#Metodo constructor de la clase
    def __init__(self):
        [...]
        self.btn_calcular.clicked.connect(self.btn_calcular_clicked)
        # establecemos una lista con el orden de los QLineEdit
        self.les = [self.txt_diamhta, self.txt_rpm, self.txt_vc]

        #evento del boton btn_calcular
    def btn_calcular_clicked(self):
        [...]

    def keyPressEvent(self, event):
        if event.key()==Qt.Key_Return:
            wfocus = QApplication.focusWidget() # obtenemos el widget que tiene el focus
            if wfocus in self.les: # verificamos que se encuentre en la lista
                ix = self.les.index(wfocus) #obtenemos el indice del QLineEdit
                if ix != len(self.les)-1: # cambiamos el focus si no es el ultimo
                    self.les[ix+1].setFocus()
        QDialog.keyPressEvent(self, event)

Another recommended way when you can not access the keyPressEvent method is to use a filter as shown below:

class FocusHelper(QObject):
    def __init__(self, *args, **kwargs):
        QObject.__init__(self, *args, **kwargs)
        self.w = []

    def addWidget(self, w):
        self.w.append(w)
        w.installEventFilter(self)

    def eventFilter(self, obj, event):
        if event.type() == QEvent.KeyPress:
            wfocus = QApplication.focusWidget()
            if obj in self.w and wfocus in self.w:
                ix = self.w.index(wfocus)
                if ix != len(self.w) -1:
                    self.w[ix+1].setFocus()
        return QObject.eventFilter(self, obj, event)

if __name__ == '__main__':
    #Instancia para iniciar una aplicacion
    app = QApplication(sys.argv)
    #Crear un objeto de la clase
    ventana2 = VentanaFresado()
    helper = FocusHelper()
    helper.addWidget(ventana2.txt_diamhta)
    helper.addWidget(ventana2.txt_rpm)
    helper.addWidget(ventana2.txt_vc)
    #Mostra la ventana
    ventana2.show()
    #Ejecutar la aplicacion
    sys.exit(app.exec_())
    
answered by 12.01.2018 / 02:12
source