Mouse movements too fast for `mouseMoveEvent()`

♀尐吖头ヾ 提交于 2021-02-10 05:21:25

问题


The following Python 3 resp. UI XML code shows 4 QProgessBar for each mouse direction (labeled X+, X-, Y+, Y-).

Fast mouse movements (in circles) will make the 4 QProgessBar move up to 99% and then some of the QProgessBar take a break until the mouse movements are slowed down to finally reach 100%.

  1. What's the reason for this behavior ?
  2. How can the code be fixed so that no QProgessBar takes a break at 99% ?

main.py

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QCursor
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        loadUi("mainwindow.ui", self)
        self.showMaximized()
        self.setMouseTracking(True)
        self.centralWidget().setAttribute(Qt.WA_TransparentForMouseEvents)

        self._old_x = QCursor.pos().x()
        self._old_y = QCursor.pos().y()

    def _update_bar(self, progress_bar, delta):
        current_value = progress_bar.value()
        new_value = current_value + delta
        progress_bar.setValue(new_value)

    def mouseMoveEvent(self, event):
        new_x = event.x()
        new_y = event.y()

        if new_x > self._old_x:
            self._update_bar(self.progressBar_x_plus, new_x - self._old_x)
        if new_x < self._old_x:
            self._update_bar(self.progressBar_x_minus, self._old_x - new_x)

        if new_y > self._old_y:
            self._update_bar(self.progressBar_y_plus, new_y - self._old_y)
        if new_y < self._old_y:
            self._update_bar(self.progressBar_y_minus, self._old_y - new_y)

        self._old_x = new_x
        self._old_y = new_y


def main():
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

mainwindow.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>800</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Mouse Pointer</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout_2">
    <item row="0" column="0">
     <layout class="QGridLayout" name="gridLayout">
      <item row="0" column="0">
       <widget class="QLabel" name="label_x_plus">
        <property name="text">
         <string>X+</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
        </property>
       </widget>
      </item>
      <item row="0" column="1">
       <widget class="QProgressBar" name="progressBar_x_plus">
        <property name="maximum">
         <number>1000</number>
        </property>
        <property name="value">
         <number>0</number>
        </property>
       </widget>
      </item>
      <item row="1" column="0">
       <widget class="QLabel" name="label_x_minus">
        <property name="text">
         <string>X-</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
        </property>
       </widget>
      </item>
      <item row="1" column="1">
       <widget class="QProgressBar" name="progressBar_x_minus">
        <property name="maximum">
         <number>1000</number>
        </property>
        <property name="value">
         <number>0</number>
        </property>
       </widget>
      </item>
      <item row="2" column="0">
       <widget class="QLabel" name="label_y_plus">
        <property name="text">
         <string>Y+</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
        </property>
       </widget>
      </item>
      <item row="2" column="1">
       <widget class="QProgressBar" name="progressBar_y_plus">
        <property name="maximum">
         <number>1000</number>
        </property>
        <property name="value">
         <number>0</number>
        </property>
       </widget>
      </item>
      <item row="3" column="0">
       <widget class="QLabel" name="label_y_minus">
        <property name="text">
         <string>Y-</string>
        </property>
        <property name="alignment">
         <set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
        </property>
       </widget>
      </item>
      <item row="3" column="1">
       <widget class="QProgressBar" name="progressBar_y_minus">
        <property name="maximum">
         <number>1000</number>
        </property>
        <property name="value">
         <number>0</number>
        </property>
       </widget>
      </item>
     </layout>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>28</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

回答1:


1.What's the reason for this behavior ?

Please write print(self._old_x, new_x) and see the result.

In my case, the part of results like this.

681 673
673 666
666 660
660 660
660 660
660 661
661 670
670 681
681 693
693 708
708 732
732 750
750 779

you can see the same position between self._old_x and new_x. and you can also see self._old_x is more than new_x in the result.

Why does the same position come?

It is easy to say... event.pos() is QCursor.pos(). But you call event.x() and event.y() at the first line in mouseMoveEvent().

Between event.x(), event.y() calling and new_x, new_y updating, there are a very slight time.So there is no time for moving 1 sometimes. This is also a reason but it is not core.

And please write print(progress_bar.objectName(), new_value) in _update_bar

progressBar_y_plus 394
progressBar_x_minus 368
progressBar_y_plus 400
progressBar_x_minus 372
progressBar_y_plus 405
progressBar_x_minus 375
progressBar_y_plus 409
progressBar_x_minus 378
progressBar_y_plus 412
progressBar_x_minus 381
progressBar_y_plus 415
progressBar_x_minus 383
progressBar_y_plus 418
progressBar_x_minus 387
progressBar_y_plus 424
progressBar_x_minus 389
progressBar_y_plus 427
progressBar_x_minus 394
progressBar_y_plus 431
progressBar_x_minus 397
progressBar_y_plus 434
progressBar_x_minus 400
progressBar_y_plus 438
progressBar_x_minus 404
progressBar_y_plus 443
progressBar_x_minus 409
progressBar_y_plus 449
progressBar_x_minus 414
progressBar_y_plus 454
progressBar_x_minus 419
progressBar_y_plus 458
progressBar_x_minus 423
progressBar_y_plus 463
progressBar_x_minus 428
progressBar_y_plus 468
progressBar_x_minus 436
progressBar_y_plus 476
progressBar_x_minus 440
progressBar_y_plus 479
progressBar_x_minus 448
progressBar_y_plus 484
progressBar_x_minus 456
progressBar_y_plus 489
progressBar_x_minus 462
progressBar_y_plus 493
progressBar_x_minus 467
progressBar_y_plus 495
progressBar_x_minus 475
progressBar_y_plus 497
progressBar_x_minus 482
progressBar_y_plus 500
progressBar_x_minus 489
progressBar_x_minus 503
progressBar_y_plus 504
progressBar_x_minus 515
progressBar_x_minus 529
progressBar_x_minus 541
progressBar_x_minus 559
progressBar_x_minus 575
progressBar_x_minus 598
progressBar_x_minus 619
progressBar_x_minus 631
progressBar_x_minus 657
progressBar_x_minus 675
progressBar_x_minus 679
progressBar_x_minus 682
progressBar_x_minus 707
progressBar_y_minus 807
progressBar_x_minus 742
progressBar_y_minus 818
progressBar_x_minus 767
progressBar_y_minus 819
progressBar_x_minus 799
progressBar_x_minus 814
progressBar_x_minus 825
progressBar_y_minus 822
progressBar_x_minus 842
progressBar_y_minus 823
progressBar_x_minus 857
progressBar_y_minus 825
progressBar_x_minus 863
progressBar_y_minus 826
progressBar_x_minus 877
progressBar_y_minus 831
progressBar_x_minus 892
progressBar_x_minus 907
progressBar_x_minus 918
progressBar_y_minus 835
progressBar_x_minus 928
progressBar_x_minus 931
progressBar_x_minus 936
progressBar_x_minus 940
progressBar_x_minus 945
progressBar_x_minus 949
progressBar_x_minus 952
progressBar_x_minus 956
progressBar_x_minus 959
progressBar_x_minus 963
progressBar_x_minus 970
progressBar_x_minus 975
progressBar_x_minus 980
progressBar_x_minus 987
progressBar_x_minus 992
progressBar_x_minus 998
progressBar_x_minus 1002
progressBar_x_minus 1001
progressBar_x_minus 1000
progressBar_x_minus 1004
progressBar_x_minus 1003
progressBar_x_minus 1001
progressBar_x_minus 1001
progressBar_x_minus 1001
progressBar_x_minus 1001
progressBar_x_plus 475
progressBar_x_plus 482
progressBar_x_plus 496
progressBar_x_plus 509
progressBar_x_plus 524
progressBar_x_plus 548
progressBar_x_plus 579
progressBar_x_plus 614
progressBar_x_plus 673
progressBar_x_plus 718
progressBar_x_plus 756
progressBar_x_plus 788
progressBar_x_plus 826
progressBar_x_plus 848
progressBar_y_minus 836
progressBar_x_plus 898
progressBar_y_plus 507
progressBar_x_plus 940
progressBar_x_plus 978
progressBar_x_plus 993
progressBar_x_plus 1014
progressBar_x_plus 1014
progressBar_x_plus 1010
progressBar_x_plus 1016
progressBar_x_plus 1015
progressBar_y_plus 509
progressBar_x_plus 1018
progressBar_x_plus 1012
progressBar_x_plus 1011
progressBar_x_plus 1000
progressBar_x_plus 1012
progressBar_x_plus 1019
progressBar_x_plus 1015
progressBar_x_plus 1012
progressBar_x_plus 1015
progressBar_x_plus 1019
progressBar_x_plus 1015
progressBar_x_plus 1015
progressBar_x_plus 1012
progressBar_x_plus 1014
progressBar_x_plus 1005
progressBar_x_plus 1003
progressBar_x_plus 1009
progressBar_y_minus 839
progressBar_x_plus 1008
progressBar_y_minus 841
progressBar_x_plus 1012
progressBar_y_minus 844
progressBar_x_plus 1011
progressBar_y_minus 848
progressBar_x_plus 1006
progressBar_y_minus 851
progressBar_x_plus 1007
progressBar_y_minus 855
progressBar_x_plus 1006
progressBar_y_minus 860
progressBar_x_plus 1007
progressBar_y_minus 865
progressBar_x_plus 1001
progressBar_y_minus 866
progressBar_x_plus 1001
progressBar_x_plus 1004
progressBar_y_minus 872
progressBar_x_plus 1005
progressBar_y_minus 881
progressBar_x_plus 1005
progressBar_y_minus 889
progressBar_x_plus 1007
progressBar_y_minus 898
progressBar_x_plus 1004
progressBar_y_minus 907
progressBar_x_plus 1005
progressBar_y_minus 917
progressBar_x_plus 1005
progressBar_y_minus 931
progressBar_x_plus 1004
progressBar_y_minus 941
progressBar_x_plus 1004
progressBar_y_minus 952
progressBar_x_plus 1005
progressBar_y_minus 965
progressBar_x_plus 1002
progressBar_y_minus 977
progressBar_x_plus 1001
progressBar_y_minus 991
progressBar_x_plus 1001
progressBar_y_minus 1004
progressBar_y_minus 1005
progressBar_x_plus 1003
progressBar_y_minus 1001
progressBar_y_minus 1007
progressBar_y_minus 1004
progressBar_y_minus 1005
progressBar_y_minus 1005
progressBar_x_minus 1003
progressBar_y_minus 1001
progressBar_x_minus 1004
progressBar_y_minus 1004
progressBar_y_minus 996
progressBar_x_minus 1006
progressBar_y_minus 1007
progressBar_x_minus 1008
progressBar_y_minus 1007
progressBar_x_minus 1007
progressBar_y_minus 1007
progressBar_x_minus 1008
progressBar_y_minus 1009
progressBar_x_minus 1009
progressBar_y_minus 1007
progressBar_x_minus 1005
progressBar_y_minus 1005
progressBar_x_minus 1010
progressBar_y_minus 1006
progressBar_x_minus 1007
progressBar_y_minus 1004
progressBar_x_minus 1010
progressBar_y_minus 1004
progressBar_x_minus 1008
progressBar_y_minus 1001
progressBar_x_minus 1008
progressBar_y_minus 1005
progressBar_x_minus 1013
progressBar_y_minus 1005
progressBar_x_minus 1012
progressBar_y_minus 1003
progressBar_x_minus 1023
progressBar_y_minus 1004
progressBar_x_minus 1013
progressBar_y_minus 1003
progressBar_x_minus 1016
progressBar_y_minus 1003
progressBar_x_minus 1015
progressBar_y_minus 1003
progressBar_x_minus 1014
progressBar_y_minus 1002
progressBar_x_minus 1018
progressBar_y_minus 1000
progressBar_x_minus 1015
progressBar_y_minus 1005
progressBar_x_minus 1009
progressBar_y_minus 1002
progressBar_x_minus 1018
progressBar_y_minus 1003
progressBar_x_minus 1015
progressBar_y_minus 1003
progressBar_x_minus 1014
progressBar_y_minus 1001
progressBar_x_minus 1015
progressBar_y_minus 1005
progressBar_x_minus 1009
progressBar_x_minus 1012
progressBar_y_minus 1001
progressBar_x_minus 1012
progressBar_x_minus 1014
progressBar_x_minus 1008
progressBar_x_minus 1014
progressBar_x_minus 1012
progressBar_x_minus 1015
progressBar_x_minus 1014
progressBar_x_minus 1012
progressBar_x_minus 1013
progressBar_x_minus 1014
progressBar_y_plus 512
progressBar_x_minus 1014
progressBar_y_plus 515
progressBar_x_minus 1014
progressBar_y_plus 519
progressBar_x_minus 1018
progressBar_y_plus 525
progressBar_x_minus 1009
progressBar_y_plus 528
progressBar_x_minus 1012
progressBar_y_plus 532
progressBar_x_minus 1013
progressBar_y_plus 536
progressBar_x_minus 1014
progressBar_y_plus 540
progressBar_x_minus 1010
progressBar_y_plus 546
progressBar_x_minus 1015
progressBar_y_plus 553
progressBar_x_minus 1012
progressBar_y_plus 558
progressBar_x_minus 1010
progressBar_y_plus 565
progressBar_x_minus 1012
progressBar_y_plus 572
progressBar_x_minus 1013
progressBar_y_plus 580
progressBar_x_minus 1008
progressBar_y_plus 586
progressBar_x_minus 1012
progressBar_y_plus 596
progressBar_x_minus 1010
progressBar_y_plus 604
progressBar_x_minus 1009
progressBar_y_plus 612
progressBar_x_minus 1010
progressBar_y_plus 623
progressBar_x_minus 1006
progressBar_y_plus 629
progressBar_x_minus 1007
progressBar_y_plus 637
progressBar_x_minus 1005
progressBar_y_plus 648
progressBar_x_minus 1003
progressBar_y_plus 655
progressBar_x_minus 1002
progressBar_y_plus 663
progressBar_x_minus 1003
progressBar_y_plus 673
progressBar_x_minus 1004
progressBar_y_plus 684
progressBar_x_minus 1001
progressBar_y_plus 693
progressBar_y_plus 702
progressBar_y_plus 712
progressBar_y_plus 713
progressBar_y_plus 718
progressBar_y_plus 724
progressBar_x_plus 1001
progressBar_y_plus 734
progressBar_x_plus 1002
progressBar_y_plus 743
progressBar_y_plus 757
progressBar_y_plus 769
progressBar_y_plus 780
progressBar_y_plus 794
progressBar_y_plus 803
progressBar_y_plus 815
progressBar_y_plus 822
progressBar_x_plus 1003
progressBar_y_plus 829
progressBar_x_plus 1002
progressBar_y_plus 836
progressBar_x_plus 1002
progressBar_y_plus 844
progressBar_x_plus 1004
progressBar_y_plus 850
progressBar_x_plus 1002
progressBar_y_plus 857
progressBar_x_plus 1004
progressBar_y_plus 865
progressBar_x_plus 1002
progressBar_y_plus 872
progressBar_x_plus 1006
progressBar_y_plus 880
progressBar_x_plus 1002
progressBar_y_plus 884
progressBar_x_plus 1002
progressBar_y_plus 893
progressBar_x_plus 1003
progressBar_y_plus 899
progressBar_x_plus 1003
progressBar_y_plus 905
progressBar_x_plus 1002
progressBar_y_plus 907
progressBar_x_plus 1002
progressBar_x_plus 1002
progressBar_y_plus 913
progressBar_x_plus 1001
progressBar_y_plus 918
progressBar_x_plus 1004
progressBar_y_plus 924
progressBar_x_plus 1001
progressBar_y_plus 927
progressBar_y_plus 931
progressBar_x_plus 1003
progressBar_y_plus 938
progressBar_x_plus 1002
progressBar_y_plus 941
progressBar_x_plus 1002
progressBar_y_plus 945
progressBar_x_plus 1004

Reason 1. I assume that progressBar counts 1000 as 100%.so 990 ~ 999 are counted as 99%. To stop at 99%, this often happens mousemoving get slower than before ( because the mouse point the edge of screen.) We can reach 100% very soon sometimes.

Reason 2.

You update new_x, new_y to self.old_x, self.old_y, so the valuables almost equal to each other.It seems to go up to 100 from 99 easily.For the most part, we can reach 100% soon.But the fact is not always so.Because even if you plus 990 + 11 , even if the point is more than 1000, the progress bar doesn't become 100%... This is the core reason to stop at 99%.

For the evidence, you write _update_bar,for example, new_value = 900 + 11 , any number you like but it must be more than 1000 as the result.Your progress bars absolutely don't become full.Rather, they don't move at all.

progressBar_y_minus 992
109 132
progressBar_y_minus 1012
98 121

I would you like to move mouse slowly.You can see you reach 100% easier than you move mouse faster.Because the problem was the movement width of 9** ~ 1000.

If you move the mouse faster, the self._pos_x and event.pos().x() will make a big width.For this, it results in not being able to reach 100% vice versa.

So If I move the mouse from the left position of my pc window to right position of it very fast,the progress bar doesn't increase at all.If you are at 99%, the effect appears clearly.

When you circle the moving of mouse, you often see 99%. But I think if you do it slowly, you can reach 100% with no problem. circling contains all direction moving.It is not for one progressbar. Until one moving ends, the next sometimes doesn't increase. I assume that circling makes the width little by little, it is easier to reach 99% than other moving. fake news.

It is not related with the desktop size.

2.How can the code be fixed so that no QProgessBar takes a break at 99% ?

If you understand the explanation of 1, you can escape from stopping by this code.

please try to write this code in _update_bar y punto...

if new_value > 1000:
   new_value = 1000


来源:https://stackoverflow.com/questions/55734585/mouse-movements-too-fast-for-mousemoveevent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!