[PYTHON 3.8] How to transfer found pyqt code to pyside2? [BEGINNER]
-
I would like to know what do I have to do in order to convert found pyqt code when I research stuff in google to pyside2 for my project? For example I want to add mousePressEvent to my project and connect it to QLineEdit so when I click QLineEdit it select all of my text with selectAll() but I don't know how to do that with signals and just adding mousePressEvent function inside parent class works only if you click on QWidget and won't work if you click on QLineEdit.
I am sorry if this is going to be a duplicate post but please share tips of what to do when you find pyqt code snippet that you want but your project is in pyside2.
Source of what I'm looking at right now and want: https://stackoverflow.com/questions/25560748/add-a-click-on-qlineedit
What I want is this code to pyside:mousePressed = QtCore.pyqtProperty(QtGui.QMouseEvent) self.tc.mousePressed[QtGui.QMouseEvent].connect(self.mousePressEvent)
Edit (doing this with eventFilter so I don't have to create QLineEdit subclass but the problem is the selectAll() is not selecting the text):
# This Python file uses the following encoding: utf-8 import youtube_dl import sys import os from PySide2.QtWidgets import (QApplication, QWidget, QPushButton, QLineEdit, QFileDialog, QLabel, QProgressBar, QCheckBox, QMessageBox) from PySide2.QtCore import QFile, QThread, Signal from PySide2.QtUiTools import QUiLoader from PySide2 import QtXml from PySide2.QtWinExtras import QtWin from PySide2 import QtGui import Resource class GUI(QWidget): def __init__(self): super(GUI, self).__init__() self.load_ui() self.setFixedSize(607, 154) ... self.downloadURL = self.findChild(QLineEdit, "lineEditURL") self.downloadURL.installEventFilter(self) ... def load_ui(self): loader = QUiLoader() path = os.path.join(os.path.dirname(__file__), "form.ui") ui_file = QFile(path) ui_file.open(QFile.ReadOnly) loader.load(ui_file, self) ui_file.close() ... def mousePressEvent(self, event): print("testing123") super(GUI, self).mousePressEvent(event) def eventFilter(self, obj, event): if event.type() == event.MouseButtonPress: self.downloadURL.selectAll() print("test") return super(GUI, self).eventFilter(obj, event) ... if __name__ == "__main__": app = QApplication([]) app.setWindowIcon(QtGui.QIcon(":/icon/window_icon.ico")) widget = GUI() widget.show() sys.exit(app.exec_())
Output:
test test testing123 test testing123 test testing123 testing123 testing123 test testing123 test testing123 test test test testing123 test test test test test
It works but selectAll() doesn't work and I don't know what to do.
-
Hi,
In this case, you should let the mouseButtonPress do its job first and only then call selectAll on your QLineEdit.