Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Function called many times
Forum Updated to NodeBB v4.3 + New Features

Function called many times

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thilith
    wrote on last edited by
    #1

    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 !

    m.sueM 1 Reply Last reply
    0
    • T Thilith

      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 !

      m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      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.

      T 1 Reply Last reply
      8
      • m.sueM m.sue

        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.

        T Offline
        T Offline
        Thilith
        wrote on last edited by
        #3

        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 ?

        mrjjM 1 Reply Last reply
        0
        • T Thilith

          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 ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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.

          T 1 Reply Last reply
          4
          • mrjjM mrjj

            @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.

            T Offline
            T Offline
            Thilith
            wrote on last edited by
            #5

            @mrjj

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

            Regards,
            Thilith.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved