0

Tengo el siguiente problema, tengo el siguiente fragmento de código que lo utilizo para poder mover el MainWindow.

def mousePressEvent(self,event):
    if event.button() == QtCore.Qt.LeftButton:
        self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
        event.accept()
def mouseMoveEvent(self,event):
    if event.buttons() == QtCore.Qt.LeftButton:
        self.move(event.globalPos()-self.dragPosition)
        event.accept()

sin embargo al ejecutar el programa, unicamente al dar click en el QComboBox, me arroja 2 errores,

1.- El primero es que si muevo por muy minimo el mouse al hacer click en el QComboBox, me arroja el siguiente error:

introducir la descripción de la imagen aquí

Traceback (most recent call last):
      File "C:\Users\Angel\Desktop\Test\Login.py", line 87, in mouseMoveEvent
        self.move(event.globalPos()-self.dragPosition)
    AttributeError: 'Dato' object has no attribute 'dragPosition'
    [Finished in 28.2s with exit code 3]

2.- Cuando trato de volver a dar click al QComboBox se cambia de lugar:

Dejo un gif con los 2 problemas que genera...

introducir la descripción de la imagen aquí

este es el codigo completo de la aplicación:

from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5 import uic, QtCore, Qt
from Inicio import Inicio
from Firebase_load import *
from Registro import Image
from add_users import Usuarios
from es import user_name_1

class Principal(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        uic.loadUi("Login.ui",self)
        self._Inicio = Inicio()
        self.registro = Image()
        self.add = Usuarios()

        self.combo_login ########## Este es el nombre de el QComboBox            

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground,True)
        self.setAttribute(QtCore.Qt.WA_NoSystemBackground,False)

        self.closed.clicked.connect(lambda:self.close())
        self.mostrar_hora()

        self.timer = QtCore.QTimer(self)
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.mostrar_hora)
        self.timer.start()

    def mostrar_hora(self):
        self.hora.setText(QtCore.QDateTime.currentDateTime().toString("hh:mm:ss AP"))



class Dato(Principal):
    def __init__(self):
        Principal.__init__(self)
        self.b_login.clicked.connect(self.validate_User)

    def validate_User(self):
        user = self.l_usuario.text()

        if user == '':
            self.l_status.setText('No se ingreso un usuario')
        else:
            ref = db.reference("/usuarios/")
            res = ref.get()

            for key in res.items():
                if key[0] == user:
                    self.validate_password(user)
    def validate_password(self,user):
        password  = self.l_password.text()

        refp = db.reference("/usuarios/"+user)
        res = refp.get()

        for key in res.items():
            if key[0] == 'password':
                if key[1] == password:
                    self.other()
                    self.l_status.setText("login")
                    pass
                else:
                    self.l_password.setText('')
                    self.l_status.setText("Invalidate Password")
            elif key[0] == 'permisos':
                self.registro.acces = int(key[1])
                self.add.acces = int(key[1])



    def other(self):

        self.close()
        user_name_1(self.l_usuario.text())
        self._Inicio.user_online_on(self.l_usuario.text())
        self._Inicio.user.setText(self.l_usuario.text())
        self._Inicio.show()

    def mousePressEvent(self,event):
        if event.button() == QtCore.Qt.LeftButton:
            self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
            event.accept()
    def mouseMoveEvent(self,event):
        if event.buttons() == QtCore.Qt.LeftButton:
            self.move(event.globalPos()-self.dragPosition)
            event.accept()

app = QApplication([])
p = Dato()
p.show()
app.exec_()

Login.ui

<?xml version="1.0" encoding="UTF-8"?>
  <ui version="4.0">
   <class>MainWindow</class>
   <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>351</width>
      <height>551</height>
     </rect>
    </property>
    <property name="windowTitle">
     <string>MainWindow</string>
    </property>
    <property name="windowIcon">
     <iconset>
      <normaloff>icono.ico</normaloff>icono.ico</iconset>
    </property>
    <property name="styleSheet">
     <string notr="true">background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));</string>
    </property>
    <widget class="QWidget" name="centralwidget">
     <property name="styleSheet">
      <string notr="true">background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));</string>
     </property>
     <widget class="QFrame" name="frame">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>90</y>
        <width>351</width>
        <height>461</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true">background:qlineargradient(spread:pad, x1:0.494249, y1:0, x2:0.472, y2:1, stop:0 rgba(0, 0, 0, 202), stop:1 rgba(51, 51, 51, 218));</string>
      </property>
      <property name="frameShape">
       <enum>QFrame::StyledPanel</enum>
      </property>
      <property name="frameShadow">
       <enum>QFrame::Raised</enum>
      </property>
      <widget class="QLabel" name="login">
       <property name="geometry">
        <rect>
         <x>10</x>
         <y>180</y>
         <width>161</width>
         <height>31</height>
        </rect>
       </property>
       <property name="font">
        <font>
         <pointsize>14</pointsize>
        </font>
       </property>
       <property name="styleSheet">
        <string notr="true">QLabel#login{
  background:None;
  color:white;
  }</string>
       </property>
       <property name="text">
        <string>Login</string>
       </property>
      </widget>
      <widget class="QLabel" name="icono_usuario">
       <property name="geometry">
        <rect>
         <x>10</x>
         <y>230</y>
         <width>32</width>
         <height>32</height>
        </rect>
       </property>
       <property name="styleSheet">
        <string notr="true">QLabel#icono_usuario{
  background:#7A7B7F;
  border:0px;
  background-image:url(Images/usuario.png)
  }</string>
       </property>
       <property name="text">
        <string/>
       </property>
       <property name="pixmap">
        <pixmap>new/Imagenes/usuario.png</pixmap>
       </property>
       <property name="alignment">
        <set>Qt::AlignCenter</set>
       </property>
      </widget>
      <widget class="QLabel" name="icono_contrasena">
       <property name="geometry">
        <rect>
         <x>10</x>
         <y>280</y>
         <width>32</width>
         <height>32</height>
        </rect>
       </property>
       <property name="styleSheet">
        <string notr="true">QLabel#icono_contrasena{
  background:#7A7B7F;
  border :0px;
  background-image:url(Images/pss.png);
  background-repeat:none;
  background-position:center;
  }</string>
       </property>
       <property name="text">
        <string/>
       </property>
       <property name="pixmap">
        <pixmap>new/Imagenes/pss.png</pixmap>
       </property>
       <property name="scaledContents">
        <bool>false</bool>
       </property>
       <property name="alignment">
        <set>Qt::AlignCenter</set>
       </property>
      </widget>
      <widget class="QLineEdit" name="l_password">
       <property name="geometry">
        <rect>
         <x>42</x>
         <y>280</y>
         <width>291</width>
         <height>32</height>
        </rect>
       </property>
       <property name="font">
        <font>
         <pointsize>10</pointsize>
        </font>
       </property>
       <property name="styleSheet">
        <string notr="true">QLineEdit#l_password{
  border:0px;
  background:qlineargradient(spread:pad, x1:0.494249, y1:0, x2:0.472, y2:1, stop:0 rgba(0, 0, 0, 183), stop:1 rgba(157, 157, 157, 94));
  color:white;
  }</string>
       </property>
       <property name="inputMethodHints">
        <set>Qt::ImhHiddenText|Qt::ImhNoAutoUppercase|Qt::ImhNoPredictiveText|Qt::ImhSensitiveData</set>
       </property>
       <property name="text">
        <string/>
       </property>
       <property name="echoMode">
        <enum>QLineEdit::Password</enum>
       </property>
      </widget>
      <widget class="QLineEdit" name="l_usuario">
       <property name="geometry">
        <rect>
         <x>42</x>
         <y>230</y>
         <width>291</width>
         <height>32</height>
        </rect>
       </property>
       <property name="font">
        <font>
         <pointsize>10</pointsize>
        </font>
       </property>
       <property name="styleSheet">
        <string notr="true">QLineEdit#l_usuario{
  border:0px;
  background:qlineargradient(spread:pad, x1:0.494249, y1:0, x2:0.472, y2:1, stop:0 rgba(0, 0, 0, 183), stop:1 rgba(157, 157, 157, 94));
  color:white;
  }</string>
       </property>
       <property name="inputMask">
        <string/>
       </property>
       <property name="text">
        <string/>
       </property>
       <property name="cursorPosition">
        <number>0</number>
       </property>
       <property name="placeholderText">
        <string/>
       </property>
      </widget>
      <widget class="QPushButton" name="b_login">
       <property name="geometry">
        <rect>
         <x>240</x>
         <y>380</y>
         <width>91</width>
         <height>31</height>
        </rect>
       </property>
       <property name="font">
        <font>
         <pointsize>9</pointsize>
         <weight>50</weight>
         <bold>false</bold>
        </font>
       </property>
       <property name="styleSheet">
        <string notr="true">QPushButton#b_login{
  border:0px;
  background:qlineargradient(spread:pad, x1:0.494249, y1:0, x2:0.472, y2:1, stop:0 rgba(255, 255, 255, 255), stop:1 rgba(234, 234, 234, 255));
  }


  }
  QPushButton#b_login:hover{
  border:0px;
  background:qlineargradient(spread:pad, x1:0.494249, y1:0, x2:0.472, y2:1, stop:0 rgba(255, 255, 255, 255), stop:1 rgba(200, 200, 200, 255));

  }
  QPushButton#b_login:pressed{
  border:0px;
  background:qlineargradient(spread:pad, x1:0.494249, y1:0, x2:0.472, y2:1, stop:0 rgba(255, 255, 255, 255), stop:1 rgba(234, 234, 234, 255));
  border-bottom:2px solid grey;</string>
       </property>
       <property name="text">
        <string>Login</string>
       </property>
      </widget>
      <widget class="QPushButton" name="closed">
       <property name="geometry">
        <rect>
         <x>320</x>
         <y>0</y>
         <width>31</width>
         <height>23</height>
        </rect>
       </property>
       <property name="font">
        <font>
         <pointsize>9</pointsize>
         <weight>75</weight>
         <bold>true</bold>
        </font>
       </property>
       <property name="styleSheet">
        <string notr="true">QPushButton#closed{
  background:None;
  border:0px;
  color:lightgrey;
  }
  QPushButton#closed:hover{
  color:#D8211D;
  }</string>
       </property>
       <property name="text">
        <string>X</string>
       </property>
      </widget>
      <widget class="QLabel" name="hora">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>440</y>
         <width>101</width>
         <height>20</height>
        </rect>
       </property>
       <property name="font">
        <font>
         <pointsize>10</pointsize>
        </font>
       </property>
       <property name="styleSheet">
        <string notr="true">QLabel#hora{
  background:None;
  color:white;
  }</string>
       </property>
       <property name="text">
        <string/>
       </property>
       <property name="alignment">
        <set>Qt::AlignCenter</set>
       </property>
      </widget>
      <widget class="QLabel" name="l_status">
       <property name="geometry">
        <rect>
         <x>60</x>
         <y>330</y>
         <width>231</width>
         <height>21</height>
        </rect>
       </property>
       <property name="styleSheet">
        <string notr="true">QLabel#l_status{
  background:None;
  color:white;
  }</string>
       </property>
       <property name="text">
        <string/>
       </property>
       <property name="alignment">
        <set>Qt::AlignCenter</set>
       </property>
      </widget>
      <widget class="QComboBox" name="combo_login">
       <property name="enabled">
        <bool>true</bool>
       </property>
       <property name="geometry">
        <rect>
         <x>150</x>
         <y>360</y>
         <width>50</width>
         <height>17</height>
        </rect>
       </property>
       <property name="styleSheet">
        <string notr="true">QComboBox#combo_login{
  background:qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0, stop:0 rgba(0, 0, 0, 0), stop:1 rgba(255, 255, 255, 0));
  border:0px;
  color:white;
  border-bottom: 1px solid lightblue;
  }


  QComboBox QAbstractItemView
  {
      border: 2px solid darkgray;
      background:grey;
      color: black;
      selection-background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #111, stop: 1 #333);
  }
  QComboBox#combo_login::drop-down
  {
       subcontrol-origin: padding;
       subcontrol-position: top right;
       width: 15px;
       color: white;
       border-left-width: 0px;
       border-left-color: darkgray;
       border-left-style: solid; /* just a single line */
       border-top-right-radius: 3px; /* same radius as the QComboBox */
       border-bottom-right-radius: 3px;
       padding-left: 10px;
   }</string>
       </property>
       <item>
        <property name="text">
         <string>exe1</string>
        </property>
       </item>
       <item>
        <property name="text">
         <string>exe2</string>
        </property>
       </item>
       <item>
        <property name="text">
         <string>exe3</string>
        </property>
       </item>
      </widget>
     </widget>
     <widget class="QLabel" name="label">
      <property name="geometry">
       <rect>
        <x>90</x>
        <y>10</y>
        <width>181</width>
        <height>171</height>
       </rect>
      </property>
      <property name="styleSheet">
       <string notr="true">background:url(Images/Login.png);
  background-repeat:none;
  background-position:center;</string>
      </property>
      <property name="text">
       <string/>
      </property>
     </widget>
    </widget>
   </widget>
   <resources/>
   <connections/>
  </ui>
13
  • podrias compartir el archivo Login.ui
    – user22539
    el 24 ago. 2018 a las 3:05
  • He tratado de reproducir tu error pero no he podido, seria bueno que te tomes el tiempo y proveas un real ejemplo mínimo verificable, pienso que ya tienes la suficiente saber lo que es. :)
    – user22539
    el 24 ago. 2018 a las 3:11
  • @eyllanesc, claro ya subi el archivo login.ui
    – Revsky01
    el 24 ago. 2018 a las 3:12
  • Por favor elimina las cosas que no estan definidas, un ejemplo mínimo verificable no debe depender de un codigo externo, por eso se dice que debe ser completo
    – user22539
    el 24 ago. 2018 a las 3:14
  • Acabo de probar tu código con Login.ui comentando los imports que generan errores y aun no puedo reproducir su error ¿esta seguro que ese es código que genera el error?
    – user22539
    el 24 ago. 2018 a las 3:16

1 respuesta 1

1

La causa de este comportamiento es que el evento "Press" no es propagado al padre desde el hijo, pero si lo es el evento "Move" cuando, manteniendo pulsado el botón izquierdo, sacamos el cursor de los límites del botón (pasa tanto con el QPushButton como con el botón del QComboBox).

Voy a aclara que en Linux (probado en ArchLinux con KDE Plasma) no hay forma de reproducir el problema con el QComboBox al menos en mi caso, ahora bién, en Windows 10 el comportamiento es completamente distinto porque la lista de opciones no se superpone al botón y el evento "Move" se propaga al padre cuando rebasamos los límites del botón del QComboBox (el problema no ocurre dentro de la lista desplegable). Lo anterior ocasiona el comportamiento indeseado mostrado, al permitir el arrastre de la ventana cuando el click se originó en el QComboBox, pero además, éste se despliega en posiciones erráticas al soltar el botón izquierdo del ratón.

El error:

AttributeError: 'Dato' object has no attribute 'dragPosition'

es causado porque se produce el evento "Move" sin que previamente tuviera lugar el evento "Press" en el padre, como defines el atributo dragPosition en mousePressEvent cuando se llama mouseMoveEvent e intenta usar self.dragPosition este no existe aún. Por eso el error no se produce si previamente a desplegar el QComboBox hacemos click en la ventana, en este caso el atributo ya está definido, pero no actualizado por mousePressEvent, lo que causa que dragPosition no tenga la posición de este último click, sino la del anterior, causando el "salto" apreciado en el GIF.

Como recomendación, no crees nunca atributos de instancia fuera del __init__, aunque solo sean usados por unos métodos determinados inicializalos siempre en el __init__ (a None si no quieres darles un valor), no los crees en los otros métodos.

El problema se puede solucionar de varias formas, por ejemplo asegurándonos antes de procesar mouseMoveEvent de que llegó previamente un evento "Press" a la ventana (dado que este es bloqueado por el botón):

class Dato(Principal):

    def __init__(self):
        super().__init__()
        self._on_drag = None   # <<<<<<<<<<<<< No te olvides de esto

    def mousePressEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self._drag_pos= event.globalPos() - self.frameGeometry().topLeft()
            event.accept()

    def mouseMoveEvent(self, event):
        if event.buttons() == QtCore.Qt.LeftButton and self._drag_pos is not None:
            self.move(event.globalPos() - self._drag_pos)
            event.accept()

    def mouseReleaseEvent(self, event):
        if event.button() == QtCore.Qt.LeftButton:
            self._drag_pos = None      
            event.accept()

Otra opción es simplemente bloquear la propagación de los eventos del ratón al padre mediante el atributo QtCore.Qt.WA_NoMousePropagation:

class Dato(Principal):
    def __init__(self):
        super().__init__()
        self.combo_login.setAttribute(QtCore.Qt.WA_NoMousePropagation)
        # Resto del código
1
  • muchas gracias llevava mucho tiempo tratando de arreglar esto
    – Revsky01
    el 25 ago. 2018 a las 5:50

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.