c++ - Qt如何连接rubberBandChanged信号

标签 c++ qt qt5 qt-signals qtcharts

我尝试将 QChartView 类中的 ruby​​BandChanged 信号链接到 MainWindow 类中的特定函数。

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;
};

主窗口.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的子级,并通过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中找到。

关于c++ - Qt如何连接rubberBandChanged信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49538489/

相关文章:

c++ - QOpenGLWidget 和 QPainter ,不能同时渲染 2D 和 3D

qt - 基于小数点的中心文本 QML 类型

c++ - 在 QScrollArea 中居中 QLabel 派生小部件

python - 通过 QBuffer 到 QGeometry 的 Numpy 数组

在 Linux 机器上使用 cmake Qt5 Webkit 进行 C++ 交叉编译,目标是使用 mingw 的 Windows,失败

c++ - boost addable 和 dll 的辅助功能错误

c++ - 处理 "Thrown exception type is not nothrow copy constructible"警告

c++ - MongoDB C++:mongocxx::pool 线程安全吗?

c++ - 如何使用 SDL 在两个独立的声卡上播放声音?

qt - 如何(正确)在 Qt-Embedded 中输出多语言文本?