我正在使用 Qt 绘制这样的频谱图:
. 我还希望能够选择图形区域并对其进行编辑,以及滚动和缩放。
我正在考虑 QGraphicsView
类,但不确定它的性能。据我所知,QGraphicsView
中的对象是单独存储的,绘制大量点可能会影响性能。
我应该使用哪些 Qt 类来实现这一点?
最佳答案
绝对不要对每个点/标记使用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
。
关于c++ - 在 Qt 中绘制绘图的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37018210/