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.

    mainwindows.cpp

    mainWindows::mainWindows():
        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 = stringList.at(0);
        double revenusSize = temp.toInt();
    
        if(state == "getOK")
        {
            for(int i = 1; i < revenusSize * 2; i = i + 2)
            {
                m_mainSimuWindows->addEntreesSorties("Revenu", stringList.at(i), stringList.at(i + 1));
            }
        }
    

    connection.cpp

    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.device()->seek(0);
        out << quint16(packet.size() - sizeof(quint16));
    
        socket->write(packet);
    }
    
    void Connexion::dataReceived()
    {
        socket = qobject_cast<QTcpSocket *>(sender());
        QDataStream socketStream(socket);
    
        forever
        {
            if(tailleMessage == 0)
            {
                if(socket->bytesAvailable() < sizeof(quint16))
                    break;
    
                socketStream >> tailleMessage;
            }
    
            if(socket->bytesAvailable() < tailleMessage)
                break;
    
            quint8 ID;
            socketStream >> ID;
    
            if(ID == 'G')
            {
                stringList = QStringList();
                socketStream >> stringList;
    
                if(stringList.last() == "getOK")
                emit getES("getOK");
            }
    

    simuWindows.cpp

    simuWindows::simuWindows(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::simuWindows)
    {
        ui->setupUi(this);
    
        ui->date->setDate(QDate::currentDate());
    
        connect(ui->date, SIGNAL(dateChanged(QDate)), this, SLOT(slot_simuWindows_dateChanged(QDate)));
    }
    
    void simuWindows::addEntreesSorties(QString operation, QString nom, QString montant)
    {
        if(operation == "Revenu")
        {
            ui->tab_revenus->insertRow(ui->tab_revenus->rowCount());
    
            QTableWidgetItem *itemNom = new QTableWidgetItem;
            itemNom->setText(nom);
            ui->tab_revenus->setItem(ui->tab_revenus->rowCount() - 1, 0, itemNom);
    
            QTableWidgetItem *itemMontant = new QTableWidgetItem;
            itemMontant->setText(montant);
            ui->tab_revenus->setItem(ui->tab_revenus->rowCount() - 1, 1, itemMontant);
        }
    }
    
    void simuWindows::slot_simuWindows_dateChanged(QDate date)
    {
        ui->tab_revenus->setRowCount(0);
        ui->tab_depenses->setRowCount(0);
    
        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.

    -Michael.



  • 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

    @Thilith

    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 !

    Regards,
    Thilith.


Log in to reply
 

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