Q:

Connect signal after hold mouse button for 10 secs PyQt Python

I want to send "connect" signal after clicking and holding a button for certain time. After that time pass (f.e 10 seconds) the action or event will be triggerd even if button is not released. It could be  combinations of clicking or holdin for a while or anything different then just click and releasing the button.

A:

Here is the code I tested on PyQt4
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import * 

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        #Creation of a sample Window
        self.textLabel = QLabel()
        self.textLabel.setText('This is a Sample Window')
        box = QHBoxLayout(self)
        box.addWidget(self.textLabel)

        #Create a Timer
        self.timer = QTimer()
        self.timer.timeout.connect(lambda: self.mouse_event_check())
        self.held_time = 0

        #Create Sample Button
        self.button = QPushButton('SAMPLE BUTTON')
        box.addWidget(self.button)

        #Connect Signals Pressed and Released
        self.button.pressed.connect(self.button_pressed)
        self.button.released.connect(self.button_released)

        self.timer_button = QTimer()
        self.timer_button.timeout.connect(lambda: self.button_event_check())
        self.button_held_time = 0

    # =========================================================================
    # Implementation for Buttons
    # =========================================================================
    def button_pressed(self):
        self.timer_button.start(50)

    def button_released(self):
        self.timer_button.stop()
        print('Button Held for {:.4f} seconds'.format(self.button_held_time))
        self.button.setText('{:.4f} seconds'.format(self.button_held_time))
        self.button_held_time = 0 

    def button_event_check(self):
        self.button_held_time += 0.05

    # =========================================================================
    # Implmentation for Mouse Held on Window
    # =========================================================================
    def mousePressEvent(self, mouse_event):
        self.timer.start(50)

    def mouse_event_check(self):
        self.held_time += 0.05

    def mouseReleaseEvent(self, mouse_event):   
        self.timer.stop()
        print('Mouse Held for {:.4f} seconds'.format(self.held_time))
        self.textLabel.setText('Mouse Held for {:.4f} seconds'.format(self.held_time))

        self.held_time = 0

if __name__ == '__main__':  
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())   

Algorithm for Mouse Hold on Window

Create a held_time variable. Set it to 0
Create a Timer
Connect timeout() signal of timer to a function (mouse_event_check)
Reimplement MousePressEvent()

Start the Timer (50msec)

Reimplement MouseReleaseEvent()

Stop the Timer
Reset held_time to 0

Implement mouse_event_check()

Increase the held_time variable by 0.05

Algorithm for Mouse Hold on Button

Create a button_held_time variable. Set it to 0
Create a Timer
Connect timeout() signal of timer to a function (button_event_check)
Connect the button signals pressed and released to some callables
Implement pressed method

Start timer (50msec)

Implement released method

Stop Timer
Reset button_held_time to 0

Implement button_event_check()

Increase the held_time variable by 0.05

