Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
mainwidget.cpp
Go to the documentation of this file.
1#include "mainwidget.h"
2
3#include <QHBoxLayout>
4#include <QLabel>
5#include <QCheckBox>
6#include <QPushButton>
7#include <QVBoxLayout>
8#include <QApplication>
9
10#include "TQt6Canvas.h"
11#include "TCanvas.h"
12#include "TRandom.h"
13#include "TMath.h"
14#include "TLatex.h"
15#include "TH1I.h"
16#include "TH2I.h"
17#include "TGraph.h"
18
19using namespace ROOT::Experimental;
20
22{
23 auto *mainLayout = new QHBoxLayout(this);
24
25 // left side - buttons
26 auto *leftPanel = new QWidget(this);
27 leftPanel->setFixedWidth(160);
28 auto *leftLayout = new QVBoxLayout(leftPanel);
29 leftLayout->setContentsMargins(0, 0, 0, 0);
30
31 const char *btns[8] = {"TLatex", "TH1", "TH2", "TGraph", "Toggle menu", "Toggle toobar", "Toggle statusbar", "Exit"};
32
33 for (int i = 0; i < 8; ++i) {
34 auto *btn = new QPushButton(tr("%1").arg(btns[i]), leftPanel);
35 btn->setMinimumHeight(25);
36 leftLayout->addWidget(btn);
37 connect(btn, &QPushButton::clicked, this, [this, i] {
39 // emit buttonClicked(i);
40 });
41 if (i == 6) {
42 auto box1 = new QCheckBox("Update", leftPanel);
43 leftLayout->addWidget(box1);
44 connect(box1, &QCheckBox::toggled, this, [this](bool on) { toggle_timer(on); });
45 leftLayout->addStretch(); // keep buttons at the top
46 }
47 }
48
49 // right side - placeholder for canvas
50 m_placeholder = new QWidget(this);
51 m_placeholderLayout = new QVBoxLayout(m_placeholder);
52 m_placeholderLayout->setContentsMargins(0, 0, 0, 0);
53
54 mainLayout->addWidget(leftPanel);
55 mainLayout->addWidget(m_placeholder, 1); // stretch factor 1: takes remaining space
56
57 // embed special ROOT widget and create TCanvas instance with the name "qt6canv"
58 // after this method canvas will be shown inside m_placeholder widget
59 // one can add primitives, call Update() and all other ROOT methods - but not Close()
60 m_canvas = TQt6Canvas::EmbedQt6Canvas(m_placeholder, "qt6canv");
61
63
64 // configure update timer
65 QObject::connect(&m_timer, &QTimer::timeout, [this]() { m_counter++; fill_canvas_object(); });
66 m_timer.setSingleShot(false);
67}
68
70{
71 if (on)
72 m_timer.start(200);
73 else
74 m_timer.stop();
75}
76
78{
79 if (!m_canvas)
80 return;
81
82 TObject *obj = nullptr;
84 while (obj = iter()) {
85 // skip TFrame object
86 if (!obj->InheritsFrom("TFrame"))
87 break;
88 }
89
90 switch (m_clicked_button) {
91 case 0:
92 if (auto ltx = dynamic_cast<TLatex *>(obj))
93 if (m_counter > 0) {
94 ltx->SetTextAlign(12);
95 ltx->SetX(0.2);
96 ltx->SetTitle(TString::Format("TLatex : #color[%d]{%d}", 2 + m_counter % 7, m_counter));
97 ltx->SetY(0.5 + 0.2*TMath::Sin(m_counter * 0.15));
98 }
99 break;
100 case 1:
101 if (auto h1 = dynamic_cast<TH1 *>(obj)) {
102 h1->Reset();
103 auto meanx = TMath::Sin(m_counter * 0.4);
104 for (int n = 0; n < 300; ++n) {
105 auto x = gRandom->Gaus(meanx);
106 h1->Fill(x);
107 }
108 if (m_counter > 0)
109 h1->SetTitle(TString::Format("TH1 example cnt: %d", m_counter));
110 }
111 break;
112 case 2:
113 if (auto h2 = dynamic_cast<TH2 *>(obj)) {
114 h2->Reset();
115 auto meanx = TMath::Sin(m_counter * 0.15);
116 auto meany = -TMath::Sin(m_counter * 0.37);
117 for (int n = 0; n < 300; ++n) {
118 auto x = gRandom->Gaus(meanx);
119 auto y = gRandom->Gaus(meany);
120 h2->Fill(x, y);
121 }
122 if (m_counter > 0)
123 h2->SetTitle(TString::Format("TH2 example cnt: %d", m_counter));
124 }
125 break;
126 case 3:
127 if (auto gr = dynamic_cast<TGraph *>(obj)) {
128 for (int i = 0; i < 100; ++i)
129 gr->SetPoint(i, i + 10, TMath::Sin((i + m_counter) * 0.2));
130 if (m_counter > 0)
131 gr->SetTitle(TString::Format("TGraph example cnt: %d", m_counter));
132 }
133 break;
134 }
135 m_canvas->Update();
136}
137
139{
140 if (!m_canvas)
141 return;
142
143 if (index == 4) {
144 // there is no toggle method in TCanvas class, use TCanvasImp
148 return;
149 } else if (index == 5) {
151 return;
152 } else if (index == 6) {
154 return;
155 }
156
158 m_counter = 0;
159
160 m_canvas->Clear();
161 m_canvas->Resize();
162
163 switch (index) {
164 case 0: {
165 auto ltx = new TLatex(0.5, 0.5, "TLatex");
166 ltx->SetTextAlign(22);
167 ltx->SetTextSize(0.2);
168 ltx->SetBit(kCanDelete);
169 m_canvas->Range(0, 0, 1, 1); // set default ranges
170 m_canvas->Add(ltx);
171 break;
172 }
173 case 1: {
174 auto h1 = new TH1I("h1", "Example of TH1I", 100, -5, 5);
175 h1->SetDirectory(nullptr);
177 m_canvas->Add(h1);
178 break;
179 }
180 case 2: {
181 auto h2 = new TH2I("h2", "Example of TH2I", 40, -5, 5, 40, -5, 5);
182 h2->SetDirectory(nullptr);
183 h2->SetBit(kCanDelete);
184 m_canvas->Add(h2, "colz");
185 break;
186 }
187 case 3: {
188 auto gr = new TGraph(100);
189 gr->SetName("gr1");
190 gr->SetTitle("Example of TGraph");
192 gr->SetLineWidth(2);
194 m_canvas->Add(gr, "al");
195 break;
196 }
197 default: QApplication::quit(); return;
198 }
199
200 fill_canvas_object(); // also canvas->Update() will be called
201}
@ kGreen
Definition Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void on
@ kCanDelete
Definition TObject.h:375
R__EXTERN TRandom * gRandom
Definition TRandom.h:73
QTimer m_timer
Definition mainwidget.h:35
QVBoxLayout * m_placeholderLayout
Definition mainwidget.h:29
TCanvas * m_canvas
Definition mainwidget.h:31
void button_clicked(int index)
int m_counter
Definition mainwidget.h:36
MainWidget(QWidget *parent=nullptr)
int m_clicked_button
Definition mainwidget.h:37
void fill_canvas_object()
QWidget * m_placeholder
Definition mainwidget.h:28
void toggle_timer(bool on)
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:47
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:44
virtual void ShowMenuBar(Bool_t show=kTRUE)
Definition TCanvasImp.h:79
void Clear(Option_t *option="") override
Remove all primitives from the canvas.
Definition TCanvas.cxx:763
virtual void ToggleEventStatus()
Toggle event statusbar.
Definition TCanvas.cxx:2483
TCanvasImp * GetCanvasImp() const override
Definition TCanvas.h:163
virtual void Resize(Option_t *option="")
Recompute canvas parameters following a X11 Resize.
Definition TCanvas.cxx:1701
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2543
@ kMenuBar
Definition TCanvas.h:96
virtual void ToggleToolBar()
Toggle toolbar.
Definition TCanvas.cxx:2494
A TGraph is an object made of two arrays X and Y with npoints each.
Definition TGraph.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition TGraph.cxx:2386
void SetName(const char *name="") override
Set graph name.
Definition TGraph.cxx:2425
void SetTitle(const char *title="") override
Change (i.e.
Definition TGraph.cxx:2441
void Reset(Option_t *option="") override
Reset.
Definition TH1.cxx:10528
1-D histogram with an int per channel (see TH1 documentation)
Definition TH1.h:826
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:126
virtual void SetDirectory(TDirectory *dir)
By default, when a histogram is created, it is added to the list of histogram objects in the current ...
Definition TH1.cxx:9170
void SetTitle(const char *title) override
Change/set the title.
Definition TH1.cxx:6932
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3489
2-D histogram with an int per channel (see TH1 documentation)
Definition TH2.h:245
Service class for 2-D histogram classes.
Definition TH2.h:30
To draw Mathematical Formula.
Definition TLatex.h:20
Mother of all ROOT objects.
Definition TObject.h:42
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition TObject.cxx:886
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:548
TList * GetListOfPrimitives() const override
Definition TPad.h:259
void Range(Double_t x1, Double_t y1, Double_t x2, Double_t y2) override
Set world coordinate system for the pad.
Definition TPad.cxx:5278
void Add(TObject *obj, Option_t *opt="", Bool_t modified=kTRUE) override
Add an object to list of primitives with specified draw option When.
Definition TPad.cxx:485
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:274
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2460
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TGraphErrors * gr
Definition legend1.C:25
TH1F * h1
Definition legend1.C:5
Namespace for ROOT features in testing.
Definition TROOT.h:100
Double_t Sin(Double_t)
Returns the sine of an angle of x radians.
Definition TMath.h:601