QT如何连接橡胶带信号



我尝试将橡胶带信号从QCHARTVIEW类链接到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;
};

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

当我在图表中使用橡胶带时,我从不进入RubberzoomAdapt((。

任何修复此操作的IDEE?

谢谢。

问题是,尽管QChartViewQGraphicsView继承,但它不使用相同的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;
}

可以在以下链接

中找到完整的示例

相关内容

  • 没有找到相关文章

最新更新