title: Maybe the range of a QTLineEdit inside a QThread that crashes the program?
body: <p>Thanks for reading this.</p>
  
  <p>I'm making a program to view running processes on <em>Linux</em> in a <em>QTableWidget</em>.</p>
  
  <p>I am using a <em>QThread</em> to update the <em>QTableWidget</em>, and I've added filtering with a <em>QLineEdit</em>, up to there everything works.</p>
  
  <p>Now I've added access to a SLOT called <strong>killProcessFromLineEdit()</strong> from a button click, but what appends is that I can't access my other <em>QLineEdit</em> called <strong>processIdToKill</strong>, it crashes the program without any compilation notice.</p>
  
  <p>I wonder if it is the range of my UI objects that make the program crash when the signal contacts the slot, as the program crashes if I try to reach any Widget from the slot.</p>
  
  <p>I use QtCreator as environment and Linux with C++</p>
  
  <p>The code can be directly browsed or downloaded from :</p>
  
  <p><a href="https://sourceforge.net/p/nicesystemmonitor/code/ci/master/tree/" rel="nofollow">https://sourceforge.net/p/nicesystemmonitor/code/ci/master/tree/</a></p>
  
  <p>I've been stuck for a while and it makes me crazy :-)</p>
  
  <p>Patrick</p>
  
  <p>PS : Here's an Image of my debbuger as the problem occurs and the code of my Class where it appends.</p>
  
  <p><a href="http://i.stack.imgur.com/m8ioM.png" rel="nofollow">http://i.stack.imgur.com/m8ioM.png</a></p>
  
  <pre><code>#include &lt;unistd.h&gt;
  #include &lt;pwd.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;ios&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;dirent.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;cctype&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;vector&gt;
  #include &lt;sstream&gt;
  #include "renderprocesstablethread.h"
  #include &lt;proc/readproc.h&gt;
  #include &lt;proc/procps.h&gt;
  #include "mainwindow.h"
  #include &lt;sys/types.h&gt;
  #include &lt;signal.h&gt;
  using namespace std;
  
  
  RenderProcessTableThread::RenderProcessTableThread(QObject *parent)
      : QThread(parent)
  {
  
      restart = false;
      abort = false;
  }
  
  RenderProcessTableThread::~RenderProcessTableThread()
  {
      mutex.lock();
      abort = true;
      condition.wakeOne();
      mutex.unlock();
  
      wait();
  }
  
  
  void RenderProcessTableThread::quitRenderProcessTableThread()
  {
      cout &lt;&lt; "exit thread" &lt;&lt; endl;
      quit();
  }
  
  void RenderProcessTableThread::killProcessFromLineEdit()
  {
  
      cout &lt;&lt; "getProcessIdToKill()-&gt;text().length() : " &lt;&lt; getProcessIdToKill()-&gt;text().length() &lt;&lt; endl;
      if (getProcessIdToKill()-&gt;text().length() != 0) {
          string s="kill -9 " + getProcessIdToKill()-&gt;text().toStdString();
          system(s.c_str());
      }
     statusBar-&gt;showMessage(QString((char*)("killed process : " + getProcessIdToKill()-&gt;text().toStdString()).c_str()));
  
  }
  
  bool RenderProcessTableThread::isNum(char *s) {
      int i = 0,  flag;
  
      while(s[i]){
              //if there is a letter in a string then string is not a number
          if(isalpha(s[i]) || s[i] == '.'){
              flag = 0;
              break;
          }
          else flag = 1;
          i++;
          }
      if (flag == 1) return true;
      else return false;
  }
  
  string RenderProcessTableThread::convertDouble(double value) {
    std::ostringstream o;
    if (!(o &lt;&lt; value))
      return "";
    return o.str();
  }
  
  string RenderProcessTableThread::convertInt(int value) {
    std::ostringstream o;
    if (!(o &lt;&lt; value))
      return "";
    return o.str();
  }
  
  
  
  void RenderProcessTableThread::run()
  {
      linesCount = countProcesses();
      int myTableSize=0;
      ui_tableWidgetProcessus-&gt;clearContents();
      while (!abort) {
          mutex.lock();
          mutex.unlock();
          vector&lt;QStringList&gt; myTableList;
          myTableList = fillProcessTable(true);
          myTableSize = myTableList.size();
          // cout &lt;&lt; myTableSize &lt;&lt; endl;
  
          // cout &lt;&lt; "processIdToKill-&gt;text().length() : " &lt;&lt; processIdToKill-&gt;text().length() &lt;&lt; endl;
  
          /* Checks what lines must be select according to filter */
          for (int row=0; row&lt;myTableSize; row++)
              if (!(myTableList[row].at(0).contains(filterProcessTable-&gt;text(), Qt::CaseInsensitive) ||
                      myTableList[row].at(10).contains(filterProcessTable-&gt;text(), Qt::CaseInsensitive) ||
                      filterProcessTable-&gt;text().length() == 0)) {
                  myTableList.erase(myTableList.begin() + row);
  
                  myTableSize--;
                  row = -1;
  
              }
  
          ui_tableWidgetProcessus-&gt;setSortingEnabled(false);
          ui_tableWidgetProcessus-&gt;setRowCount(myTableSize);
  
          /* Fills the Table by either creating items or updating if exists */
          for (int row=0; row&lt;myTableSize; row++)
  
                  for (int column=0; column&lt;11; column++) {
                      if (ui_tableWidgetProcessus-&gt;item(row, column) == 0) {
  
                          /* creates an item */
                          ui_tableWidgetProcessus-&gt;setItem(row,column,new QTableWidgetItem(myTableList[row].at(column),0));
  
  
                      } else {
  
                          /* function to get username from */
                          struct passwd *passwd;
                          passwd = getpwuid(getuid());
                          char *userName = passwd-&gt;pw_name;
  
                          /* set colors according to username */
                          if (myTableList[row].at(0).compare("root") == 0 &amp;&amp; userName != "root")
                              ui_tableWidgetProcessus-&gt;item(row,0)-&gt;setBackgroundColor(Qt::red);
                          else if (myTableList[row].at(0).compare(userName) == 0)
                              ui_tableWidgetProcessus-&gt;item(row,0)-&gt;setBackgroundColor(Qt::green);
                          else
                              ui_tableWidgetProcessus-&gt;item(row,0)-&gt;setBackgroundColor(Qt::yellow);
  
                          /* updates an item */
                          ui_tableWidgetProcessus-&gt;item(row,column)-&gt;setText(myTableList[row].at(column));
  
                      }
                  }
  
          emit renderedTable();
          sleep(1);
  
          //cout &lt;&lt; "ça tourne" &lt;&lt; endl;
      }
      mutex.lock();
      if (!restart)
          condition.wait(&amp;mutex);
      restart = false;
      mutex.unlock();
  
  }
  QLineEdit *RenderProcessTableThread::getProcessIdToKill() const
  {
      return processIdToKill;
  }
  
  void RenderProcessTableThread::setProcessIdToKill(QLineEdit *value)
  {
      processIdToKill = value;
  }
  
  
  /* Prepares the process table */
  void RenderProcessTableThread::setLocalMainWindow(MainWindow&amp; w)
  {
      localMainWindow = &amp;w;
      ui_tableWidgetProcessus = localMainWindow-&gt;findChild&lt;QTableWidget*&gt;("tableWidgetProcessus");
      ui_tableWidgetProcessus-&gt;setColumnCount(11);
      ui_tableWidgetProcessus-&gt;setColumnWidth(10,508);
      ui_ProcessusTabLayout = localMainWindow-&gt;findChild&lt;QVBoxLayout*&gt;("verticalLayout_3");
      setProcessIdToKill(localMainWindow-&gt;findChild&lt;QLineEdit*&gt;("lineEdit_process"));
      filterProcessTable = localMainWindow-&gt;findChild&lt;QLineEdit*&gt;("lineEdit_filter");
      statusBar = localMainWindow-&gt;findChild&lt;QStatusBar*&gt;("statusBar");
      QFont fnt;
      fnt.setPointSize(10);
      fnt.setFamily("Arial");
      ui_tableWidgetProcessus-&gt;setFont(fnt);
      QStringList labels;
      labels &lt;&lt; "user" &lt;&lt; "pid" &lt;&lt; "cpu" &lt;&lt; "nice" &lt;&lt; "vsz" &lt;&lt; "rss" &lt;&lt; "tty" &lt;&lt; "stat" &lt;&lt; "start" &lt;&lt; "time" &lt;&lt; "cmd";
      ui_tableWidgetProcessus-&gt;setHorizontalHeaderLabels(labels);
  }
  
  
  int RenderProcessTableThread::countProcesses() {
      PROCTAB* proc = openproc(PROC_FILLUSR | PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLSTATUS | PROC_FILLARG);
      proc_t proc_info;
  
      memset(&amp;proc_info, 0, sizeof(proc_info));
  
      int i = 0;
      while (readproc(proc, &amp;proc_info) != NULL) {
          i++;
      }
      return i;
  }
  
  
  /* Prepares a dynamic table of lists of the different elements to be put in the process table */
  vector&lt;QStringList&gt; RenderProcessTableThread::fillProcessTable(bool update) {
  
      vector&lt;QStringList&gt; myTableList;
  
      QMutexLocker locker(&amp;mutex);
  
      if (!isRunning()) {
          start(LowPriority);
      } else {
          restart = true;
          condition.wakeOne();
      }
  
      PROCTAB* proc = openproc(PROC_FILLUSR | PROC_FILLMEM | PROC_FILLSTAT | PROC_FILLSTATUS | PROC_FILLARG);
      proc_t proc_info;
  
      memset(&amp;proc_info, 0, sizeof(proc_info));
  
      while (readproc(proc, &amp;proc_info) != NULL) {
  
          QStringList myLine;
          myLine &lt;&lt; QString(proc_info.fuser) &lt;&lt; QString((char*)convertInt(proc_info.tid).c_str()) &lt;&lt; QString((char*)convertInt(proc_info.pcpu).c_str())
                 &lt;&lt; QString((char*)convertInt(proc_info.nice).c_str()) &lt;&lt; QString((char*)convertInt(proc_info.vm_size).c_str()) &lt;&lt; QString((char*)convertInt(proc_info.rss).c_str())
                 &lt;&lt; QString((char*)convertInt(proc_info.tty).c_str()) &lt;&lt; QString(proc_info.state) &lt;&lt; QString((char*)convertInt(proc_info.start_time).c_str())
                 &lt;&lt; QString((char*)convertInt(proc_info.stime).c_str());
  
          string text;
          if (proc_info.cmdline != 0) {
            vector&lt;string&gt; v(proc_info.cmdline, proc_info.cmdline + sizeof(proc_info.cmdline) / sizeof(string));
            text = v[0];
          }
          else {
            vector&lt;string&gt; v;
            v.push_back(proc_info.cmd);
            text = v[0];
          }
  
          myLine &lt;&lt; QString((char*)text.c_str());
          myTableList.push_back(myLine);
  
      }
      closeproc(proc);
  
      return myTableList;
  }
  </code></pre>