Помогите определить активный виджет, пример работающего варианта прилагаю в начале, опираясь на который пытаюсь реализовать свой. Но только добился отрисовки без функциональных возможностей. Задача определить номер активного виджета. (это мне надо для виртуальной клавиатуры). Конкретно проблема когда добавляю конструктор класса в класс MyQPlainTextEdit, перестает работать, этим действием хочу добавить пре менную id для определения активного виджета.
Пример:
from PyQt5 import QtWidgets
class MyLineEdit(QtWidgets.QPlainTextEdit):
def __init__(self, id):
QtWidgets.QPlainTextEdit.__init__(self)
self.id = id
def focusInEvent(self, e):
print("Получен фокус полем", self.id)
QtWidgets.QPlainTextEdit.focusInEvent(self, e)
def focusOutEvent(self, e):
print("Потерян фокус полем", self.id)
QtWidgets.QPlainTextEdit.focusOutEvent(self, e)
class MyWindow(QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.resize(300, 100)
self.line1 = MyLineEdit(1)
self.line2 = MyLineEdit(2)
self.line3 = MyLineEdit(3)
self.vbox = QtWidgets.QVBoxLayout()
self.vbox.addWidget(self.line1)
self.vbox.addWidget(self.line2)
self.vbox.addWidget(self.line3)
self.setLayout(self.vbox)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
моя попытка реализации кода:
from PyQt5 import QtWidgets
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton
from PyQt5.QtWidgets import QVBoxLayout, QLabel
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QTimer, QTime, Qt, QDateTime
from PyQt5 import QtCore, QtGui, QtWidgets
class MyQPlainTextEdit(QtWidgets.QPlainTextEdit):
def __init__(self, id):
QtWidgets.QPlainTextEdit.__init__(self)
self.id = id
def focusInEvent(self, e):
print("Получен фокус полем", self.id)
self.setStyleSheet("background-color: yellow; color: red;")
QtWidgets.QPlainTextEdit.focusInEvent(self, e)
def focusOutEvent(self, e):
print("Потерян фокус полем", self.id)
self.setStyleSheet("background-color: white; color: black;")
QtWidgets.QPlainTextEdit.focusOutEvent(self, e)
class MyWindow(QtWidgets.QWidget): # object,
def __init__(self):
super().__init__()
self.setupUi(self)
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(801, 208)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(590, 150, 191, 41))
font = QtGui.QFont()
font.setPointSize(12)
self.pushButton.setFont(font)
self.pushButton.setStyleSheet("QPushButton {\n"
" \n"
" background-color: rgb(73, 71, 71);\n"
" color: rgb(255, 255, 255);\n"
" \n"
"}\n"
"\n"
"QPushButton:pressed {\n"
" background-color: rgb(118, 118, 118);\n"
"}")
self.pushButton.setObjectName("pushButton")
self.label_3 = QtWidgets.QLabel(Form)
self.label_3.setGeometry(QtCore.QRect(10, 130, 51, 17))
self.label_3.setObjectName("label_3")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(10, 40, 151, 21))
self.label.setObjectName("label")
self.label_7 = QtWidgets.QLabel(Form)
self.label_7.setGeometry(QtCore.QRect(310, 130, 67, 17))
self.label_7.setObjectName("label_7")
self.label_9 = QtWidgets.QLabel(Form)
self.label_9.setGeometry(QtCore.QRect(580, 39, 201, 21))
self.label_9.setObjectName("label_9")
self.label_5 = QtWidgets.QLabel(Form)
self.label_5.setGeometry(QtCore.QRect(310, 40, 121, 17))
self.label_5.setObjectName("label_5")
self.label_11 = QtWidgets.QLabel(Form)
self.label_11.setGeometry(QtCore.QRect(-4, -1, 811, 31))
self.label_11.setLayoutDirection(QtCore.Qt.RightToLeft)
self.label_11.setStyleSheet("background-color: rgb(73, 71, 71);\n"
" color: rgb(255, 255, 255);")
self.label_11.setAlignment(QtCore.Qt.AlignCenter)
self.label_11.setObjectName("label_11")
self.plainTextEdit = MyQPlainTextEdit(Form)
# self.plainTextEdit = QtWidgets.QPlainTextEdit(Form)
self.plainTextEdit.setGeometry(QtCore.QRect(10, 60, 291, 70))
self.plainTextEdit.setStyleSheet("background-color: rgb(243, 243, 243);")
self.plainTextEdit.setPlainText("")
self.plainTextEdit.setObjectName("plainTextEdit")
self.plainTextEdit_2 = MyQPlainTextEdit(Form)
# self.plainTextEdit_2 = QtWidgets.QPlainTextEdit(Form)
self.plainTextEdit_2.setGeometry(QtCore.QRect(10, 150, 291, 51))
self.plainTextEdit_2.setStyleSheet("background-color: rgb(243, 243, 243);")
self.plainTextEdit_2.setObjectName("plainTextEdit_2")
self.plainTextEdit_3 = MyQPlainTextEdit(Form)
# self.plainTextEdit_3 = QtWidgets.QPlainTextEdit(Form)
self.plainTextEdit_3.setGeometry(QtCore.QRect(310, 60, 261, 70))
self.plainTextEdit_3.setStyleSheet("background-color: rgb(243, 243, 243);")
self.plainTextEdit_3.setObjectName("plainTextEdit_3")
self.plainTextEdit_4 = MyQPlainTextEdit(Form)
# self.plainTextEdit_4 = QtWidgets.QPlainTextEdit(Form)
self.plainTextEdit_4.setGeometry(QtCore.QRect(310, 150, 261, 51))
self.plainTextEdit_4.setStyleSheet("background-color: rgb(243, 243, 243);")
self.plainTextEdit_4.setObjectName("plainTextEdit_4")
self.plainTextEdit_5 = MyQPlainTextEdit(Form)
# self.plainTextEdit_5 = QtWidgets.QPlainTextEdit(Form)
self.plainTextEdit_5.setGeometry(QtCore.QRect(580, 60, 211, 71))
self.plainTextEdit_5.setStyleSheet("background-color: rgb(243, 243, 243);")
self.plainTextEdit_5.setObjectName("plainTextEdit_5")
self.plainTextEdit.setStyleSheet('background-color: #f0f0f0; border: none')
self.plainTextEdit_2.setStyleSheet('background-color: #f0f0f0; border: none')
self.plainTextEdit_3.setStyleSheet('background-color: #f0f0f0; border: none')
self.plainTextEdit_4.setStyleSheet('background-color: #f0f0f0; border: none')
self.plainTextEdit_5.setStyleSheet('background-color: #f0f0f0; border: none')
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.pushButton.setText(_translate("Form", "СОХРАНИТЬ"))
self.label_3.setText(_translate("Form", "ФИО:"))
self.label.setText(_translate("Form", "Наименование ЛПУ:"))
self.label_7.setText(_translate("Form", "E-mail:"))
self.label_9.setText(_translate("Form", "Создайте пароль доступа:"))
self.label_5.setText(_translate("Form", "Специализация:"))
self.label_11.setText(_translate("Form", "РЕГИСТРАЦИЯ НОВОГО ОПЕРАТОРА"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())