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_())
Related
I'm using a USB-barcode scanner to set the text of a Qt lineEdit field to scan production order number, My issue is that after scanning the window is closed instead of shifting to next lineEdit_2 to scan item number. How to setup lineEdit to shift the crusor to next lineEdit_2 and be ready for the next scan.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(290, 20, 81, 241))
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(50, 50, 113, 22))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_2.setGeometry(QtCore.QRect(50, 120, 113, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(Dialog)
self.lineEdit_3.setGeometry(QtCore.QRect(50, 200, 113, 22))
self.lineEdit_3.setObjectName("lineEdit_3")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
By default the buttons of a QPushButton are pressed when the enter key is clicked and the QDialog has focus, and that click causes the rejected or accepted signal to be emitted, which closes the window, and this is handled by the default and autoDefault properties, so the first thing is to override that behavior.
On the other hand, pressing the enter key does not move to the other QLineEdit, in that case you must listen to that key and use focusNextPrevChild to move the focus.
Finally the codes generated by QtDesigner should not be modified so I will assume that the code you show is in the gui.py file, and I will implement the logic in main.py:
import sys
from PyQt5 import QtCore, QtWidgets
from gui import Ui_Dialog
class Dialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(self)
self.show()
for btn in self.buttonBox.buttons():
btn.setDefault(False)
btn.setAutoDefault(False)
btn.setFocusPolicy(QtCore.Qt.NoFocus)
def keyPressEvent(self, event):
if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):
self.focusNextPrevChild(True)
super().keyPressEvent(event)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Dialog()
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 :
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:
I am making a GUI using qt and trying incorporate a LineEdit with Vertical Orientation.
When using the code:
import sys
from PyQt4 import QtCore, QtGui
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(250, 250)
self.lineEdit = QtGui.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(40, 80, 120, 20))
self.lineEdit.setObjectName("lineEdit")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(_translate("Dialog", "Dialog #1", None))
class themain(QtGui.QDialog, Ui_Dialog):
def __init__(self, parent=None):
super(themain,self).__init__(parent)
self.setupUi(self)
self.lineEdit.setText('Some Text')
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
form = themain()
form.show()
sys.exit(app.exec_())
I get a simple LineEdit with Horizontal Orientation.
In essence I need something that looks like:
[Image][1]
http://users.ntua.gr/anthpro/images/dialog.png
(The image has been photoshopped to show the lineedit in vertical orientation)