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. why my inline ui function call create segmentation fault when i call it from other class ?
Forum Updated to NodeBB v4.3 + New Features

why my inline ui function call create segmentation fault when i call it from other class ?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 2 Posters 667 Views
  • 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.
  • jsulmJ jsulm

    @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

    delete pGasLable

    Why do you delete it?
    You should not delete widgets like that, do

    pGasLable->deleteLater();
    

    instead.
    Also, if you set parent then DO NOT DELETE IT! It will be deleted as soon as parent is deleted. If you delete it by yourself you get a double delete! Please read https://doc.qt.io/qt-5/objecttrees.html

    Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by Qt embedded developer
    #3

    @jsulm in one parent class this function get called multiple time so i have used delete. so in this case what will you suggest ?

    jsulmJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      @jsulm in one parent class this function get called multiple time so i have used delete. so in this case what will you suggest ?

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

      so i have used delete

      But you don't have to delete all widgets manually. When you delete msgBox it will delete all its children including pGasLable.
      You should really read the link I gave you, parent-child is one of the core concepts in Qt.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Q 2 Replies Last reply
      2
      • jsulmJ jsulm

        @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

        so i have used delete

        But you don't have to delete all widgets manually. When you delete msgBox it will delete all its children including pGasLable.
        You should really read the link I gave you, parent-child is one of the core concepts in Qt.

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by
        #5

        @jsulm Thanks

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

          so i have used delete

          But you don't have to delete all widgets manually. When you delete msgBox it will delete all its children including pGasLable.
          You should really read the link I gave you, parent-child is one of the core concepts in Qt.

          Q Offline
          Q Offline
          Qt embedded developer
          wrote on last edited by Qt embedded developer
          #6

          @jsulm Hi !

          i have seen that this function does not create segmentation fault in other class. it works properly but it happens in my one class only.

          i have printed line number to find out segmentation fault. and i found out last line number before line "delete pGasLable" , after that line number not get printed. so i think it created segmentation fault. so can you refer me right link to find out segmentation fault in case qt programs

          jsulmJ 1 Reply Last reply
          0
          • Q Qt embedded developer

            @jsulm Hi !

            i have seen that this function does not create segmentation fault in other class. it works properly but it happens in my one class only.

            i have printed line number to find out segmentation fault. and i found out last line number before line "delete pGasLable" , after that line number not get printed. so i think it created segmentation fault. so can you refer me right link to find out segmentation fault in case qt programs

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by jsulm
            #7

            @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

            so can you refer me right link to find out segmentation fault in case qt programs

            There is nothing special about Qt here. Simply use the debugger.
            And I already said several times: if a QObject based object has a parent you DO NOT HAVE to delete it explicitly, it will be deleted as soon as its parent is deleted. I also posted this link for you to read (and you should really do!): https://doc.qt.io/qt-5/objecttrees.html
            A heave nothing to add to this topic...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            Q 1 Reply Last reply
            2
            • jsulmJ jsulm

              @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

              so can you refer me right link to find out segmentation fault in case qt programs

              There is nothing special about Qt here. Simply use the debugger.
              And I already said several times: if a QObject based object has a parent you DO NOT HAVE to delete it explicitly, it will be deleted as soon as its parent is deleted. I also posted this link for you to read (and you should really do!): https://doc.qt.io/qt-5/objecttrees.html
              A heave nothing to add to this topic...

              Q Offline
              Q Offline
              Qt embedded developer
              wrote on last edited by
              #8

              @jsulm i have read it yesterday. i removed delete statement. and segmentation fault gone.

              but now in that function it not wait for user action to click on ok button. so what is solution of this problem ?

              jsulmJ 1 Reply Last reply
              0
              • Q Qt embedded developer

                @jsulm i have read it yesterday. i removed delete statement. and segmentation fault gone.

                but now in that function it not wait for user action to click on ok button. so what is solution of this problem ?

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

                so what is solution of this problem ?

                Please post the current code, this issue has nothing to do with object deletion.
                It should wait here:

                msgBox->exec();
                

                as exec() is a blocking call.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                Q 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

                  so what is solution of this problem ?

                  Please post the current code, this issue has nothing to do with object deletion.
                  It should wait here:

                  msgBox->exec();
                  

                  as exec() is a blocking call.

                  Q Offline
                  Q Offline
                  Qt embedded developer
                  wrote on last edited by
                  #10

                  @jsulm

                  #include "FuelCheckProfileUpdate.h"
                  #include "ui_FuelCheckProfileUpdate.h"
                  #include "CommonClass/AppMain.h"
                  #include "FuelCheck.h"
                  #include "MainScreen.h"
                  inline void MSG_BOX11(QWidget* parent,QString sMessage, QString sButtonText)
                  {
                      QFont font("Open Sans", 14);
                      QFont qButtonFont("Open Sans", 16, QFont::Bold);
                  
                      const QSize btn_size1 = QSize(300,80);
                  
                      QFrame *FrameMessageBox = new QFrame(parent);
                      FrameMessageBox->setGeometry(0,0,480,800);
                      FrameMessageBox->show();
                      FrameMessageBox->setStyleSheet("background-color: rgba(0, 0, 0, 0.5);border-radius:10px;");
                  
                      QMessageBox * msgBox = new QMessageBox(QMessageBox::NoIcon, "", "", QMessageBox::Ok,FrameMessageBox, Qt::FramelessWindowHint);
                  
                      QLabel *pGasLable = new QLabel(msgBox);
                      QString sStringhtml =  "<html>"
                                             "<head>"
                                             "</style>"
                                             "</head>"
                                             "<body>"
                                             "<p><p align=\'center\'><span style= \""+QString("font-family:'Open Sans';font-weight:400;font-size:14pt;")+"\">"+(sMessage)+"</span><p align=\"center\">"
                                             "</body></html>";
                  
                      pGasLable->setTextFormat(Qt::RichText);
                      pGasLable->setText(sStringhtml);
                      pGasLable->setTextInteractionFlags(Qt::NoTextInteraction);
                      pGasLable->setWordWrap(true);
                      pGasLable->setAlignment(Qt::AlignCenter);
                      pGasLable->setStyleSheet("Padding-top: 10px;background-color: rgb(255, 255, 255);");
                      pGasLable->setGeometry(MSGLBL_STARTX, MSGLBL_STARTY, MSGLBL_WIDTH, MSGLBL_HEIGHT);
                  
                      msgBox->setStyleSheet("QMessageBox { background-color: rgb(255, 255, 255);border-radius:10px;}"
                                           "QLabel{min-height:80px; max-height:80px; min-width:280px; max-width:300px;border-radius:10px;}"); // msgBox->setText("<center>Machine Configuration Saved</center>");
                  
                      msgBox->setFont(font);
                      msgBox->button(QMessageBox::Ok)->setText(sButtonText);
                      msgBox->button(QMessageBox::Ok)->setStyleSheet("border: none;color: rgb(0, 0, 0);background-color: #FFBF00; background-repeat: no-repeat;position: center;border-radius:10px;");
                      msgBox->button(QMessageBox::Ok)->setMinimumSize(btn_size1);
                      msgBox->button(QMessageBox::Ok)->setMaximumSize(btn_size1);
                      msgBox->button(QMessageBox::Ok)->setFont(qButtonFont);
                  
                  #if 1       //To add border-radius
                      int radius = 10;
                      int width = 324;
                      int height = 192;
                  
                      QRegion verticalRegion(0, radius, width, height - (2 * radius));
                      QRegion horizontalRegion(radius, 0, width - (2 * radius), height);
                      QRegion circle(0, 0, 2 * radius, 2 * radius, QRegion::Ellipse);
                  
                      QRegion region = verticalRegion.united(horizontalRegion);
                      region = region.united(circle);
                      region = region.united(circle.translated(width - (2 * radius), 0));
                      region = region.united(circle.translated(width - (2 * radius), height - (2 * radius)));
                      region = region.united(circle.translated(0, height - (2 * radius)));
                  
                      msgBox->setMask(region);
                  #endif
                  
                      msgBox->exec();
                  
                      if(IS_VALID_OBJ(pGasLable))
                      {
                          pGasLable->deleteLater();
                      }
                  
                      if (IS_VALID_OBJ(pGasLable))
                      {
                          msgBox->deleteLater();
                      }
                  
                      if (IS_VALID_OBJ(pGasLable))
                      {
                          FrameMessageBox->deleteLater();
                      }
                  
                  }
                  
                  #define X_SECONDS_TO_SHOW 5
                  
                  FuelCheckProfileUpdate::FuelCheckProfileUpdate(QWidget *parent) :
                      QWidget(parent),
                      ui(new Ui::FuelCheckProfileUpdate)
                  {
                      ui->setupUi(this);
                      ui->lblProgress->setVisible(false);
                      bIsRequiredToUpdateFirmware = false;
                  
                      timer=new QTimer(this);
                      QFont font;
                      font.setFamily(FONT_FAMILY_AWESOME);
                      font.setPixelSize(28);
                      font.setPixelSize(44);
                  
                      u16OldFirmware = 0;
                      u16FwToUpdate = 0;
                      u16NewFirmware = 0;
                      u8BlinkCounter=X_SECONDS_TO_SHOW;
                  
                      FwUpdateProcess();
                  
                      QTimer::singleShot(1000,this, SLOT(UpdateFirmware()));
                  
                      QObject::connect(timer, SIGNAL(timeout()), this, SLOT(FwUpdateFail()));
                      timer->start(500);
                  
                  
                    //  QObject::connect(&qFwFailTimer, SIGNAL(timeout()), this, SLOT(FwUpdateFail()));
                  }
                  
                  FuelCheckProfileUpdate::~FuelCheckProfileUpdate()
                  {
                      if(timer->isActive())
                         timer->stop();
                  
                      QObject::disconnect(timer, SIGNAL(timeout()), this, SLOT(FwUpdateFail()));
                  
                      pMainApp.ObjSettings.eScreenStatus = SCREEN_MAIN;
                  
                      if( pMainApp.ObjSettings.bIsCritiCalFailed == true)
                      {
                          pMainApp.ObjHwControl.ObjLed.OnOff(RedLed);
                      }
                      else
                      {
                          pMainApp.ObjHwControl.ObjLed.OnOff(GreenLed);
                      }
                  
                      MainScreen* parent = qobject_cast<MainScreen*>(this->parent());
                      parent->LaserModuleFirmwareUpgrade();
                      delete ui;
                  }
                  
                  void FuelCheckProfileUpdate::FwUpdateFail()
                  {
                      u8BlinkCounter=u8BlinkCounter-1;
                  
                      ui->lblProgress->show();
                  
                  
                      QString sMessage = "<html><head/><body><p align=\"center\"><span style=\" font-size:16pt; font-weight:400;\">"+sUpdatingFuelGaugeProfile()+" </span></p>"
                                         "<p align=\"center\"><span style=\" font-size:16pt; font-weight:400;\">Profile From Version </span>"
                                         "<span style=\" font-size:16pt;\">"+ sCurrentFuelGaugeFirmwareValue +"</span><span style=\" font-size:16pt; font-weight:400;\"> to </span>"
                                         "<span style=\" font-size:16pt;\">"+ sNewFuelGaugeFirmwareValue +"</span></p></body></html>";
                  
                      if(u8BlinkCounter%2 == 0)
                      {
                      ui->lblProgress->setText(sMessage);
                      }
                      else
                      {
                          QString sMessage = "<html><head/><body><p align=\"center\"><span style=\" font-size:16pt; font-weight:400;\">"+sUpdatingFuelGaugeProfile()+" </span></p>"
                                             "<p align=\"center\"><span style=\" font-size:16pt; font-weight:400;\">Profile From Version </span>"
                                             "<span style=\" font-size:16pt;\">"+ sCurrentFuelGaugeFirmwareValue +"</span><span style=\" font-size:16pt; font-weight:400;\"> to </span>"
                                             "<span style=\" font-size:16pt;\">"+ sNewFuelGaugeFirmwareValue +"</span></p></body></html>";
                  
                         ui->lblProgress->setText(sMessage);
                      }
                  
                      if(u8BlinkCounter==0)
                      {  timer->stop();
                         this->deleteLater();
                      }
                  }
                  
                  void FuelCheckProfileUpdate::FwUpdateProcess()
                  {
                      bIsRequiredToUpdateFirmware = false;
                  if(GetFwVersionToUpdate())
                      {
                          //2. Get Current version
                          if(GetFirmwareVersion(u16OldFirmware) == true)
                          {
                             sOldFuelGaugeFirmwareValue = QString::number(u16OldFirmware);
                  
                              if(u16FwToUpdate != u16OldFirmware)
                              {
                                  bIsRequiredToUpdateFirmware = true;
                  
                              }
                          }
                      }
                  }
                  
                  bool FuelCheckProfileUpdate::GetFirmwareVersion(u_int16_t &u16GotFwVersion)
                  {
                    bool bStatus = false;
                      unsigned char pData[36];
                      char text1[4];
                      u16OldFirmware = 0;
                      if(pMainApp.ObjSettings.ObjFuelCheck.gauge_read_firmware(pMainApp.ObjSettings.ObjFuelCheck.nI2CId,0x0100,pData,36) == 0)
                      {
                          text1[0] = pData[2];
                          text1[1] = pData[3];
                          text1[2] = pData[4];
                          text1[3] = '\0';
                          sCurrentFuelGaugeFirmwareValue = text1;
                          u16GotFwVersion = sCurrentFuelGaugeFirmwareValue.toInt();
                  
                  #ifdef QDEBUG_ENABLE
                          pMainApp.ObjSettings.DebugStation("Existing Fuel Gauge profile version :"+sCurrentFuelGaugeFirmwareValue);
                  #endif
                          bStatus = true;
                      }
                  
                      return bStatus;
                  }
                  
                  bool FuelCheckProfileUpdate::GetFwVersionToUpdate()
                  {
                      bool bStatus = false;
                      u16FwToUpdate = 0;
                      QString sFwUpdateFilePath = SOURCE_FILE;
                  
                      do
                      {
                          if(sFwUpdateFilePath.isEmpty())
                          {
                  #ifdef QDEBUG_ENABLE
                              pMainApp.ObjSettings.DebugStation(sInvalidFilePath());
                              MSG_BOX1(this, sInvalidFilePath(), sOk());
                  #endif
                              break;
                          }
                          else
                          {
                              FILE *fp;
                              int errnum;
                  
                              char ra[50],text[4];
                  
                                  fp = fopen(SOURCE_FILE,"r");
                  
                                      if( fp == NULL)
                                      {
                                          errnum = errno;
                  
                                          fprintf(stderr, "Value of errno: %d\n", errno);
                  
                                          perror("Error printed by perror");
                  
                                          fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
                  
                                      }
                                      else
                                      {
                  
                                         fseek(fp, -543, SEEK_END);
                                          //fflush(ra);
                                         fgets ( ra, 50, fp );
                                          if( fclose(fp) == 0)
                                          {
                                              if((ra[0]=='3')&&(ra[3]=='3')&&(ra[6]=='3'))
                                              {
                                                  if(((ra[1]>='0') &&  (ra[1]<='9')) && ((ra[4]>='0') &&  (ra[4]<='9')) &&((ra[7]>='0') &&  (ra[7]<='9')))
                                                  {
                                                      text[0] = ra[1];
                                                      text[1] = ra[4];
                                                      text[2] = ra[7];
                                                      text[3] = '\0';
                                                      sNewFuelGaugeFirmwareValue = text;
                                                      u16FwToUpdate = sNewFuelGaugeFirmwareValue.toInt();
                  #ifdef QDEBUG_ENABLE
                          pMainApp.ObjSettings.DebugStation("Source file Fuel Gauge profile version :"+sNewFuelGaugeFirmwareValue);
                  #endif
                  
                                                  }
                                                  else
                                                  {
                  #ifdef QDEBUG_ENABLE
                          pMainApp.ObjSettings.DebugStation("golden image not recognized");
                  #endif
                                                  }
                  
                  
                                              }
                                              else
                                              {
                  #ifdef QDEBUG_ENABLE
                          pMainApp.ObjSettings.DebugStation("golden image not recognized");
                  #endif
                                              }
                                          }
                  
                                      }
                  
                  
                          }
                  
                          bStatus = true;
                      }while(false);
                      return bStatus;
                  }
                  
                  bool FuelCheckProfileUpdate::UpdateFirmware()
                  {
                      bool bStatus = false;
                       pMainApp.ObjSettings.DebugStation("sCurrentFuelGaugeFirmwareValue" + sCurrentFuelGaugeFirmwareValue);
                       pMainApp.ObjSettings.DebugStation("sNewFuelGaugeFirmwareValue" + sNewFuelGaugeFirmwareValue);
                  
                      if ( sCurrentFuelGaugeFirmwareValue != sNewFuelGaugeFirmwareValue )
                      {
                          do
                          {
                              pMainApp.ObjSettings.eScreenStatus = SCREEN_FWUPDATE;
                  #ifdef QDEBUG_ENABLE
                          pMainApp.ObjSettings.DebugStation("Golden image update process started");
                  #endif
                              int nSourceFile;
                              struct stat st;
                              char *pFileBuffer;
                  
                              stat(SOURCE_FILE,&st);
                  
                              if((nSourceFile= open(SOURCE_FILE,O_RDONLY))<  0)
                              {
                  #ifdef QDEBUG_ENABLE
                          pMainApp.ObjSettings.DebugStation("Can not open Golden image Source file");
                  #endif
                                  return bStatus;
                              }
                  
                              pFileBuffer =(char *)malloc(st.st_size);
                  
                              if (!pFileBuffer)
                                  return bStatus;
                  
                              read(nSourceFile, pFileBuffer, st.st_size);
                              ::close(nSourceFile);
                              QString str =pMainApp.ObjSettings.ObjFuelCheck.gauge_execute_fs(pMainApp.ObjSettings.ObjFuelCheck.nI2CId, pFileBuffer);
                  
                              if(GetFwVersionToUpdate())
                              {
                                  pMainApp.ObjSettings.ObjFuelCheck.bIsFuelCheckOn=pMainApp.ObjSettings.ObjFuelCheck.gauge_init();
                  
                                  if ( pMainApp.ObjSettings.ObjFuelCheck.bIsFuelCheckOn)
                                  {
                  
                                      int Percentage = -1;
                                      bool bisFuelConnected = true;
                                      for(UINT8 u8loop =0; u8loop < 3 ; u8loop++)
                                      {
                                          if(pMainApp.ObjSettings.ObjFuelCheck.gauge_cmd_read(pMainApp.ObjSettings.ObjFuelCheck.nI2CId, 0x2c) > 0) //SOC in [%]
                                          {
                                              Percentage = pMainApp.ObjSettings.ObjFuelCheck.u16Datareceived;
                                          }
                                          else
                                          {
                                              pMainApp.ObjSettings.bIsFuelCheckPassed=false;
                                              qDebug()<<"Fuel gauge not found";
                  
                  
                  #ifdef QDEBUG_ENABLE
                                              pMainApp.ObjSettings.DebugStation("Fuel gauge not found");
                  #endif
                                              return bStatus;
                                          }
                                          QThread::msleep(100);
                                          if((Percentage < 0) && (Percentage > 100))
                                          {
                                              bisFuelConnected = false;
                                              qDebug()<<"Fuel gauge found but wrong percentarge detected : " + QString::number(Percentage);
                  
                  #ifdef QDEBUG_ENABLE
                                              pMainApp.ObjSettings.DebugStation("Fuel gauge found but wrong percentarge detected : " + QString::number(Percentage));
                  #endif
                                          }
                                      }
                                      if(bisFuelConnected == true)
                                      {
                                          qDebug()<<"Battery percentage" + QString::number(Percentage);
                  #ifdef QDEBUG_ENABLE
                                          pMainApp.ObjSettings.DebugStation("Battery percentage" + QString::number(Percentage));
                  #endif
                                          pMainApp.ObjSettings.bIsFuelCheckPassed=true;
                                          pMainApp.ObjSettings.ObjFuelCheck.nResult  = Percentage;
                                      }
                                  }
                                  else
                                  {
                                      pMainApp.ObjSettings.bIsFuelCheckPassed=false;
                                  }
                                  //2. Get Current version
                                  if(GetFirmwareVersion(u16OldFirmware) == true)
                                  {
                                      if(u16FwToUpdate == u16OldFirmware)
                                      {
                                          pMainApp.ObjSettings.bIsFuelGaugeFirmWareUpdateRequire = false;
                  
                  #ifdef QDEBUG_ENABLE
                                          pMainApp.ObjSettings.DebugStation("Profile updated successfully");
                  #endif
                                          pMainApp.ObjHwControl.ObjSounder.SetAlarmLevel(AlarmLevel1);
                                          QString  sMessage = "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:400;\">"+sFuelGaugeProfile() +"</span>"
                                                                                                                                                                                  "<span style=\" font-size:12pt;\"><br/></span>"
                                                                                                                                                                                  "<span style=\" font-size:12pt; font-weight:400;\">From </span><span style=\" font-size:12pt;\">"+sOldFuelGaugeFirmwareValue +"</span>"
                                                                                                                                                                                                                                                                                                                    "<span style=\" font-size:12pt; font-weight:400;\"> To </span><span style=\" font-size:12pt;\">"+sNewFuelGaugeFirmwareValue +" "+sSuccessfully() +".</span></p>"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                      "<p align=\"center\"><span style=\" font-size:12pt; font-weight:600;\">" "</span></p></body></html>";
                  
                                          MSG_BOX1(this, sMessage, sOk());
                                          //  pMainApp.ObjSettings.bIsLaserModuleFirmwareUpdate = true;
                                          //  pMainApp.ObjSettings.UpdateConfig(SETTING_ISLASERMODULEFIRMWAREUPDATE, QString::number(pMainApp.ObjSettings.bIsLaserModuleFirmwareUpdate));
                                          pMainApp.ObjHwControl.ObjSounder.SetAlarmLevel(SounderOFF);
                  
                                      }
                                      else
                                      {
                                          pMainApp.ObjHwControl.ObjSounder.SetAlarmLevel(AlarmLevel1);
                                          MSG_BOX1(this, sFuelGaugeProfileUpdateFail(), sOk());
                                          pMainApp.ObjHwControl.ObjSounder.SetAlarmLevel(SounderOFF);
                                          this->deleteLater();
                                      }
                                  }
                              }
                  
                              if(str == '\0' || str == "")
                              {
                  
                                  qDebug()<<"Profile updated successfully" <<str;
                              }
                              else
                              {
                                  qDebug()<<"Profile updation failed"<<str;
                              }
                  
                              free(pFileBuffer);
                              bStatus = true;
                  
                          }     while(false);
                          return bStatus;
                      }
                      return false;
                  }
                  
                  
                  jsulmJ 1 Reply Last reply
                  0
                  • Q Qt embedded developer

                    @jsulm

                    #include "FuelCheckProfileUpdate.h"
                    #include "ui_FuelCheckProfileUpdate.h"
                    #include "CommonClass/AppMain.h"
                    #include "FuelCheck.h"
                    #include "MainScreen.h"
                    inline void MSG_BOX11(QWidget* parent,QString sMessage, QString sButtonText)
                    {
                        QFont font("Open Sans", 14);
                        QFont qButtonFont("Open Sans", 16, QFont::Bold);
                    
                        const QSize btn_size1 = QSize(300,80);
                    
                        QFrame *FrameMessageBox = new QFrame(parent);
                        FrameMessageBox->setGeometry(0,0,480,800);
                        FrameMessageBox->show();
                        FrameMessageBox->setStyleSheet("background-color: rgba(0, 0, 0, 0.5);border-radius:10px;");
                    
                        QMessageBox * msgBox = new QMessageBox(QMessageBox::NoIcon, "", "", QMessageBox::Ok,FrameMessageBox, Qt::FramelessWindowHint);
                    
                        QLabel *pGasLable = new QLabel(msgBox);
                        QString sStringhtml =  "<html>"
                                               "<head>"
                                               "</style>"
                                               "</head>"
                                               "<body>"
                                               "<p><p align=\'center\'><span style= \""+QString("font-family:'Open Sans';font-weight:400;font-size:14pt;")+"\">"+(sMessage)+"</span><p align=\"center\">"
                                               "</body></html>";
                    
                        pGasLable->setTextFormat(Qt::RichText);
                        pGasLable->setText(sStringhtml);
                        pGasLable->setTextInteractionFlags(Qt::NoTextInteraction);
                        pGasLable->setWordWrap(true);
                        pGasLable->setAlignment(Qt::AlignCenter);
                        pGasLable->setStyleSheet("Padding-top: 10px;background-color: rgb(255, 255, 255);");
                        pGasLable->setGeometry(MSGLBL_STARTX, MSGLBL_STARTY, MSGLBL_WIDTH, MSGLBL_HEIGHT);
                    
                        msgBox->setStyleSheet("QMessageBox { background-color: rgb(255, 255, 255);border-radius:10px;}"
                                             "QLabel{min-height:80px; max-height:80px; min-width:280px; max-width:300px;border-radius:10px;}"); // msgBox->setText("<center>Machine Configuration Saved</center>");
                    
                        msgBox->setFont(font);
                        msgBox->button(QMessageBox::Ok)->setText(sButtonText);
                        msgBox->button(QMessageBox::Ok)->setStyleSheet("border: none;color: rgb(0, 0, 0);background-color: #FFBF00; background-repeat: no-repeat;position: center;border-radius:10px;");
                        msgBox->button(QMessageBox::Ok)->setMinimumSize(btn_size1);
                        msgBox->button(QMessageBox::Ok)->setMaximumSize(btn_size1);
                        msgBox->button(QMessageBox::Ok)->setFont(qButtonFont);
                    
                    #if 1       //To add border-radius
                        int radius = 10;
                        int width = 324;
                        int height = 192;
                    
                        QRegion verticalRegion(0, radius, width, height - (2 * radius));
                        QRegion horizontalRegion(radius, 0, width - (2 * radius), height);
                        QRegion circle(0, 0, 2 * radius, 2 * radius, QRegion::Ellipse);
                    
                        QRegion region = verticalRegion.united(horizontalRegion);
                        region = region.united(circle);
                        region = region.united(circle.translated(width - (2 * radius), 0));
                        region = region.united(circle.translated(width - (2 * radius), height - (2 * radius)));
                        region = region.united(circle.translated(0, height - (2 * radius)));
                    
                        msgBox->setMask(region);
                    #endif
                    
                        msgBox->exec();
                    
                        if(IS_VALID_OBJ(pGasLable))
                        {
                            pGasLable->deleteLater();
                        }
                    
                        if (IS_VALID_OBJ(pGasLable))
                        {
                            msgBox->deleteLater();
                        }
                    
                        if (IS_VALID_OBJ(pGasLable))
                        {
                            FrameMessageBox->deleteLater();
                        }
                    
                    }
                    
                    #define X_SECONDS_TO_SHOW 5
                    
                    FuelCheckProfileUpdate::FuelCheckProfileUpdate(QWidget *parent) :
                        QWidget(parent),
                        ui(new Ui::FuelCheckProfileUpdate)
                    {
                        ui->setupUi(this);
                        ui->lblProgress->setVisible(false);
                        bIsRequiredToUpdateFirmware = false;
                    
                        timer=new QTimer(this);
                        QFont font;
                        font.setFamily(FONT_FAMILY_AWESOME);
                        font.setPixelSize(28);
                        font.setPixelSize(44);
                    
                        u16OldFirmware = 0;
                        u16FwToUpdate = 0;
                        u16NewFirmware = 0;
                        u8BlinkCounter=X_SECONDS_TO_SHOW;
                    
                        FwUpdateProcess();
                    
                        QTimer::singleShot(1000,this, SLOT(UpdateFirmware()));
                    
                        QObject::connect(timer, SIGNAL(timeout()), this, SLOT(FwUpdateFail()));
                        timer->start(500);
                    
                    
                      //  QObject::connect(&qFwFailTimer, SIGNAL(timeout()), this, SLOT(FwUpdateFail()));
                    }
                    
                    FuelCheckProfileUpdate::~FuelCheckProfileUpdate()
                    {
                        if(timer->isActive())
                           timer->stop();
                    
                        QObject::disconnect(timer, SIGNAL(timeout()), this, SLOT(FwUpdateFail()));
                    
                        pMainApp.ObjSettings.eScreenStatus = SCREEN_MAIN;
                    
                        if( pMainApp.ObjSettings.bIsCritiCalFailed == true)
                        {
                            pMainApp.ObjHwControl.ObjLed.OnOff(RedLed);
                        }
                        else
                        {
                            pMainApp.ObjHwControl.ObjLed.OnOff(GreenLed);
                        }
                    
                        MainScreen* parent = qobject_cast<MainScreen*>(this->parent());
                        parent->LaserModuleFirmwareUpgrade();
                        delete ui;
                    }
                    
                    void FuelCheckProfileUpdate::FwUpdateFail()
                    {
                        u8BlinkCounter=u8BlinkCounter-1;
                    
                        ui->lblProgress->show();
                    
                    
                        QString sMessage = "<html><head/><body><p align=\"center\"><span style=\" font-size:16pt; font-weight:400;\">"+sUpdatingFuelGaugeProfile()+" </span></p>"
                                           "<p align=\"center\"><span style=\" font-size:16pt; font-weight:400;\">Profile From Version </span>"
                                           "<span style=\" font-size:16pt;\">"+ sCurrentFuelGaugeFirmwareValue +"</span><span style=\" font-size:16pt; font-weight:400;\"> to </span>"
                                           "<span style=\" font-size:16pt;\">"+ sNewFuelGaugeFirmwareValue +"</span></p></body></html>";
                    
                        if(u8BlinkCounter%2 == 0)
                        {
                        ui->lblProgress->setText(sMessage);
                        }
                        else
                        {
                            QString sMessage = "<html><head/><body><p align=\"center\"><span style=\" font-size:16pt; font-weight:400;\">"+sUpdatingFuelGaugeProfile()+" </span></p>"
                                               "<p align=\"center\"><span style=\" font-size:16pt; font-weight:400;\">Profile From Version </span>"
                                               "<span style=\" font-size:16pt;\">"+ sCurrentFuelGaugeFirmwareValue +"</span><span style=\" font-size:16pt; font-weight:400;\"> to </span>"
                                               "<span style=\" font-size:16pt;\">"+ sNewFuelGaugeFirmwareValue +"</span></p></body></html>";
                    
                           ui->lblProgress->setText(sMessage);
                        }
                    
                        if(u8BlinkCounter==0)
                        {  timer->stop();
                           this->deleteLater();
                        }
                    }
                    
                    void FuelCheckProfileUpdate::FwUpdateProcess()
                    {
                        bIsRequiredToUpdateFirmware = false;
                    if(GetFwVersionToUpdate())
                        {
                            //2. Get Current version
                            if(GetFirmwareVersion(u16OldFirmware) == true)
                            {
                               sOldFuelGaugeFirmwareValue = QString::number(u16OldFirmware);
                    
                                if(u16FwToUpdate != u16OldFirmware)
                                {
                                    bIsRequiredToUpdateFirmware = true;
                    
                                }
                            }
                        }
                    }
                    
                    bool FuelCheckProfileUpdate::GetFirmwareVersion(u_int16_t &u16GotFwVersion)
                    {
                      bool bStatus = false;
                        unsigned char pData[36];
                        char text1[4];
                        u16OldFirmware = 0;
                        if(pMainApp.ObjSettings.ObjFuelCheck.gauge_read_firmware(pMainApp.ObjSettings.ObjFuelCheck.nI2CId,0x0100,pData,36) == 0)
                        {
                            text1[0] = pData[2];
                            text1[1] = pData[3];
                            text1[2] = pData[4];
                            text1[3] = '\0';
                            sCurrentFuelGaugeFirmwareValue = text1;
                            u16GotFwVersion = sCurrentFuelGaugeFirmwareValue.toInt();
                    
                    #ifdef QDEBUG_ENABLE
                            pMainApp.ObjSettings.DebugStation("Existing Fuel Gauge profile version :"+sCurrentFuelGaugeFirmwareValue);
                    #endif
                            bStatus = true;
                        }
                    
                        return bStatus;
                    }
                    
                    bool FuelCheckProfileUpdate::GetFwVersionToUpdate()
                    {
                        bool bStatus = false;
                        u16FwToUpdate = 0;
                        QString sFwUpdateFilePath = SOURCE_FILE;
                    
                        do
                        {
                            if(sFwUpdateFilePath.isEmpty())
                            {
                    #ifdef QDEBUG_ENABLE
                                pMainApp.ObjSettings.DebugStation(sInvalidFilePath());
                                MSG_BOX1(this, sInvalidFilePath(), sOk());
                    #endif
                                break;
                            }
                            else
                            {
                                FILE *fp;
                                int errnum;
                    
                                char ra[50],text[4];
                    
                                    fp = fopen(SOURCE_FILE,"r");
                    
                                        if( fp == NULL)
                                        {
                                            errnum = errno;
                    
                                            fprintf(stderr, "Value of errno: %d\n", errno);
                    
                                            perror("Error printed by perror");
                    
                                            fprintf(stderr, "Error opening file: %s\n", strerror( errnum ));
                    
                                        }
                                        else
                                        {
                    
                                           fseek(fp, -543, SEEK_END);
                                            //fflush(ra);
                                           fgets ( ra, 50, fp );
                                            if( fclose(fp) == 0)
                                            {
                                                if((ra[0]=='3')&&(ra[3]=='3')&&(ra[6]=='3'))
                                                {
                                                    if(((ra[1]>='0') &&  (ra[1]<='9')) && ((ra[4]>='0') &&  (ra[4]<='9')) &&((ra[7]>='0') &&  (ra[7]<='9')))
                                                    {
                                                        text[0] = ra[1];
                                                        text[1] = ra[4];
                                                        text[2] = ra[7];
                                                        text[3] = '\0';
                                                        sNewFuelGaugeFirmwareValue = text;
                                                        u16FwToUpdate = sNewFuelGaugeFirmwareValue.toInt();
                    #ifdef QDEBUG_ENABLE
                            pMainApp.ObjSettings.DebugStation("Source file Fuel Gauge profile version :"+sNewFuelGaugeFirmwareValue);
                    #endif
                    
                                                    }
                                                    else
                                                    {
                    #ifdef QDEBUG_ENABLE
                            pMainApp.ObjSettings.DebugStation("golden image not recognized");
                    #endif
                                                    }
                    
                    
                                                }
                                                else
                                                {
                    #ifdef QDEBUG_ENABLE
                            pMainApp.ObjSettings.DebugStation("golden image not recognized");
                    #endif
                                                }
                                            }
                    
                                        }
                    
                    
                            }
                    
                            bStatus = true;
                        }while(false);
                        return bStatus;
                    }
                    
                    bool FuelCheckProfileUpdate::UpdateFirmware()
                    {
                        bool bStatus = false;
                         pMainApp.ObjSettings.DebugStation("sCurrentFuelGaugeFirmwareValue" + sCurrentFuelGaugeFirmwareValue);
                         pMainApp.ObjSettings.DebugStation("sNewFuelGaugeFirmwareValue" + sNewFuelGaugeFirmwareValue);
                    
                        if ( sCurrentFuelGaugeFirmwareValue != sNewFuelGaugeFirmwareValue )
                        {
                            do
                            {
                                pMainApp.ObjSettings.eScreenStatus = SCREEN_FWUPDATE;
                    #ifdef QDEBUG_ENABLE
                            pMainApp.ObjSettings.DebugStation("Golden image update process started");
                    #endif
                                int nSourceFile;
                                struct stat st;
                                char *pFileBuffer;
                    
                                stat(SOURCE_FILE,&st);
                    
                                if((nSourceFile= open(SOURCE_FILE,O_RDONLY))<  0)
                                {
                    #ifdef QDEBUG_ENABLE
                            pMainApp.ObjSettings.DebugStation("Can not open Golden image Source file");
                    #endif
                                    return bStatus;
                                }
                    
                                pFileBuffer =(char *)malloc(st.st_size);
                    
                                if (!pFileBuffer)
                                    return bStatus;
                    
                                read(nSourceFile, pFileBuffer, st.st_size);
                                ::close(nSourceFile);
                                QString str =pMainApp.ObjSettings.ObjFuelCheck.gauge_execute_fs(pMainApp.ObjSettings.ObjFuelCheck.nI2CId, pFileBuffer);
                    
                                if(GetFwVersionToUpdate())
                                {
                                    pMainApp.ObjSettings.ObjFuelCheck.bIsFuelCheckOn=pMainApp.ObjSettings.ObjFuelCheck.gauge_init();
                    
                                    if ( pMainApp.ObjSettings.ObjFuelCheck.bIsFuelCheckOn)
                                    {
                    
                                        int Percentage = -1;
                                        bool bisFuelConnected = true;
                                        for(UINT8 u8loop =0; u8loop < 3 ; u8loop++)
                                        {
                                            if(pMainApp.ObjSettings.ObjFuelCheck.gauge_cmd_read(pMainApp.ObjSettings.ObjFuelCheck.nI2CId, 0x2c) > 0) //SOC in [%]
                                            {
                                                Percentage = pMainApp.ObjSettings.ObjFuelCheck.u16Datareceived;
                                            }
                                            else
                                            {
                                                pMainApp.ObjSettings.bIsFuelCheckPassed=false;
                                                qDebug()<<"Fuel gauge not found";
                    
                    
                    #ifdef QDEBUG_ENABLE
                                                pMainApp.ObjSettings.DebugStation("Fuel gauge not found");
                    #endif
                                                return bStatus;
                                            }
                                            QThread::msleep(100);
                                            if((Percentage < 0) && (Percentage > 100))
                                            {
                                                bisFuelConnected = false;
                                                qDebug()<<"Fuel gauge found but wrong percentarge detected : " + QString::number(Percentage);
                    
                    #ifdef QDEBUG_ENABLE
                                                pMainApp.ObjSettings.DebugStation("Fuel gauge found but wrong percentarge detected : " + QString::number(Percentage));
                    #endif
                                            }
                                        }
                                        if(bisFuelConnected == true)
                                        {
                                            qDebug()<<"Battery percentage" + QString::number(Percentage);
                    #ifdef QDEBUG_ENABLE
                                            pMainApp.ObjSettings.DebugStation("Battery percentage" + QString::number(Percentage));
                    #endif
                                            pMainApp.ObjSettings.bIsFuelCheckPassed=true;
                                            pMainApp.ObjSettings.ObjFuelCheck.nResult  = Percentage;
                                        }
                                    }
                                    else
                                    {
                                        pMainApp.ObjSettings.bIsFuelCheckPassed=false;
                                    }
                                    //2. Get Current version
                                    if(GetFirmwareVersion(u16OldFirmware) == true)
                                    {
                                        if(u16FwToUpdate == u16OldFirmware)
                                        {
                                            pMainApp.ObjSettings.bIsFuelGaugeFirmWareUpdateRequire = false;
                    
                    #ifdef QDEBUG_ENABLE
                                            pMainApp.ObjSettings.DebugStation("Profile updated successfully");
                    #endif
                                            pMainApp.ObjHwControl.ObjSounder.SetAlarmLevel(AlarmLevel1);
                                            QString  sMessage = "<html><head/><body><p align=\"center\"><span style=\" font-size:12pt; font-weight:400;\">"+sFuelGaugeProfile() +"</span>"
                                                                                                                                                                                    "<span style=\" font-size:12pt;\"><br/></span>"
                                                                                                                                                                                    "<span style=\" font-size:12pt; font-weight:400;\">From </span><span style=\" font-size:12pt;\">"+sOldFuelGaugeFirmwareValue +"</span>"
                                                                                                                                                                                                                                                                                                                      "<span style=\" font-size:12pt; font-weight:400;\"> To </span><span style=\" font-size:12pt;\">"+sNewFuelGaugeFirmwareValue +" "+sSuccessfully() +".</span></p>"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                        "<p align=\"center\"><span style=\" font-size:12pt; font-weight:600;\">" "</span></p></body></html>";
                    
                                            MSG_BOX1(this, sMessage, sOk());
                                            //  pMainApp.ObjSettings.bIsLaserModuleFirmwareUpdate = true;
                                            //  pMainApp.ObjSettings.UpdateConfig(SETTING_ISLASERMODULEFIRMWAREUPDATE, QString::number(pMainApp.ObjSettings.bIsLaserModuleFirmwareUpdate));
                                            pMainApp.ObjHwControl.ObjSounder.SetAlarmLevel(SounderOFF);
                    
                                        }
                                        else
                                        {
                                            pMainApp.ObjHwControl.ObjSounder.SetAlarmLevel(AlarmLevel1);
                                            MSG_BOX1(this, sFuelGaugeProfileUpdateFail(), sOk());
                                            pMainApp.ObjHwControl.ObjSounder.SetAlarmLevel(SounderOFF);
                                            this->deleteLater();
                                        }
                                    }
                                }
                    
                                if(str == '\0' || str == "")
                                {
                    
                                    qDebug()<<"Profile updated successfully" <<str;
                                }
                                else
                                {
                                    qDebug()<<"Profile updation failed"<<str;
                                }
                    
                                free(pFileBuffer);
                                bStatus = true;
                    
                            }     while(false);
                            return bStatus;
                        }
                        return false;
                    }
                    
                    
                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

                    msgBox->exec();

                    It should wait here for user to close dialog. So, what exactly is not working?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    Q 1 Reply Last reply
                    2
                    • jsulmJ jsulm

                      @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

                      msgBox->exec();

                      It should wait here for user to close dialog. So, what exactly is not working?

                      Q Offline
                      Q Offline
                      Qt embedded developer
                      wrote on last edited by Qt embedded developer
                      #12

                      @jsulm

                      In my above mentioned class's slot FwUpdateFail() used
                      below statement so parent delete messagebox function before click on okay.

                      if(u8BlinkCounter==0)
                      { timer->stop();
                      this->deleteLater(); // THIS LINE CAUSE PROBLEM
                      }

                      So i have commented it and i have added "this->deleteLater()" statement in UpdateFirmware slot before return statment.

                      Thank you jsulm for solve problem correctly.

                      jsulmJ 1 Reply Last reply
                      0
                      • Q Qt embedded developer

                        @jsulm

                        In my above mentioned class's slot FwUpdateFail() used
                        below statement so parent delete messagebox function before click on okay.

                        if(u8BlinkCounter==0)
                        { timer->stop();
                        this->deleteLater(); // THIS LINE CAUSE PROBLEM
                        }

                        So i have commented it and i have added "this->deleteLater()" statement in UpdateFirmware slot before return statment.

                        Thank you jsulm for solve problem correctly.

                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #13

                        @Qt-embedded-developer said in why my inline ui function call create segmentation fault when i call it from other class ?:

                        i think solution is i need to delete 'this' in destructor

                        What? Destructor is called just before "this" is deleted! You should read about memory management in C++.

                        I don't really understand what you are doing, sorry. No idea why you have a timer and why you want to delete "this"? It just makes no sense...

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        3

                        • Login

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