问题
我想开始动态地将 TCanvas 从程序的“模型”部分动态传递到“视图”部分。我考虑这样做的方式是在启动时在 View 上创建我的 TCanvas,然后在填充图表后使用 View TCanvas 更新此 TCanvas。我创建了一个测试台,看看它是否可以工作。
我展示了一种工作方法和损坏的方法。
我正在使用 QT-ROOT,TQtWidget是一个自定义小部件,它本质上是对 TCanvas 的一种支持。
设置我的画布
void DataTestTab::setupCanvas(int cNCbc) //I pass "2" to this for now to generate the below loop twice
{
for (int i=0; i<cNCbc; i++)
{
m_vectorCanvas.push_back(new TQtWidget(this));
//m_vectorCanvas[i]->GetCanvas()->SetFillColor(i);
QHBoxLayout *loH = new QHBoxLayout(this);
loH->addWidget(m_vectorCanvas[i]);
m_vectorLayout.push_back(loH);
QGroupBox *gbCanvas = new QGroupBox(this);
QString title = QString("CBC %1").arg(i);
gbCanvas->setTitle(title);
gbCanvas->setLayout(m_vectorLayout[i]);
m_vectorGroupBox.push_back(gbCanvas);
ui->loCbcBox->addWidget(m_vectorGroupBox[i]); //adding the panels to the main layout
}
}
这个作品
void DataTestTab::drawTest()
{
static Int_t HistoID = 1;
qDebug() << "in Testing env ";
std::vector<TH1D*> graphs;
std::vector<TCanvas*> vCanvas;
TString name("h1_");
Bool_t build = false;
for (int i = 0; i <m_vectorCanvas.size() ; i++)
{
TCanvas *cCanvas = new TCanvas(build);
name += HistoID++;
vCanvas.push_back(cCanvas);
vCanvas.at(i)->cd();
TH1D *h1 = new TH1D(name.Data(),name.Data(),10,0, 10);
graphs.push_back(h1);
graphs.at(i)->Fill(i);
graphs.at(i)->Draw();
//graphs.at(i)->DrawCopy();
m_vectorCanvas.at(i)->GetCanvas()->SetFillColor(i+5);
m_vectorCanvas.at(i)->cd();
qDebug() << i;
m_vectorCanvas.at(i)->GetCanvas()->SetCanvas(vCanvas.at(i));
m_vectorCanvas.at(i)->Refresh();
}
}
对应输出:
尽管图表的顺序错误。
这不起作用
我将此方法转移到另一个类并通过信号/插槽将 TCanvas 传回。
void DataTestWorker::doWork()
{
static Int_t HistoID = 1;
qDebug() << "in Testing env ";
std::vector<TH1D*> graphs;
std::vector<TCanvas*> vCanvas;
TString name("h1_");
Bool_t build = false;
for (int i = 0; i <2 ; i++)
{
TCanvas *cCanvas = new TCanvas(build);
name += HistoID++;
vCanvas.push_back(cCanvas);
vCanvas.at(i)->cd();
TH1D *h1 = new TH1D(name.Data(),name.Data(),10,0, 10);
graphs.push_back(h1);
graphs.at(i)->Fill(i);
graphs.at(i)->Draw();
}
emit sendGraphData(vCanvas); //void sendGraphData(const std::vector<TCanvas*> &canvas);
然后将图形数据发送到这里:
void DataTestTab::drawGraph(const std::vector<TCanvas*> &canvas)
{
for (int i=0; i<m_vectorCanvas.size(); i++)
{
canvas.at(i)->cd();
m_vectorCanvas.at(i)->cd();
m_vectorCanvas.at(i)->GetCanvas()->SetCanvas(canvas.at(i));
m_vectorCanvas.at(i)->Refresh();
//m_vectorCanvas.at(i)->GetCanvas()->Update();
}
}
这是此方法的输出:
目前我能看到的唯一错误是我在 SetupTab 中得到了这个:
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