I am using the given code, I want the user to enter text in the QLineEdit widget, press the Copy! button and see the inputted text replace the 'N/A' label. My questions is: following this procedure, how can I clear the text inputted in the QLineEdit widget with a simple mouse click?
From what I read (this, this and this) it seems like I need to reimplement focusInEvent() in a new class extending QLineEdit. My problem is that the code for my GUI has been imported from Qt Designer using pyuic5 and the examples cited above don't seem to take this in consideration.
Here is my code:
from PyQt5.QtWidgets import *
import sys
import QLineEdit_test
class MainWindow(QMainWindow, QLineEdit_test.Ui_QLineEdit_test):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.copy_button.clicked.connect(self.copy_and_print)
def copy_and_print(self):
self.label.setText(self.lineEdit.text())
def main():
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
if __name__ == "__main__":
main()
Here is my converted .ui file:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_QLineEdit_test(object):
def setupUi(self, QLineEdit_test):
QLineEdit_test.setObjectName("QLineEdit_test")
QLineEdit_test.resize(300, 200)
QLineEdit_test.setMaximumSize(QtCore.QSize(300, 200))
self.centralwidget = QtWidgets.QWidget(QLineEdit_test)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setMaximumSize(QtCore.QSize(120, 16777215))
self.lineEdit.setObjectName("lineEdit")
self.gridLayout.addWidget(self.lineEdit, 0, 0, 1, 1)
self.copy_button = QtWidgets.QPushButton(self.centralwidget)
self.copy_button.setObjectName("copy_button")
self.gridLayout.addWidget(self.copy_button, 1, 0, 1, 1)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setMaximumSize(QtCore.QSize(200, 20))
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.gridLayout.addWidget(self.label, 2, 0, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
QLineEdit_test.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(QLineEdit_test)
self.menubar.setGeometry(QtCore.QRect(0, 0, 300, 22))
self.menubar.setObjectName("menubar")
QLineEdit_test.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(QLineEdit_test)
self.statusbar.setObjectName("statusbar")
QLineEdit_test.setStatusBar(self.statusbar)
self.retranslateUi(QLineEdit_test)
QtCore.QMetaObject.connectSlotsByName(QLineEdit_test)
def retranslateUi(self, QLineEdit_test):
_translate = QtCore.QCoreApplication.translate
QLineEdit_test.setWindowTitle(_translate("QLineEdit_test", "MainWindow"))
self.copy_button.setText(_translate("QLineEdit_test", "Copy!"))
self.copy_button.setShortcut(_translate("QLineEdit_test", "Return"))
self.label.setText(_translate("QLineEdit_test", "N/A"))
The solution is to promote QtDesigner use our custom QLineEdit where we implement the signal clicked with the help of mousePressEvent, this class will be called ClickableLineEdit and the file will be called ClickableLineEdit.py.
ClickableLineEdit.py
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QLineEdit
class ClickableLineEdit(QLineEdit):
clicked = pyqtSignal()
def mousePressEvent(self, event):
self.clicked.emit()
QLineEdit.mousePressEvent(self, event)
To promote it, the following structure will be considered:
.
├── ClickableLineEdit.py
├── main.py
├── your.ui
└── QLineEdit_test.py
Open the design with Qt Designer and right click on the QLineEdit and select Promote to ...:
A menu will open and place the following
then press and Promote. Then we generate the code again.
Then we connect the signal to clear:
class MainWindow(QMainWindow, QLineEdit_test.Ui_QLineEdit_test):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.copy_button.clicked.connect(self.copy_and_print)
self.lineEdit.clicked.connect(self.lineEdit.clear)
def copy_and_print(self):
self.label.setText(self.lineEdit.text())
Update:
PySide2:
from PySide2 import QtCore, QtWidgets
class ClickableLineEdit(QtWidgets.QLineEdit):
clicked = QtCore.Signal()
def mousePressEvent(self, event):
super(ClickableLineEdit, self).mousePressEvent(event)
self.clicked.emit()
class App(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.lineedit = ClickableLineEdit()
self.lineedit.clicked.connect(self.lineedit.clear)
lay = QtWidgets.QVBoxLayout(self)
lay.addWidget(self.lineedit)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
ex = App()
ex.show()
sys.exit(app.exec_())
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
layout = QGridLayout()
self.setLayout(layout)
self.lineedit = QLineEdit()
self.lineedit.returnPressed.connect(self.press)
layout.addWidget(self.lineedit, 0, 0)
def press(self):
print("Hi World")
self.lineedit.clear()
If someone is still looking for a way to do this, but you only want the Line edit to be cleared when it first clicked and not every time it is clicked, you can do so without using variables like this :
def init_UI(self):
self.text_input = QLineEdit('Type your name')
self.text_input.mousePressEvent = self._mousePressEvent
def _mousePressEvent(self, event):
self.text_input.clear()
self.text_input.mousePressEvent = None
This way the _mousePressEvent gets called only once
I have an optional solution in one line:
self.lineEdit.mouseReleaseEvent = self.copy_and_print
Make sure you are receiving two parameters in your function (self,event)
I hope I helped you
Full post: https://wiki.python.org/moin/PyQt/Making%20non-clickable%20widgets%20clickable
Use mousePressEvent of QLineEdit to detect mouse click. To clear the text use clear() method or setText() method of QLineEdit.
#called when ever mouse is pressed
def mousePressed(self, event):
print('mouse pressed')
self.lineEdit=QLineEdit("Awesome day")#from PyQt5.QtWidget import QLineEdit
self.lineEdit.mousePressEvent = self.mousePressed
Example program :
import sys
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QApplication, QLineEdit
class ButtonPanel(QWidget):
def __init__(self, heading):
self.initUI(heading)
def initUI(self, heading):
super().__init__()
self.layout = QHBoxLayout()
self.lineEdit = QLineEdit(heading)
#self.lineEdit.setReadOnly(True)
self.lineEdit.returnPressed.connect(self.returnPressed)
self.lineEdit.mousePressEvent = self.mousePressed
self.delete = QPushButton("D")
self.layout.addWidget(self.lineEdit)
self.layout.addWidget(self.delete)
self.setLayout(self.layout)
self.show()
#called when mouse is clicked
def mousePressed(self, event):
self.lineEdit.clear() #text is cleared
//self.lineEdit.setText("") #this way we can also clear the text
print('mouse pressed')
//called when return key is pressed
def returnPressed(self):
print('return pressed')
if __name__ == "__main__":
app = QApplication(sys.argv)
b = ButtonPanel("Awesome")
sys.exit(app.exec())
Output :
Related
I am using QTextBrowser for printing data but want to check which line is clicked and get its string. I looked at other resources and was able to make this, but its not printing the line.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class TextBrowser(QtWidgets.QTextBrowser):
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseButtonPress:
tc = self.cursorForPosition(event.pos())
print("text = ", tc.block().text())
return super().eventFilter(source, event)
def setupUi(self, MainWindow):
MainWindow.resize(104, 105)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName(u"gridLayout_8")
self.code_textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.gridLayout.addWidget(self.code_textBrowser, 1, 0, 1, 1)
self.code_textBrowser.viewport().installEventFilter(self)
self.code_textBrowser.append("ab\ncde\nfghi\njklmn\nopqrstu")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
MainWindow.setMenuBar(self.menubar)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = TextBrowser()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
One of the reasons why I do not recommend modifying the code generated by .ui is that it causes confusion so for my possible solution you must restore that file (run pyuic again) and call the generated file as gui.py.
The problem is that the cursorForPosition returns the cursor associated with "TextBrowser" (in your case "ui") and not with "code_textBrowser", that a class inherits from class T does not imply that all attributes are of the same class T they will be the same.
The solution is to implement the logic in another class that inherits from a QObject like QMainWindow and implement the logic using code_textBrowser.
import sys
from PyQt5.QtCore import QEvent
from PyQt5.QtWidgets import QApplication, QMainWindow
from gui import Ui_MainWindow
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.code_textBrowser.viewport().installEventFilter(self)
def eventFilter(self, obj, event):
if (
obj is self.ui.code_textBrowser.viewport()
and event.type() == QEvent.MouseButtonPress
):
tc = self.ui.code_textBrowser.cursorForPosition(event.pos())
print("text = ", tc.block().text())
return super().eventFilter(obj, event)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
How to make a loop and AutoClosed messagebox in PyQt5?
Here is a kind of messagebox that can auto close after 3sec.
I want to show the message by using it in a loop.
How can i do it?
PS:the code is not writen by me
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, Qt
import time
class Ui_Message(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(538, 91)
self.frame = QtWidgets.QFrame(Form)
self.frame.setGeometry(QtCore.QRect(0, 0, 541, 111))
# self.frame.setStyleSheet("background-image: url(:/img/messageback.png);")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(0, 0, 531, 91))
font = QtGui.QFont()
font.setPointSize(31)
font.setBold(False)
font.setWeight(50)
self.label.setFont(font)
self.label.setStyleSheet("background-color: transparent;\n"
"fontsize: 30px;")
self.label.setAlignment(QtCore.Qt.AlignCenter)
self.label.setObjectName("label")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.label.setText(_translate("Form", "显示信息"))
# import img_rc
# Define a removable borderless 3S prompt message interface
class MessageWindow(Qt.QMainWindow):
def __init__(self, parent=None):
Qt.QWidget.__init__(self, parent)
self.ui = Ui_Message()
self.ui.setupUi(self)
self.setWindowFlags(Qt.Qt.FramelessWindowHint)
QtCore.QTimer().singleShot(3000, self.close)
self.show()
def mousePressEvent(self, event):
# Define mouse click events
if event.button() == QtCore.Qt.LeftButton:
self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
event.accept()
def mouseMoveEvent(self, event):
# Define mouse movement events
if event.buttons() == QtCore.Qt.LeftButton:
self.move(event.globalPos() - self.dragPosition)
event.accept()
def setMessage(self, message):
self.ui.label.setText(message)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
login = MessageWindow()
#----how can i maike a loop messagebox?------
for i in range(0,5):
login.setMessage("Number"+str(i))
time.sleep(3)
sys.exit(app.exec())
I know maybe i can't use time.sleep in PyQt5,however i had search for anything but not get the answer.
In Qt if you want to do a periodic task then you must use a QTimer, and forget about synchronous logic since you must work through events.
In this case a possible solution is to use a queue that stores the information and in each shot of the timer an element is obtained.
from collections import deque
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
login = MessageWindow()
values = range(0, 5)
q = deque(values)
def on_timeout():
print(q)
if q:
i = q.popleft()
Qt.QTimer.singleShot(3000, on_timeout)
login.setMessage("Number" + str(i))
on_timeout()
sys.exit(app.exec())
Notes:
Your class must not inherit from QMainWindow but from QWidget.
You must eliminate the timer that closes the window, otherwise you will not see the text change since the time is very short.
I have used Qt designer to create two different windows, input_window.ui and help_window.ui. Here is the python scripts for showing the input window. In input window, there is a menu bar("About>>Help"). How could it pop up a help_window when "Help" is clicked?
Here is init.py
import sys
from input_window import Ui_MainWindow
from PyQt5.QtWidgets import QMainWindow, QApplication
from help_window import Ui_Help
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.Pophelp.triggered.connect(self.Ui_Help)
def help_window(self):
self.window=Ui_Help()
self.window.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
Here is the code of Ui_Help
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Help(object):
def setupUi(self, Help):
Help.setObjectName("Help")
Help.resize(251, 99)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Help.setWindowIcon(icon)
self.gridLayoutWidget = QtWidgets.QWidget(Help)
self.gridLayoutWidget.setGeometry(QtCore.QRect(9, 9, 231, 81))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.gridLayoutWidget)
font = QtGui.QFont()
font.setFamily("Times New Roman")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.plainTextEdit.setFont(font)
self.plainTextEdit.setFrameShape(QtWidgets.QFrame.WinPanel)
self.plainTextEdit.setFrameShadow(QtWidgets.QFrame.Sunken)
self.plainTextEdit.setLineWidth(1)
self.plainTextEdit.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustIgnored)
self.plainTextEdit.setReadOnly(True)
self.plainTextEdit.setObjectName("plainTextEdit")
self.gridLayout.addWidget(self.plainTextEdit, 0, 0, 1, 1)
self.retranslateUi(Help)
QtCore.QMetaObject.connectSlotsByName(Help)
Qt Designer serves to implement the view in a simple way, and therefore the class that generates is oriented to the view, and our job is to implement the logic like you did with Ui_MainWindow and MainWindow, similarly you do with Ui_Help. In your case I recommend that when you have built help_window.ui you would have used the Dialog template, but if you chose the Widget template there is no problem, both are very compatible.
A simple solution is to create a QDialog and implement in it the Ui_Help view as shown below:
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.Pophelp.triggered.connect(self.help_window)
def help_window(self):
# If you pass a parent (self) will block the Main Window,
# and if you do not pass both will be independent,
# I recommend you try both cases.
widget = QDialog(self)
ui=Ui_Help()
ui.setupUi(widget)
widget.exec_()
If in the Ui_Help you want to implement some logic I recommend creating a class similar to MainWindow as shown below:
class Help(QDialog, Ui_Help):
def __init__(self, parent=None):
super(Help, self).__init__(parent)
self.setupUi(self)
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.Pophelp.triggered.connect(self.help_window)
def help_window(self):
widget = Help()
widget.exec_()
You didn't include input_window.ui so it's hard to fully replicate what you are doing, but I think the main problem you have is stemming from this line:
self.Pophelp.triggered.connect(self.Ui_Help)
You don't want to connect the button to Ui_Help, you want to connect it to self.help_window.
It works if change the help_window to the code below.
def help_window(self):
dialog=QtWidgets.QDialog()
dialog.ui=Ui_Help()
dialog.ui.setupUi(dialog)
dialog.exec_()
dialog.show()
My current code below works for updating the x-y coordinates in 2 textBrowsers in my MainWindow, but it doesn't work when the cursor is inside of the textBrowsers.
For this example, I want the coordinates to ONLY update when the cursor is moving inside of textBrowser_1 and nowhere else.
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtGui import QApplication, QMainWindow
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(800, 132)
self.centralwidget = QtGui.QWidget(MainWindow)
self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.textBrowser_1 = QtGui.QTextBrowser(self.centralwidget)
self.horizontalLayout.addWidget(self.textBrowser_1)
self.textBrowser_2 = QtGui.QTextBrowser(self.centralwidget)
self.horizontalLayout.addWidget(self.textBrowser_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
MainWindow.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
class MyMainScreen(QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow() # This is from a python export from QtDesigner
self.ui.setupUi(self)
self.setMouseTracking(True)
self.ui.textBrowser_1.installEventFilter(self)
# self.ui.textBrowser_1.setMouseTracking(True)
# self.ui.menubar.setMouseTracking(True)
# self.ui.statusbar.setMouseTracking(True)
def setMouseTracking(self, flag):
def recursive_set(parent):
for child in parent.findChildren(QtCore.QObject):
try:
child.setMouseTracking(flag)
except:
pass
recursive_set(child)
QtGui.QWidget.setMouseTracking(self, flag)
recursive_set(self)
def mouseMoveEvent(self, event):
self.ui.textBrowser_1.setText(str(event.x()))
self.ui.textBrowser_2.setText(str(event.y()))
QtGui.QMainWindow.mouseMoveEvent(self, event)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainscreen = MyMainScreen()
mainscreen.show()
app.exec_()
This is what the program looks like:
mouseTest
From your code example, it looks like you may have already tried an event-filter, but that is probably the best solution. The trick is to install it on the viewport of the widget (if it has one):
class MyMainScreen(QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.textBrowser_1.setMouseTracking(True)
self.ui.textBrowser_1.viewport().installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseMove:
self.ui.textBrowser_1.setText(str(event.x()))
self.ui.textBrowser_2.setText(str(event.y()))
return QtGui.QMainWindow.eventFilter(self, source, event)
I am using a PyQt4.QMainWindow as my application interface, and I want to get the x and y coordinates of the mouse inside of a QWidget and set them continuously in 2 textBrowsers in the MainWindow.
The documentation for QWidget is here.
and the documentation for QMouseEvent is here.
Here is the code
from PyQt4 import QtGui
from PyQt4.QtGui import QApplication
import sys
class Ui_MainWindow(object):
def setupUI(self, MainWindow):
self.textBrowser_1 = QtGui.QTextBrowser(self.tab)
self.textBrowser_2 = QtGui.QTextBrowser(self.tab)
self.widget_1 = QtGui.QWidget(self.tab)
self.widget_1.setMouseTracking(True)
class MyMainScreen(QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow() # This is from a python export from QtDesigner
# There is a QWidget inside that is self.ui.widget_1
# and 2 textBrowsers, textBrowser_1 and textBrowser_2
# I want to populate these 2 textBrowsers with the current x,y
# coordinates.
if __name__ == "__main__":
app = QApplication(sys.argv)
mainscreen = MyMainScreen()
mainscreen.show()
app.exec_()
When you apply setMouseTracking it only applies to that widget, and not to your children, so you must manually, in the next solution:
def setMouseTracking(self, flag):
def recursive_set(parent):
for child in parent.findChildren(QtCore.QWidget):
child.setMouseTracking(flag)
recursive_set(child)
QtGui.QWidget.setMouseTracking(self, flag)
recursive_set(self)
complete code:
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtGui import QApplication, QMainWindow
import sys
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.resize(800, 132)
self.centralwidget = QtGui.QWidget(MainWindow)
self.horizontalLayout = QtGui.QHBoxLayout(self.centralwidget)
self.textBrowser_1 = QtGui.QTextBrowser(self.centralwidget)
self.horizontalLayout.addWidget(self.textBrowser_1)
self.textBrowser_2 = QtGui.QTextBrowser(self.centralwidget)
self.horizontalLayout.addWidget(self.textBrowser_2)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
MainWindow.setStatusBar(self.statusbar)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
class MyMainScreen(QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.ui = Ui_MainWindow() # This is from a python export from QtDesigner
self.ui.setupUi(self)
self.setMouseTracking(True)
self.ui.textBrowser_1.setMouseTracking(True)
self.ui.textBrowser_2.setMouseTracking(True)
self.ui.menubar.setMouseTracking(True)
self.ui.statusbar.setMouseTracking(True)
def setMouseTracking(self, flag):
def recursive_set(parent):
for child in parent.findChildren(QtCore.QWidget):
child.setMouseTracking(flag)
recursive_set(child)
QtGui.QWidget.setMouseTracking(self, flag)
recursive_set(self)
def mouseMoveEvent(self, event):
pos = event.pos()
self.ui.textBrowser_1.append(str(pos.x()))
self.ui.textBrowser_2.append(str(pos.y()))
QtGui.QMainWindow.mouseMoveEvent(self, event)
if __name__ == "__main__":
app = QApplication(sys.argv)
mainscreen = MyMainScreen()
mainscreen.show()
app.exec_()
This is my output: