问题
I am able to read ECG signals and plot the wave using QPainter. But the resulting wave is formed by removing the first coordinate and appending the new coordinates as the last point. So this gives a scrolling effect to the wave.
I wanted to know if there was any way to paint the wave like how a patient monitoring system does (a black bar running the length of the wave and updating the wave like this).
Code examples or snippets will be very helpful.Thanks.
回答1:
Here is a quick example in a few lines, far from perfect, but enough to get the idea:
class ECG : public QWidget {
Q_OBJECT
public:
ECG(QWidget * p) : QWidget(p), t(0), x(0), lastPoint(0,0) {
setAttribute(Qt::WA_NoSystemBackground); // don't erase previous painting
}
void paintEvent(QPaintEvent *) {
QPainter painter(this);
painter.setPen(QPen(Qt::green, 2));
painter.fillRect(x, 0, 60, height(), Qt::black);
if (line.length() < 100) painter.drawLine(line); // don't draw a line accross the screen
}
public slots:
void drawReading(qreal reading) {
x = t++ % width();
QPointF newPoint(x, (reading * height() * 0.4) + (height() * 0.5));
line = QLineF(lastPoint, newPoint);
lastPoint = newPoint;
update();
}
private:
quint32 t, x;
QPointF lastPoint;
QLineF line;
};
The widget will draw something similar to what you want, and as is it is set to accept readings in the range of -1.0 to 1.0.
You can test it with a generator:
class Gen : public QObject {
Q_OBJECT
public:
Gen(int f) : time(0) {
t.setInterval(30);
freq = (2 * 3.14159) / f;
connect(&t, QTimer::timeout, [&](){
qreal r = sin(time);
time = fmod(time + freq, 2 * 3.14159);
emit newReading(r);
});
}
public slots:
void toggle() { t.isActive() ? t.stop() : t.start(); }
signals:
void newReading(qreal);
private:
QTimer t;
qreal time, freq;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ECG e(0);
e.show();
Gen g(60);
QObject::connect(&g, SIGNAL(newReading(qreal)), &e, SLOT(drawReading(qreal)));
g.toggle();
return a.exec();
}
来源:https://stackoverflow.com/questions/33708497/ecg-like-waveform-painting-using-qpainter