Classes connected but don´t respond properly

i´m trying to connect two methods which are in two different classes. In the class called "Ventana", which is my main window, i have this method:

def addChannel(self):
    global channelCount

    self.scrollLayout = QFormLayout()

    self.canal = QwtPlot()

    if channelCount <= 4:                                
        channelCount += 1
        print str(channelCount)

In another class, i have these methods:

class QwtPlotList(QDialog):
def __init__(self):
    uic.loadUi("PropiedadesCanal.ui", self)

def addChannel_2(self):
    global channelCount
    if channelCount <= 4:
        self.listWidget.addItem("Canal : " + str(channelCount))

What i´m trying to do is that, when i push the button "botonAdd", the "addChannel_2" method calls the "addChannel" method, which is in the Ventana class. Then, a QWidget ("self.canal") is created.

What happens with this code, it is that when i push the "botonAdd", it puts an item in the listWidget but it doesn´t create the QWidget. And if i create the "QWidget" with a button in a toolbar, it doesn´t add any item in the QListWidget

You can see all this in these images:

The "botonAdd" creates an item in the QListWidget but not a QWidget

Another button creates the Qwidget, but not the item in the QListWidget

Hope you can help me. Thanks for your time and answers


There is several issues

1. Calling a function in the class Ventana from another class. What you do:


Ventana() creates a new instance of the class Ventana. The button botonAdd is not connected to your main window, it is connected to another main window you just created. This new window is not shown, and is destroyed add the end of addChannel_2 by the garbage collector (because you don't keep a reference to it).

To call the right addChannel, you need a reference to your actual main window. In Qt, the main window is usually the parent of the other widget. You need to change the definition of QwtPlotList so that it can have a parent:

    class QwtPlotList(QDialog):
      def __init__(self,parent):

    #in main window Ventana

Then you can call any method of Ventana in QwtPlotList:

    #in QwtPlotList

2. Connecting button click to multitple functions. What you do:


    def function1(self):

Every time the button is click, function1 will be called, and will connect the button click to another function. Those connections are additive. The first time you click the button, function1 is called and the button is connect to function2. The second time, function1 and function2 are called, and the button is connect to function2. The third time, function1, function2 and again function2 are called, etc.

You need to simply call the function2, without connecting the button:

def function1:

3. Using globals: you should avoid it, there's often a better solution. I understand you have two functions that need to use the same variable channelCount. To avoid global, I would keep a reference to channelCount in QwtPlotList, and pass it as a parameter in addChannel

def addChannel(channelCount):
  return channelCount


  def addChannel_2(self):

I let you figure out how to put everything together :)

Need Your Help

float change from python 3.0.1 to 3.1.2


I'm trying to learn python. I am using 3.1.2 and the o'reilly book is using 3.0.1

Grant select access on certain columns only in MySQL for phpmyadmin

mysql sql phpmyadmin grant

I want to create a new user in my MySQL database that is able to select and update only certain columns of my table via phpmyadmin. I know this question has been asked several times here but I have a