Qt如何连接rubberBandChanged信号
Qt How to connect the rubberBandChanged signal
我尝试 link 来自 QChartView class 的 rubberBandChanged 信号到 MainWindow class 中的特定函数 class。
MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void rubberZoomAdapt(QRect, QPointF, QPointF);
private:
Ui::MainWindow *ui;
QChartView* qcvChart;
Chart* chart;
};
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
qcvChart(new QChartView),
chart(new Chart)
{
ui->setupUi(this);
//Connexion
QObject::connect(qobject_cast<QGraphicsView*>(this->qcvChart),
&QGraphicsView::rubberBandChanged,
this,
&MainWindow::rubberZoomAdapt);
this->qcvChart->setChart(this->chart);
this->qcvChart->setRubberBand(QChartView::HorizontalRubberBand);
}
void MainWindow::rubberZoomAdapt(QRect r, QPointF fp, QPointF tp)
{
static int i = 0;
qDebug() << "(rubberZoomAdapt) RubberBand Event: " << QString::number(i++);
}
当我在图表中使用 rubberBand 时,我从不输入 rubberZoomAdapt()。
有解决这个问题的想法吗?
谢谢。
问题是虽然 QChartView
继承自 QGraphicsView
但它不使用相同的 QRubberBand
所以 rubberBandChanged
信号永远不会发出。
解决方案是寻找 QRubberBand
因为它是 QChartView
的 child 并通过 resizeEvent
事件过滤它,然后创建我们自己的信号:
*.h
...
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
bool eventFilter(QObject *watched, QEvent *event) override;
public slots:
void rubberZoomAdapt(QPointF fp, QPointF tp);
signals:
void rubberBandChanged(QPointF fp, QPointF tp);
private:
Ui::MainWindow *ui;
QChartView* qcvChart;
QChart* chart;
QRubberBand *rubberBand;
};
...
*.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
qcvChart(new QChartView),
chart(new QChart)
{
ui->setupUi(this);
qcvChart->setChart(chart);
qcvChart->setRubberBand(QChartView::HorizontalRubberBand);
rubberBand = qcvChart->findChild<QRubberBand *>();
rubberBand->installEventFilter(this);
connect(this, &MainWindow::rubberBandChanged,this, &MainWindow::rubberZoomAdapt);
setCentralWidget(qcvChart);
...
}
...
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(watched == rubberBand && event->type() == QEvent::Resize){
QPointF fp = chart->mapToValue(rubberBand->geometry().topLeft());
QPointF tp = chart->mapToValue(rubberBand->geometry().bottomRight());
emit rubberBandChanged(fp, tp);
}
return QMainWindow::eventFilter(watched, event);
}
void MainWindow::rubberZoomAdapt(QPointF fp, QPointF tp)
{
qDebug() << "(rubberZoomAdapt) RubberBand Event: "<<fp<<tp;
}
完整的例子可以在下面找到link
我尝试 link 来自 QChartView class 的 rubberBandChanged 信号到 MainWindow class 中的特定函数 class。
MainWindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void rubberZoomAdapt(QRect, QPointF, QPointF);
private:
Ui::MainWindow *ui;
QChartView* qcvChart;
Chart* chart;
};
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
qcvChart(new QChartView),
chart(new Chart)
{
ui->setupUi(this);
//Connexion
QObject::connect(qobject_cast<QGraphicsView*>(this->qcvChart),
&QGraphicsView::rubberBandChanged,
this,
&MainWindow::rubberZoomAdapt);
this->qcvChart->setChart(this->chart);
this->qcvChart->setRubberBand(QChartView::HorizontalRubberBand);
}
void MainWindow::rubberZoomAdapt(QRect r, QPointF fp, QPointF tp)
{
static int i = 0;
qDebug() << "(rubberZoomAdapt) RubberBand Event: " << QString::number(i++);
}
当我在图表中使用 rubberBand 时,我从不输入 rubberZoomAdapt()。
有解决这个问题的想法吗?
谢谢。
问题是虽然 QChartView
继承自 QGraphicsView
但它不使用相同的 QRubberBand
所以 rubberBandChanged
信号永远不会发出。
解决方案是寻找 QRubberBand
因为它是 QChartView
的 child 并通过 resizeEvent
事件过滤它,然后创建我们自己的信号:
*.h
...
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
bool eventFilter(QObject *watched, QEvent *event) override;
public slots:
void rubberZoomAdapt(QPointF fp, QPointF tp);
signals:
void rubberBandChanged(QPointF fp, QPointF tp);
private:
Ui::MainWindow *ui;
QChartView* qcvChart;
QChart* chart;
QRubberBand *rubberBand;
};
...
*.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
qcvChart(new QChartView),
chart(new QChart)
{
ui->setupUi(this);
qcvChart->setChart(chart);
qcvChart->setRubberBand(QChartView::HorizontalRubberBand);
rubberBand = qcvChart->findChild<QRubberBand *>();
rubberBand->installEventFilter(this);
connect(this, &MainWindow::rubberBandChanged,this, &MainWindow::rubberZoomAdapt);
setCentralWidget(qcvChart);
...
}
...
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(watched == rubberBand && event->type() == QEvent::Resize){
QPointF fp = chart->mapToValue(rubberBand->geometry().topLeft());
QPointF tp = chart->mapToValue(rubberBand->geometry().bottomRight());
emit rubberBandChanged(fp, tp);
}
return QMainWindow::eventFilter(watched, event);
}
void MainWindow::rubberZoomAdapt(QPointF fp, QPointF tp)
{
qDebug() << "(rubberZoomAdapt) RubberBand Event: "<<fp<<tp;
}
完整的例子可以在下面找到link