【问题标题】:What is the best way to draw plots in Qt?在 Qt 中绘制绘图的最佳方法是什么?
【发布时间】:2016-05-04 04:03:14
【问题描述】:

我正在使用 Qt 绘制这样的频谱图:

。 我还希望能够选择图形区域并对其进行编辑,以及滚动和缩放。

我正在考虑 QGraphicsView 类,但不确定它的性能。据我所知,QGraphicsView 中的对象是单独存储的,绘制大量点可能会影响性能。

我应该使用哪些 Qt 类来实现这一点?

【问题讨论】:

    标签: c++ qt graphics qgraphicsview


    【解决方案1】:

    绝对不要对每个点/标记都使用QGraphicsItem。好的方法是生成一个代表您的频谱图的QPixmap,然后将此像素图作为单个项目放入QGraphicsScene(QGraphicsPixmapItem 可以用于此)。

    要使用QPixmap,请使用QPainter。也许一个小例子会有用:

    const int spectrWidth = 1000;
    const int spectrHeight = 500;
    QPixmap spectrPixmap(spectrWidth, spectrHeight);
    QPainter p(&spectrPixmap);
    
    for (int ir = 0; ir < spectrHeight; ir++)
    {
        for (int ic = 0; ic < spectrWidth; ic++)
        {
            double data = getDataForCoordinates(ic, ir); // your function
            QColor color = getColorForData(data);  // your function
            p.setPen(color);
            p.drawPoint(ic, ir);
        }
    }
    

    getDataForCoordinates() 和 getColorForData() 只是演示其工作原理的示例函数。您可能有不同的方式来获取数据及其颜色。

    编辑

    但是,如果您不需要缩放/平移功能,那么直接在QWidget::paintEvent() 中的QWidget 上绘画而不使用QGraphicsView/QGraphicScene 就更容易了。

    【讨论】:

    • 我可以用QImage代替QPixmap吗?
    • 是的,你可以,但为什么呢? ... QPixmap 专为在屏幕上显示图像而设计和优化。 ...(来自 Qt 文档)
    【解决方案2】:

    我建议您为此使用QCustomPlot 或Qwt。 两者都有很好的文档。

    如果您选择使用 Qwt,请查看 QwtPlotSpectrogram Class。

    【讨论】:

      【解决方案3】:

      QCustomPlot 是外部库,如果您正在寻找 QT 原生的东西,请查看 QPainter class。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-16
        相关资源
        最近更新 更多