Function called many times

  • Hello guys,

    I'm writing a code in QtCreator with the UI code for the graphical part.

    In my code, when I change a date, I'm refreshing the QTableWidget to correspond with the date.
    I've found that each time I change a date, the function is called as many times as I've already changed the date + 1.
    In other words, if I've changed the date 2 times and I want to change it another time (3rd time), the function will be called 3 times. If I'm changing the date for the 15th time, the function will be called 15 times. And I don't know why.

    Below the code I'm using with all files linked to the function.


        m_mainSimuWindows(new simuWindows)
        m_connect(new Connection)
        connect(m_mainSimuWindows, SIGNAL(signal_dateChanged(QDate)), this, SLOT(slot_dateChanged(QDate)));
    void mainWindows::slot_dateChanged(QDate date)
        QString dateChanged = date.toString("yyyyMM");
        dateChanged = dateChanged + "00";
        m_connect->request(dateChanged, 'G');
        connect(m_connect, SIGNAL(getES(QString)), this, SLOT(slot_getES(QString)));
    void mainWindows::slot_getES(QString state)
        QStringList stringList;
        stringList = m_connect->getGet();
        QString temp =;
        double revenusSize = temp.toInt();
        if(state == "getOK")
            for(int i = 1; i < revenusSize * 2; i = i + 2)
                m_mainSimuWindows->addEntreesSorties("Revenu",, + 1));


    void Connetion::request(QString date, quint8 ID)
        QByteArray packet;
        QDataStream out(&packet, QIODevice::WriteOnly);
        out << quint16(0);
        out << quint8(ID);
        out << m_username->text();
        out << date;
        out << quint16(packet.size() - sizeof(quint16));
    void Connexion::dataReceived()
        socket = qobject_cast<QTcpSocket *>(sender());
        QDataStream socketStream(socket);
            if(tailleMessage == 0)
                if(socket->bytesAvailable() < sizeof(quint16))
                socketStream >> tailleMessage;
            if(socket->bytesAvailable() < tailleMessage)
            quint8 ID;
            socketStream >> ID;
            if(ID == 'G')
                stringList = QStringList();
                socketStream >> stringList;
                if(stringList.last() == "getOK")
                emit getES("getOK");


    simuWindows::simuWindows(QWidget *parent) :
        ui(new Ui::simuWindows)
        connect(ui->date, SIGNAL(dateChanged(QDate)), this, SLOT(slot_simuWindows_dateChanged(QDate)));
    void simuWindows::addEntreesSorties(QString operation, QString nom, QString montant)
        if(operation == "Revenu")
            QTableWidgetItem *itemNom = new QTableWidgetItem;
            ui->tab_revenus->setItem(ui->tab_revenus->rowCount() - 1, 0, itemNom);
            QTableWidgetItem *itemMontant = new QTableWidgetItem;
            ui->tab_revenus->setItem(ui->tab_revenus->rowCount() - 1, 1, itemMontant);
    void simuWindows::slot_simuWindows_dateChanged(QDate date)
        emit signal_dateChanged(date);

    The function which has the problem is void mainWindows::slot_getES(QString state).

    Is there a problem with my code ?
    By the way, I'm not a programmer, I'm just learning code because I like it, so my code may be improved, and I know that, so, please, don't be rude :).
    Howerver, I'm here to improve myself too !

  • Hi @Thilith

    With this line connect(m_connect, SIGNAL(getES(QString)), this, SLOT(slot_getES(QString))); you connect every time the date changes. These connect's add up, you get one connect more for every change. Just move it to the constructor should do the trick.


  • Hello @m.sue ,

    Thank you very much ! You are completely right.
    This is now working fine.

    I'm just curious, why the fact moving it to the constructor did the trick ?
    I mean, what's the difference putting this line into the constructor or into a function ?

  • Qt Champions 2016


    If you put the connect in a function.
    Each time the function runs, you make a new connection.
    Since its allowed to have 1 or more connections, you
    actually add a new each time. so when the signal is emitted
    it will be send X times. one for each connection you made.

    While you can disconnect in the function and connect again to avoid this,
    it makes more sense just to do connect one time.

  • @mrjj

    Oh Ok, I now better understand my mistake :).
    Thank you for your help guys !


Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.