我正在为 QtCreator 中的实时输入编写一个绘图窗口,其中数据波形在用户指定的周期内以循环方式覆盖自身。
实际的绘图仪和轴初始化工作得很好,但是在每个周期结束时更新时间轴时遇到了一些麻烦。我认为擦除标签区域中的所有数据并使用新值重新绘制会起作用,但是在第一个周期结束时会擦除整个轴。我怀疑我使用 QPainter 存在一些问题。
第一次发帖,轻点我。
void T_AxisWidget::initAxis(double window_period) { period = window_period; t_pixmap = QPixmap(大小()); t_pixmap.fill(Qt::black);
QPainter painter(&t_pixmap); painter.initFrom(this); painter.setPen(Qt::red); painter.drawLine(0, 0, size().width(), 0); painter.drawLine(0, 0, 0, TICK_LENGTH); //always draw first tick at origin int tick_interval = size().width()/(num_ticks); int remainder = (size().width()%(num_ticks))-1; //pixels to be skipped to cover entire axis painter.drawText(0, TICK_LENGTH+2, tick_interval/2, size().height()-TICK_LENGTH+2, Qt::AlignLeft | Qt::AlignTop, QString::number(0)); qDebug() << "width = " << size().width(); qDebug() << "interval = " << size().width()/(num_ticks); for(int i=tick_interval; i<=size().width()-tick_interval; i+=tick_interval) { if (remainder>0) { ++i; //space ticks an extra pixel apart initially to achieve an even spread --remainder; qDebug() << "remainder = " << remainder; qDebug() << "tick = " << i; } painter.drawLine(i, 0, i, TICK_LENGTH); painter.drawText(i-(tick_interval/2), TICK_LENGTH+2, tick_interval, size().height()-TICK_LENGTH+2, Qt::AlignHCenter | Qt::AlignTop, QString::number((period/(double)num_ticks)*(i/tick_interval))); } painter.drawLine(size().width()-1, 0, size().width()-1, TICK_LENGTH); painter.drawText(size().width()-tick_interval/2, TICK_LENGTH+2, tick_interval/2, size().height()-TICK_LENGTH+2, Qt::AlignRight | Qt::AlignTop, QString::number(period)); }
void T_AxisWidget::setTValues (int multiplier) { double start = period*multiplier; int tick_interval = period/num_ticks;
QPainter painter(&t_pixmap); painter.initFrom(this); painter.setPen(Qt::red); QRect eraseblock(0, TICK_LENGTH+2, size().width(), size().height()); painter.eraseRect(eraseblock); painter.drawText(0, TICK_LENGTH+2, tick_interval/2, size().height()-TICK_LENGTH+2, Qt::AlignLeft | Qt::AlignTop, QString::number(start)); for(int i=1; i<num_ticks; i++) { painter.drawText(i*tick_interval-(tick_interval/2), TICK_LENGTH+2, tick_interval, size().height()-TICK_LENGTH+2, Qt::AlignHCenter | Qt::AlignTop, QString::number(start+(i*period/(double)num_ticks))); } painter.drawText(size().width()-(tick_interval/2), TICK_LENGTH+2, tick_interval/2, size().height()-TICK_LENGTH+2, Qt::AlignRight | Qt::AlignTop, QString::number(period*multiplier));
}
void T_AxisWidget::paintEvent(QPaintEvent * /* event */) { QStylePainterpainter(this); 画家.drawPixmap(0, 0, t_pixmap); }