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:
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_())
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 :
So I have this Ui_MainWindow class generated by pyuic5.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.9
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 450)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.canvas = QtWidgets.QWidget(self.centralWidget)
self.canvas.setGeometry(QtCore.QRect(70, 30, 621, 341))
self.canvas.setAutoFillBackground(False)
self.canvas.setObjectName("canvas")
self.gview = QtWidgets.QGraphicsView(self.canvas)
self.gview.setGeometry(QtCore.QRect(80, 50, 256, 192))
self.gview.setObjectName("gview")
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menuBar.setObjectName("menuBar")
self.menuhola = QtWidgets.QMenu(self.menuBar)
self.menuhola.setObjectName("menuhola")
self.menust = QtWidgets.QMenu(self.menuBar)
self.menust.setObjectName("menust")
MainWindow.setMenuBar(self.menuBar)
self.menuBar.addAction(self.menuhola.menuAction())
self.menuBar.addAction(self.menust.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menuhola.setTitle(_translate("MainWindow", "hola"))
self.menust.setTitle(_translate("MainWindow", "splendid"))
And I want to paint on something, as long as it's not the main window. However, no matter what I try, it just doesn't work.
Here's my code:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtGui import QPainter
from src.qt_design.main_window import Ui_MainWindow
class SystemApp(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def paintEvent(self, event):
# Nope
painter = QPainter(self.ui.gview)
painter.drawLine(0, 0, 50, 50)
# Nope
painter = QPainter(self.ui.canvas)
painter.drawLine(0, 0, 50, 50)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = SystemApp()
window.show()
sys.exit(app.exec_())
I'm not sure if I should either:
1. Paint with this parent on its children (like what I tried above)
2. Override children's paintEvent, but it's maybe a bad idea, see here
Any help will be appreciated!
You can get what you are trying to accomplish with an event filter
class SystemApp(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.gview.installEventFilter(self)
self.ui.canvas.installEventFilter(self)
def eventFilter(self, obj, e):
if e.type() == QtCore.QEvent.Paint:
painter = QPainter()
painter.begin(obj)
if obj == self.ui.canvas:
painter.setPen(QtCore.Qt.red) # Some specific painting
painter.drawLine(0, 0, 50, 50)
painter.end()
return True
return super().eventFilter(obj, e)
I have three windows designed in QtDesigner. The main window calls the two childs windows using buttons. If I close the main window the childs windows also close, I did this by overwriting the closeEvent in the main window. I need to implement some stuff in the closeEvent of the child window so I overwritten the closeEvent of the child class but it does nothing. Please help.
Class of the main window made in Qt Designer
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_ventanaPrincipal(object):
def setupUi(self, ventanaPrincipal):
ventanaPrincipal.setObjectName("ventanaPrincipal")
ventanaPrincipal.resize(267, 238)
self.centralWidget = QtWidgets.QWidget(ventanaPrincipal)
self.centralWidget.setObjectName("centralWidget")
self.buttonVentana1 = QtWidgets.QPushButton(self.centralWidget)
self.buttonVentana1.setGeometry(QtCore.QRect(60, 30, 141, 25))
self.buttonVentana1.setObjectName("buttonVentana1")
self.buttonVentana2 = QtWidgets.QPushButton(self.centralWidget)
self.buttonVentana2.setGeometry(QtCore.QRect(60, 80, 141, 25))
self.buttonVentana2.setObjectName("buttonVentana2")
ventanaPrincipal.setCentralWidget(self.centralWidget)
self.menuBar = QtWidgets.QMenuBar(ventanaPrincipal)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 267, 22))
self.menuBar.setObjectName("menuBar")
ventanaPrincipal.setMenuBar(self.menuBar)
self.mainToolBar = QtWidgets.QToolBar(ventanaPrincipal)
self.mainToolBar.setObjectName("mainToolBar")
ventanaPrincipal.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtWidgets.QStatusBar(ventanaPrincipal)
self.statusBar.setObjectName("statusBar")
ventanaPrincipal.setStatusBar(self.statusBar)
self.retranslateUi(ventanaPrincipal)
QtCore.QMetaObject.connectSlotsByName(ventanaPrincipal)
def retranslateUi(self, ventanaPrincipal):
_translate = QtCore.QCoreApplication.translate
ventanaPrincipal.setWindowTitle(_translate("ventanaPrincipal", "ventanaPrincipal"))
self.buttonVentana1.setText(_translate("ventanaPrincipal", "Ventana 1"))
self.buttonVentana2.setText(_translate("ventanaPrincipal", "Ventana 2"))
Class of first child window
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_subVen1(object):
def setupUi(self, subVen1):
subVen1.setObjectName("subVen1")
subVen1.resize(320, 347)
self.label = QtWidgets.QLabel(subVen1)
self.label.setGeometry(QtCore.QRect(40, 20, 141, 17))
self.label.setObjectName("label")
self.listWidget = QtWidgets.QListWidget(subVen1)
self.listWidget.setGeometry(QtCore.QRect(20, 60, 256, 192))
self.listWidget.setObjectName("listWidget")
self.retranslateUi(subVen1)
QtCore.QMetaObject.connectSlotsByName(subVen1)
def retranslateUi(self, subVen1):
_translate = QtCore.QCoreApplication.translate
subVen1.setWindowTitle(_translate("subVen1", "Form"))
self.label.setText(_translate("subVen1", "MIAU MIAU"))
Class of second child window
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_subVen2(object):
def setupUi(self, subVen2):
subVen2.setObjectName("subVen2")
subVen2.resize(320, 304)
self.listView = QtWidgets.QListView(subVen2)
self.listView.setGeometry(QtCore.QRect(40, 60, 256, 192))
self.listView.setObjectName("listView")
self.label = QtWidgets.QLabel(subVen2)
self.label.setGeometry(QtCore.QRect(80, 20, 121, 17))
self.label.setObjectName("label")
self.retranslateUi(subVen2)
QtCore.QMetaObject.connectSlotsByName(subVen2)
def retranslateUi(self, subVen2):
_translate = QtCore.QCoreApplication.translate
subVen2.setWindowTitle(_translate("subVen2", "Form"))
self.label.setText(_translate("subVen2", "Guau Guau"))
File that calls the other three.
import sys
#import classes-----------------------
from HMIs.ventanaprincipal import Ui_ventanaPrincipal, QtWidgets
from HMIs.subventana1 import Ui_subVen1
from HMIs.subventana2 import Ui_subVen2
# hijaSub1 inherits the first child window class made by the QtDesigner
#override child closeEvent
class hijaSub1(Ui_subVen1):
def closeEvent(self, event):
print("X is clicked")
class multiVen(QtWidgets.QMainWindow):
def __init__(self,parent=None):
QtWidgets.QWidget.__init__(self,parent=None)
self.ui =Ui_ventanaPrincipal()
self.ui.setupUi(self)
self.subV1=QtWidgets.QWidget()
self.subV2=QtWidgets.QWidget()
#Conect signals whith slots--------------------
self.ui.buttonVentana1.clicked.connect(self.muestraVentana1)
self.ui.buttonVentana2.clicked.connect(self.muestraVentana2)
# slots-----------------------------------------
def muestraVentana1(self):
self.wid1=hijaSub1()
self.wid1.setupUi(self.subV1)
self.subV1.show()
def muestraVentana2(self):
self.wid2=Ui_subVen2()
self.wid2.setupUi(self.subV2)
self.subV2.show()
#Close all windows whith X button of main window
#override main closeEvent
def closeEvent(self, event):
self.subV1.close()
self.subV2.close()
event.accept()
if __name__=="__main__":
app=0
app=QtWidgets.QApplication(sys.argv)
myapp=multiVen()
myapp.show()
sys.exit(app.exec_())
The closeEvent method has the classes that inherit from QWidget, in which case the daughter classes Sub1, Ui_subVen1, Ui_subVen2 inherit from object, not from QWidget.
If you want to implement closeEvent in windows, create a class that uses the window view (Ui_xxx) and inherits from QWidget.
class hijaSub1(QtWidgets.QWidget, Ui_subVen1):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent=parent)
self.setupUi(self)
def closeEvent(self, event):
print("X is clicked: hijaSub1")
class hijaSub2(QtWidgets.QWidget, Ui_subVen2):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent=parent)
self.setupUi(self)
def closeEvent(self, event):
print("X is clicked: hijaSub2")
class multiVen(QtWidgets.QMainWindow):
def __init__(self,parent=None):
QtWidgets.QWidget.__init__(self,parent=None)
self.ui =Ui_ventanaPrincipal()
self.ui.setupUi(self)
self.subV1=hijaSub1()
self.subV2=hijaSub2()
#Conect signals whith slots--------------------
self.ui.buttonVentana1.clicked.connect(self.muestraVentana1)
self.ui.buttonVentana2.clicked.connect(self.muestraVentana2)
# slots-----------------------------------------
def muestraVentana1(self):
self.subV1.show()
def muestraVentana2(self):
self.subV2.show()
#Close all windows whith X button of main window
#override main closeEvent
def closeEvent(self, event):
self.subV1.close()
self.subV2.close()
event.accept()
if __name__=="__main__":
import sys
app=0
app=QtWidgets.QApplication(sys.argv)
myapp=multiVen()
myapp.show()
sys.exit(app.exec_())
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)