title: Why Will ROOT Only Let Me Set The Final Element of a TCanvas Vector to Another TCanvas Vector if I Pass the Vector to Another Class?
body: <h2>The Problem</h2>
  
  <p>I want to start dynamically passing a TCanvas from essentially the "Model" part of my program to the "View" part. The way I thought about doing this, was to simply create my TCanvas on the View at startup, then update this TCanvas with the View TCanvas once the graphs had been filled. I created a test bench to see if it would work.</p>
  
  <p>I've displayed a working method and the broken method.</p>
  
  <p>I'm using QT-ROOT, the <a href="http://root.cern.ch/root/html528/TQtWidget.html" rel="nofollow noreferrer">TQtWidget</a> is a custom widget which is essentially a back to the TCanvas.</p>
  
  <h2>Setting Up My Canvas</h2>
  
  <pre><code>void DataTestTab::setupCanvas(int cNCbc) //I pass "2" to this for now to generate the below loop twice
  {
      for (int i=0; i&lt;cNCbc; i++)
      {
          m_vectorCanvas.push_back(new TQtWidget(this));
  
          //m_vectorCanvas[i]-&gt;GetCanvas()-&gt;SetFillColor(i);
          QHBoxLayout *loH = new QHBoxLayout(this);
  
          loH-&gt;addWidget(m_vectorCanvas[i]);
          m_vectorLayout.push_back(loH);
  
          QGroupBox *gbCanvas = new QGroupBox(this);
          QString title = QString("CBC %1").arg(i);
          gbCanvas-&gt;setTitle(title);
          gbCanvas-&gt;setLayout(m_vectorLayout[i]);
          m_vectorGroupBox.push_back(gbCanvas);
  
          ui-&gt;loCbcBox-&gt;addWidget(m_vectorGroupBox[i]); //adding the panels to the main layout
      }
  }
  </code></pre>
  
  <h2>This Works</h2>
  
  <pre><code>void DataTestTab::drawTest()
      {
          static Int_t HistoID = 1;
          qDebug() &lt;&lt; "in Testing env ";
          std::vector&lt;TH1D*&gt; graphs;
          std::vector&lt;TCanvas*&gt; vCanvas;
  
          TString name("h1_");
          Bool_t build = false;
  
  
          for (int i = 0; i &lt;m_vectorCanvas.size() ; i++)
          {
              TCanvas *cCanvas = new TCanvas(build);
              name += HistoID++;
  
              vCanvas.push_back(cCanvas);
              vCanvas.at(i)-&gt;cd();
  
  
              TH1D *h1 = new TH1D(name.Data(),name.Data(),10,0, 10);
              graphs.push_back(h1);
              graphs.at(i)-&gt;Fill(i);
  
              graphs.at(i)-&gt;Draw();
              //graphs.at(i)-&gt;DrawCopy();
  
              m_vectorCanvas.at(i)-&gt;GetCanvas()-&gt;SetFillColor(i+5);
              m_vectorCanvas.at(i)-&gt;cd();
  
              qDebug() &lt;&lt; i;
  
              m_vectorCanvas.at(i)-&gt;GetCanvas()-&gt;SetCanvas(vCanvas.at(i));
              m_vectorCanvas.at(i)-&gt;Refresh();
          }
      }
  </code></pre>
  
  <p>Corresponding output:</p>
  
  <p><img src="https://i.stack.imgur.com/l3cFp.png" alt="Working Method"></p>
  
  <p>Albeit the graphs are in the wrong order.</p>
  
  <h2>This Does Not Work</h2>
  
  <p>I transfer this method to another class and pass the TCanvas back over signal/slots.</p>
  
  <pre><code> void DataTestWorker::doWork()
      {
          static Int_t HistoID = 1;
          qDebug() &lt;&lt; "in Testing env ";
          std::vector&lt;TH1D*&gt; graphs;
          std::vector&lt;TCanvas*&gt; vCanvas;
  
          TString name("h1_");
          Bool_t build = false;
  
          for (int i = 0; i &lt;2 ; i++)
          {
              TCanvas *cCanvas = new TCanvas(build);
              name += HistoID++;
  
              vCanvas.push_back(cCanvas);
              vCanvas.at(i)-&gt;cd();
  
              TH1D *h1 = new TH1D(name.Data(),name.Data(),10,0, 10);
              graphs.push_back(h1);
              graphs.at(i)-&gt;Fill(i);
  
              graphs.at(i)-&gt;Draw();
  
          }
          emit sendGraphData(vCanvas); //void sendGraphData(const std::vector&lt;TCanvas*&gt; &amp;canvas);
  </code></pre>
  
  <p>The graph data is then sent to here:</p>
  
  <pre><code>void DataTestTab::drawGraph(const std::vector&lt;TCanvas*&gt; &amp;canvas)
  {
      for (int i=0; i&lt;m_vectorCanvas.size(); i++)
      {
          canvas.at(i)-&gt;cd();
          m_vectorCanvas.at(i)-&gt;cd();
          m_vectorCanvas.at(i)-&gt;GetCanvas()-&gt;SetCanvas(canvas.at(i));
          m_vectorCanvas.at(i)-&gt;Refresh();
          //m_vectorCanvas.at(i)-&gt;GetCanvas()-&gt;Update();
  
      }
  }
  </code></pre>
  
  <p>This is the output of this method:</p>
  
  <p><img src="https://i.stack.imgur.com/RovCV.png" alt="Attempt 2"></p>
  
  <p>The only error I can see at the moment is I get this for the SetupTab:</p>
  
  <pre><code>QLayout: Attempting to add QLayout "" to GUI::DataTestTab "DataTestTab", which already has a layout
  QLayout: Attempting to add QLayout "" to GUI::DataTestTab "DataTestTab", which already has a layout
  </code></pre>