#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "qcustomplot.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
public slots:
private slots:
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "qcustomplot.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
private slots:
void mousePress(QMouseEvent* mevent);
void mouseMove(QMouseEvent *mevent);
void mouseRelease(QMouseEvent *mevent);
private:
Ui::MainWindow *ui;
QRubberBand *rubberBand;
QPoint origin;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
#include "mainwindow.h"
#include "ui_mainwindow.h"
{
// if an axis is selected, only allow the direction of that axis to be dragged
// if no axis is selected, both directions may be dragged
if(mevent->button() == Qt::RightButton){
origin = mevent->pos();
if (!rubberBand){
}
rubberBand->show();
}else{
/*This below is striaght from the QCustomPlot example and can be ignored in the context of the QRubberBand solution*/
if (ui->customPlot->xAxis->selected().testFlag(QCPAxis::spAxis))
ui->customPlot->setRangeDrag(ui->customPlot->xAxis->orientation());
else if (ui->customPlot->yAxis->selected().testFlag(QCPAxis::spAxis))
ui->customPlot->setRangeDrag(ui->customPlot->yAxis->orientation());
else
ui->customPlot->setRangeDrag(Qt::Horizontal|Qt::Vertical);
}
}
{
if(rubberBand)
rubberBand
->setGeometry
(QRect(origin, mevent
->pos
()).
normalized());
}
{
if(rubberBand)
rubberBand->hide();
}
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
rubberBand = 0;/*<-THIS WAS THE KEY! Without it the setGeometry call in the mousePress slot always stuffed up*/
/*set up a custom plot: This is pulled straight from one of the QCustomPlot examples*/
ui->customPlot->setInteractions(QCustomPlot::iRangeDrag | QCustomPlot::iRangeZoom | QCustomPlot::iSelectAxes |
QCustomPlot::iSelectLegend | QCustomPlot::iSelectPlottables | QCustomPlot::iSelectTitle);
ui->customPlot->setRangeDrag(Qt::Horizontal|Qt::Vertical);
ui->customPlot->setRangeZoom(Qt::Horizontal|Qt::Vertical);
ui->customPlot->xAxis->setRange(-20, 20);
ui->customPlot->yAxis->setRange(-20, 20);
ui->customPlot->setupFullAxesBox();
ui->customPlot->setTitle("Interaction Example");
ui->customPlot->xAxis->setLabel("x Axis");
ui->customPlot->yAxis->setLabel("y Axis");
ui->customPlot->legend->setVisible(true);
QFont legendFont
= font
();
legendFont.setPointSize(10);
ui->customPlot->legend->setFont(legendFont);
ui->customPlot->legend->setSelectedFont(legendFont);
ui->customPlot->legend->setSelectable(QCPLegend::spItems); // legend box shall not be selectable, only legend items
// connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed:
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
void MainWindow::mousePress(QMouseEvent* mevent)
{
// if an axis is selected, only allow the direction of that axis to be dragged
// if no axis is selected, both directions may be dragged
if(mevent->button() == Qt::RightButton){
origin = mevent->pos();
if (!rubberBand){
rubberBand = new QRubberBand(QRubberBand::Rectangle, ui->customPlot);
}
rubberBand->setGeometry(QRect(origin, QSize()));
rubberBand->show();
}else{
/*This below is striaght from the QCustomPlot example and can be ignored in the context of the QRubberBand solution*/
if (ui->customPlot->xAxis->selected().testFlag(QCPAxis::spAxis))
ui->customPlot->setRangeDrag(ui->customPlot->xAxis->orientation());
else if (ui->customPlot->yAxis->selected().testFlag(QCPAxis::spAxis))
ui->customPlot->setRangeDrag(ui->customPlot->yAxis->orientation());
else
ui->customPlot->setRangeDrag(Qt::Horizontal|Qt::Vertical);
}
}
void MainWindow::mouseMove(QMouseEvent *mevent)
{
if(rubberBand)
rubberBand->setGeometry(QRect(origin, mevent->pos()).normalized());
}
void MainWindow::mouseRelease(QMouseEvent *mevent)
{
if(rubberBand)
rubberBand->hide();
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
rubberBand = 0;/*<-THIS WAS THE KEY! Without it the setGeometry call in the mousePress slot always stuffed up*/
/*set up a custom plot: This is pulled straight from one of the QCustomPlot examples*/
ui->customPlot->setInteractions(QCustomPlot::iRangeDrag | QCustomPlot::iRangeZoom | QCustomPlot::iSelectAxes |
QCustomPlot::iSelectLegend | QCustomPlot::iSelectPlottables | QCustomPlot::iSelectTitle);
ui->customPlot->setRangeDrag(Qt::Horizontal|Qt::Vertical);
ui->customPlot->setRangeZoom(Qt::Horizontal|Qt::Vertical);
ui->customPlot->xAxis->setRange(-20, 20);
ui->customPlot->yAxis->setRange(-20, 20);
ui->customPlot->setupFullAxesBox();
ui->customPlot->setTitle("Interaction Example");
ui->customPlot->xAxis->setLabel("x Axis");
ui->customPlot->yAxis->setLabel("y Axis");
ui->customPlot->legend->setVisible(true);
QFont legendFont = font();
legendFont.setPointSize(10);
ui->customPlot->legend->setFont(legendFont);
ui->customPlot->legend->setSelectedFont(legendFont);
ui->customPlot->legend->setSelectable(QCPLegend::spItems); // legend box shall not be selectable, only legend items
// connect slots that takes care that when an axis is selected, only that direction can be dragged and zoomed:
connect(ui->customPlot, SIGNAL(mousePress(QMouseEvent*)), this, SLOT(mousePress(QMouseEvent*)));
connect(ui->customPlot, SIGNAL(mouseMove(QMouseEvent*)), this, SLOT(mouseMove(QMouseEvent*)));
connect(ui->customPlot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(mouseRelease(QMouseEvent*)));
}
To copy to clipboard, switch view to plain text mode
Bookmarks