使用movemouseEvent

时间:2018-08-09 13:38:47

标签: python qt5 pyqt5

我有一个小部件,我希望能够使用鼠标从小部件内部的任何位置移动窗口,除了qcomboboxqlineeditqpushbutton内部。

mousePressEvent初始页中定义的mouseMoveEventWizard使这项工作有效。

问题是,当单击combobox时,小部件可以跳转到屏幕上的任何位置,或者qcomboboxcomboboxlist分开。

这是随机发生的,结果是随机的。它可以向任何方向移动几厘米,也可以向屏幕左下方,右下方等移动。

这似乎只有在单击组合框时才会发生。

如何为movemouseeventcomboboxes禁用linedits

import sys

from PyQt5.QtCore import Qt, QPoint, QIODevice, QDateTime, QSize, QObject, QProcess, pyqtSignal, QThread, QEvent, QTimer, QBasicTimer
from PyQt5.QtWidgets import  QStyle, QWidget, QMainWindow, QCompleter, QProgressBar, QFileDialog, QApplication, qApp, QLineEdit, QLabel, QComboBox, QWizard, QWizardPage, QPushButton, QVBoxLayout, QShortcut, QMessageBox, QDesktopWidget, QHBoxLayout
from PyQt5.QtGui import QPainter, QFont, QIcon, QPixmap, QPalette, QLinearGradient, QColor, QBrush, QCursor


class Wizard(QWizard):
    # Initilisation of the UI and Wizard
    def __init__(self, parent=None):
        super(Wizard, self).__init__(parent)

        self.addPage(EnterCode(self))                

        #mos position
        self.oldPos = self.pos()

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):
        delta = QPoint(event.globalPos() - self.oldPos)
        print(delta)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        # if obj != self.comboBox:
        self.oldPos = event.globalPos()


class EnterCode(QWizardPage):
    """ Sensor Code Entry """

    def __init__(self, parent=None):
        super(EnterCode, self).__init__(parent)

        # Spacer Label
        self.spacer = QLabel()

        self._five_digit = QLineEdit(self)
        self.code_combo = QComboBox(self)
        self.label1 = QLabel()

        self.lineedit1 = QLineEdit(self)
        self.lineedit2 =QLineEdit(self)
        self.lineedit3 = QLineEdit(self)
        self.lineedit4 = QLineEdit(self)
        self.lineedit5 = QLineEdit(self)
        self.lineedit6 = QLineEdit(self)
        self.lineedit7 = QLineEdit(self)
        self.lineedit8 = QLineEdit(self)
        self.lineedit9 = QLineEdit(self)
        self.lineedit10 = QLineEdit(self)


        self.code_combo_list = [
            'Years', 'Months', 'Weeks', 'Days', 'Hours', 'Years', 'Months', 'Weeks', 'Days', 'Hours']
        for x in self.code_combo_list:
            self.code_combo.addItem(x)

        # num of logs combo box
        self.enter_num_logs = QLineEdit(self)
        self.num_logs_combo = QComboBox(self)
        self.logs_label = QLabel()


        self.num_logs_combo_list = [
            'Years', 'Months', 'Weeks', 'Days', 'Hours', 'Years', 'Months', 'Weeks', 'Days', 'Hours']
        for x in self.num_logs_combo_list:
            self.num_logs_combo.addItem(x)

        # ~buttons
        self.btn = QPushButton('Download Data')

        layout = QVBoxLayout()

        layout.addWidget(self.spacer)
        layout.addWidget(self.label1)
        layout.addWidget(self.code_combo)
        layout.addWidget(self._five_digit)

        layout.addWidget(self.lineedit1)
        layout.addWidget(self.lineedit2)
        layout.addWidget(self.lineedit3)
        layout.addWidget(self.lineedit4)
        layout.addWidget(self.lineedit5)
        layout.addWidget(self.lineedit6)
        layout.addWidget(self.lineedit7)
        layout.addWidget(self.lineedit8)
        layout.addWidget(self.lineedit9)
        layout.addWidget(self.lineedit10)


        layout.addWidget(self.logs_label)
        layout.addWidget(self.num_logs_combo)
        layout.addWidget(self.enter_num_logs)

        layout.addWidget(self.btn)

        self.setLayout(layout)



if __name__ == '__main__':

1 个答案:

答案 0 :(得分:1)

由于问题是随机的,可能是因为尝试打开QComboBox的弹出窗口时按下了QWizard,所以避免出现问题的一种方法是使用{{ 1}}。

QApplication.startDragDistance()