make left and right axes be selected synchronously, and handle axis and tick labels as one selectable object: Ui->customPlot->xAxis->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels) Ui->customPlot->xAxis2->setSelectedParts(QCPAxis::spAxis|QCPAxis::spTickLabels) Ui->customPlot->xAxis2->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->xAxis2->selectedParts().testFlag(QCPAxis::spTickLabels)) If (ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spAxis) || ui->customPlot->xAxis->selectedParts().testFlag(QCPAxis::spTickLabels) || make top and bottom axes be selected synchronously, and handle axis and tick labels as one selectable object: So the user can select a graph by either clicking on the graph itself The selection state of the left and right axes shall be synchronized as well as the state of theįurther, we want to synchronize the selection of the graphs with the selection state of the respective However, the axis label shall be selectable individually. The user only to be able to select the axis as a whole, so we tie the selected states of the tick labelsĪnd the axis base line together. Normally, axis base line, axis tick labels and axis labels are selectable separately, but we want QString newName = QInputDialog::getText(this, "QCustomPlot example", "New graph name:", QLineEdit::Normal, plItem->plottable()->name(), &ok) QCPPlottableLegendItem *plItem = qobject_cast(item) If (item) // only react if item was clicked (user could have clicked on border padding of legend where there is no item, then item is 0) Rename a graph by double clicking on its legend item Void MainWindow::legendDoubleClick(QCPLegend *legend, QCPAbstractLegendItem *item) QString newLabel = QInputDialog::getText(this, "QCustomPlot example", "New axis label:", QLineEdit::Normal, axis->label(), &ok) If (part = QCPAxis::spAxisLabel) // only react when the actual axis label is clicked, not tick label or axis backbone Set an axis label by double clicking on it Void MainWindow::axisLabelDoubleClick(QCPAxis *axis, QCPAxis::SelectablePart part) QString newTitle = QInputDialog::getText(this, "QCustomPlot example", "New plot title:", QLineEdit::Normal, title->text(), &ok)
![qt sync tutorial qt sync tutorial](https://i.ytimg.com/vi/H27uMS4x65A/maxresdefault.jpg)
Set the plot title by double clicking on it If (QCPTextElement *title = qobject_cast(sender())) Void MainWindow::titleDoubleClick(QMouseEvent* event) Ui->customPlot->setContextMenuPolicy(Qt::CustomContextMenu) Ĭonnect(ui->customPlot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextMenuRequest(QPoint))) setup policy and connect slot for context menu popup: connect slot that shows a message in the status bar when a graph is clicked:Ĭonnect(ui->customPlot, SIGNAL(plottableClick(QCPAbstractPlottable*,int,QMouseEvent*)), this, SLOT(graphClicked(QCPAbstractPlottable*,int))) make bottom and left axes transfer their ranges to top and right axes:Ĭonnect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange))) Ĭonnect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange))) Ĭonnect(ui->customPlot, SIGNAL(axisDoubleClick(QCPAxis*,QCPAxis::SelectablePart,QMouseEvent*)), this, SLOT(axisLabelDoubleClick(QCPAxis*,QCPAxis::SelectablePart))) Ĭonnect(ui->customPlot, SIGNAL(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*,QMouseEvent*)), this, SLOT(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*))) Ĭonnect(title, SIGNAL(doubleClicked(QMouseEvent*)), this, SLOT(titleDoubleClick(QMouseEvent*))) connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed:Ĭonnect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress())) Ĭonnect(ui->customPlot, SIGNAL(mouseWheel(QWheelEvent*)), this, SLOT(mouseWheel())) connect slot that ties some axis selections together (especially opposite axes):Ĭonnect(ui->customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChanged())) Ui->customPlot->legend->setSelectableParts(QCPLegend::spItems) // legend box shall not be selectable, only legend items Ui->customPlot->legend->setSelectedFont(legendFont)
![qt sync tutorial qt sync tutorial](https://i.ytimg.com/vi/o4sQWhA4NHQ/maxresdefault.jpg)
Ui->customPlot->legend->setFont(legendFont) Ui->customPlot->legend->setVisible(true) Ui->customPlot->yAxis->setLabel("y Axis") Ui->customPlot->xAxis->setLabel("x Axis") Ui->customPlot->plotLayout()->addElement(0, 0, title) QCPTextElement *title = new QCPTextElement(ui->customPlot, "Interaction Example", QFont("sans", 17, QFont::Bold)) Ui->customPlot->plotLayout()->insertRow(0) QCP::iSelectLegend | QCP::iSelectPlottables)
![qt sync tutorial qt sync tutorial](https://i.ytimg.com/vi/prO57xOuva4/maxresdefault.jpg)
Ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | Srand(QDateTime::currentDateTime().toTime_t()) MainWindow::MainWindow(QWidget *parent) :