[PyQt] acces to object in different class (Kyle Altendorf)

Kyle Altendorf sda at fstab.net
Fri Nov 18 16:40:33 GMT 2016



On 2016-11-18 11:28, lucaberto at libero.it wrote:

>>> Hello i'm using eric as ide and i have two class
>>> 
>>> class Form(QWidget, Ui_Form):
>>> """
>>> Class documentation goes here.
>>> """
>>> def __init__(self, parent=None):
>>> """
>>> Constructor
>>> 
>>> @param parent reference to the parent widget
>>> @type QWidget
>>> """
>>> super(Form,  self).__init__(parent)
>>> self.setupUi(self)
>>> self.tabWidget.setCurrentIndex(0)
>>> combo = QComboBox()
>>> self.disegno = Cornice(Form)
>>> 
>>> def on_pushButton_7_clicked(self):
>>> """
>>> Slot documentation goes here.
>>> """
>>> # TODO: not implemented yet
>>> #raise NotImplementedError
>>> self.lista_dati_prova = [........]
>>> lista_dati = self.dati_prova
>>> self.lineEdit.setText('test')
>>> self.disegno.cornice(lista_dati)
>>> 
>>> class Cornice():
>>> 
>>> def __init__(self, parent=None):
>>> """
>>> Constructor
v>>
>>> @param parent reference to the parent widget
>>> @type QWidget
>>> """
>>> lista_dati = []
>>> 
>>> def cornice(self, lista_dati):
>>> import math
>>> freqfinale = lista_dati[0]
>>> spostamento = lista_dati[2]
>>> nome_cal = self.lineEdit.text()
>>> 
>>> How i can access to self.lineEdit without passing parameter from the
>>> class Form and without using signal, but asking directly from Cornice
>>> class?
>> 
>> I'm not sure why you have those restrictions, but you probably mean to
>> do:
>> 
>> self.disegno = Cornice(self)
>> 
>> To set the Cornice's parent to that instance of the Form rather than 
>> the
>> Form class.  But, Cornice doesn't inherit from QObject or otherwise 
>> save
>> the parent parameter.  If it did either of those then you could do one
>> of the following (depending on which option you choose):
>> 
>> self.parent.lineEdit.text()
>> self.parent().lineEdit.text()
> 
> Thanks of your reply, but in both the case i obtain
> 
> Cornice object has not attribute parent
> 
> Luca
> 

Yes, that is because Cornice() does not have a parent member variable.  
Do you mean for Cornice to be a Qt object?  It seems like you might 
since you accept the parent parameter in the constructor.  To do this 
you should:

   Class Cornice(QObject):
       def __init__(self, parent=None):
           super(Cornice,  self).__init__(parent)
           list_dati = []

Then your Cornice() class will inherit from QObject and will allow you 
to use `.parent()`.  Alternatively, if you do not want Cornice() to be a 
QObject:

   Class Cornice():
       def __init__(self, parent=None):
           self.parent = parent
           list_dati = []

And then you could use `.parent`.

Out of curiousity, why are you against doing:

   self.disegno.cornice(lista_dati, self.lineEdit.text())

Cheers,
-kyle


More information about the PyQt mailing list