Issue
The issue comes from the multi-module Pyqt structure.
To get you to speed, all of my files with the issue are readily available in github repo here.
I downloaded the code from the Stackoverflow inquiry about the side bar in PYQT when using multiple UI file structure.
@Eyllanesc, kudos to his help with Qt answered the question and provided an example here.
The issue is, when calling Pyqt5 objects (such as lineEdit) via Widget class it throws various errors.
I am trying (for the sake of this example) fill one lineEdit in module customerpage.py
with the text from other lineEdit in module loginpage.py
.
call in loginpage.py (works)
def test(self):
try:
return CustomerWidget.test_func(self.lineEdit_server.text())
except Exception as e:
print(e)
pass
receiving in customerpage.py (error)
def test_func(name):
print(name)
CustomerWidget.parent().lineEdit_cust_name.setText(name)
The errors that I get when referencing CustomerWidget in the module customerpage
are following.
For reference as CustomerWidget
it is error type object 'CustomerWidget' has no attribute 'lineEdit_cust_name
.
For reference as CustomerWidget()
it is error wrapped C/C++ object of type QLineEdit has been deleted
.
For reference as CustomerWidget().parent().
it is error NoneType' object has no attribute 'lineEdit_cust_name'
.
For reference as CustomerWidget.parent().
it is error parent(self): first argument of unbound method must have type 'QObject'
.
How to refer to the widget class in other module with the PYQT Ui structure that it gets recognised?
Structure
├── main.py
├── pages
│ ├── loginpage.py
│ └── customer.py
└── ui
├── main.ui
├── login.ui
└── customer.ui
main.py
import os
from PyQt5 import QtGui, uic, QtCore, QtWidgets
from PyQt5.QtCore import QAbstractTableModel, Qt
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView
from functools import partial
import os.path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))
class MainWidget(Base, Form):
def __init__(self, parent=None):
super(self.__class__, self).__init__(parent)
self.setupUi(self)
buttons = (self.loginbutton,self.customerbutton)
for i, button in enumerate(buttons):
button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
app.setStyle("Breeze")
w = MainWidget()
w.show()
sys.exit(app.exec_())
loginpage.py
import os
from PyQt5 import QtGui, uic
import ctypes
import os.path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
from .customerpage import CustomerWidget
current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "../ui/login.ui"))
class LoginWidget(Base, Form):
def __init__(self, parent=None):
super(self.__class__, self).__init__(parent)
self.setupUi(self)
self.pushButton_test.clicked.connect(self.test)
self.lineEdit_server.setText("localhost")
def test(self):
try:
return CustomerWidget.test_func(self.lineEdit_server.text())
except Exception as e:
print(e)
pass
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = LoginWidget()
w.show()
sys.exit(app.exec_())
customerpage.py
from PyQt5 import QtGui, uic
from PyQt5.QtCore import QSize, Qt, QSortFilterProxyModel
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel, QSqlQuery
from PyQt5.QtWidgets import (
QApplication,
QLabel,
QLineEdit,
QMainWindow,
QWidget,
QMessageBox
)
import os
import os.path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "../ui/customer.ui"))
class CustomerWidget(Base, Form):
def __init__(self, parent=None):
super(self.__class__, self).__init__(parent)
self.setupUi(self)
self.lineEdit_cust_name.setText("")
def test_func(name):
print(name)
CustomerWidget.parent().lineEdit_cust_name.setText(name)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = CustomerWidget()
w.show()
sys.exit(app.exec_())
Solution
Okay, so there is an issue with accessing the objects, as they are not recognized as class variables.
This might not be a proper way how to do it in PYQT but it certainly works for this example. It can be done by declaring them within __init__
of thewidget class.
CustomerWidget.lineEdit_cust_name = self.lineEdit_cust_name
Answered By - Kokokoko
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.