gpt4 book ai didi

c++ - 在QwtPlot scaleDraw中绘制向内刻度线

转载 作者:行者123 更新时间:2023-11-30 05:28:58 25 4
gpt4 key购买 nike

我们如何在向内和向外绘制轴刻度线?我重新实现了 QwtScaleDraw 并用 drawTick 覆盖,但我不知道如何匹配刻度位置并使用

绘制额外的线
QwtPainter::drawLine(painter,QPointF,QPointF)

我试过了:

inside Plot::drawItems(QPainter *painter, const QRectF &rect, const QwtScaleMap map[axisCnt]) const

const QwtScaleMap ↦
for (j = 0; j < majTicks; j++)
{
y = map.transform(majTickList[j]);
QwtPainter::drawLine(painter, x, y, x + m_majTickLength, y);
}

但是axis margin和out axis的角不匹配,小偏差来了。我在这里截图了:

enter image description here

我完成了向内抽取

    void CustomScaleDraw::draw(QPainter *painter, const QPalette &palette) const
{


QwtScaleDraw::draw(painter, palette);


painter->save();

QPen pen = painter->pen();
pen.setColor(palette.color(QPalette::Foreground));
painter->setPen(pen);

int majLen = m_pPlotWidget->majorTickLength();
if (m_majTickStyle >= Both && majLen > 0){
QList<double> ticks = this->scaleDiv().ticks(QwtScaleDiv::MajorTick);
for (int i = 0; i < (int)ticks.count(); i++){
const double v = ticks[i];
if (this->scaleDiv().contains(v))
drawInwardTick(painter, v, majLen);
}
}
}


and
void CustomScaleDraw::drawInwardTick(QPainter *painter, double value, int len) const
{


int pw2 = qMin((int)painter->pen().width(), len) / 2;

QwtScaleMap scaleMap = this->scaleMap();

QPointF pos = this->pos();

int majLen = tickLength(QwtScaleDiv::MajorTick);


const int clw = m_pPlotWidget->lineWidth();
const int tval = scaleMap.transform(value);

bool draw = false;
if ( orientation() == Qt::Vertical ){
int low = (int)scaleMap.p2() + majLen;
int high = (int)scaleMap.p1() - majLen;
if ((tval > low && tval < high) ||
(tval > high && !m_pPlotWidget->axisEnabled (QwtPlot::xBottom) && !clw) ||
(tval < low && !m_pPlotWidget->axisEnabled(QwtPlot::xTop) && !clw)) draw = true;
} else {
int low = (int)scaleMap.p1() + majLen;
int high = (int)scaleMap.p2() - majLen;
if ((tval > low && tval < high) ||
(tval > high && !m_pPlotWidget->axisEnabled(QwtPlot::yRight) && !clw) ||
(tval < low && !m_pPlotWidget->axisEnabled(QwtPlot::yLeft) && !clw)) draw = true;
}

if (draw){

switch(alignment()){

case LeftScale:
{
QwtPainter::drawLine(painter, pos.x() + pw2, tval, pos.x() + len, tval);

break;
}
case RightScale:
{
QwtPainter::drawLine(painter, pos.x() - pw2, tval, pos.x() - len, tval);
break;
}
case BottomScale:
{
QwtPainter::drawLine(painter, tval, pos.y() - pw2, tval, pos.y() - len);
break;
}
case TopScale:
{
QwtPainter::drawLine(painter, tval, pos.y() + pw2, tval, pos.y() + len);
break;
}
}
}
// QwtPainter::setMetricsMap(metricsMap); // restore metrics map
}

我在 QwtPlot.cpp 中的比例设置

for (int i = 0; i < QwtPlot::axisCnt; i++) { QwtScaleWidget *scale = (QwtScaleWidget *) axisWidget(i);

    if(scale)
{
scale->setMargin(0);

//the axis title color must be initialized...
QwtText title = scale->title();
title.setColor(Qt::black);
scale->setTitle(title);

//...same for axis color
QPalette pal = scale->palette();
pal.setColor(QPalette::Foreground, QColor(Qt::black));
scale->setPalette(pal);

CustomScaleDraw *sd = new CustomScaleDraw(this);
sd->setTickLength(QwtScaleDiv::MinorTick, m_minTickLength);
sd->setTickLength(QwtScaleDiv::MediumTick, m_minTickLength);
sd->setTickLength(QwtScaleDiv::MajorTick, m_majTickLength);

setAxisScaleDraw(i,sd);

}
}
plotLayout()->setAlignCanvasToScales( true );

m_minTickLength = 5; m_majTickLength = 9;

最佳答案

您可以通过向绘图添加额外的比例项来实现向内刻度的效果,这些项具有与其位置通常需要的相反对齐属性。

与自定义画家相比,这需要方式更少的代码,而且您没有对齐问题。

输出

enter image description here

代码

#include <qapplication.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_layout.h>
#include <qwt_symbol.h>
#include <qwt_legend.h>
#include <qwt_scale_widget.h>
#include <qwt_plot_scaleitem.h>

int main( int argc, char **argv )
{
// Enable high-DPI scaling with Qt 5.6+
#if (QT_VERSION >= QT_VERSION_CHECK(5,6,0))
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

QApplication a( argc, argv );

QwtPlot plot;
plot.setTitle( "Plot Demo" );
plot.canvas()->setContentsMargins(0, 0, 0, 0);
plot.setStyleSheet("QwtPlot{ border: 0; }");
plot.canvas()->setStyleSheet("QwtPlotCanvas {border: none; margin: 1; background-color: white;}");
plot.plotLayout()->setCanvasMargin(0);

plot.enableAxis(QwtPlot::yLeft);
plot.enableAxis(QwtPlot::yRight);
plot.enableAxis(QwtPlot::xBottom);
plot.enableAxis(QwtPlot::xTop);

plot.setAxisScale( QwtPlot::yLeft, 0.0, 1000.0 );
plot.setAxisScale(QwtPlot::yRight, 0.0, 1000.0);
plot.setAxisScale(QwtPlot::xBottom, 0.0, 1000.0);
plot.setAxisScale(QwtPlot::xTop, 0.0, 1000.0);

plot.axisWidget(QwtPlot::yLeft)->setMargin(0);
plot.axisWidget(QwtPlot::yLeft)->setSpacing(0);
plot.axisWidget(QwtPlot::yRight)->setMargin(0);
plot.axisWidget(QwtPlot::xBottom)->setMargin(0);
plot.axisWidget(QwtPlot::xTop)->setMargin(0);

// create inward pointing ticks, and disable their labels
// notice the alignment is *opposite* to the position.
// in production code, don't hardcode the positions obviously.
QwtPlotScaleItem *yLeftScaleItem = new QwtPlotScaleItem(QwtScaleDraw::RightScale, 0);
yLeftScaleItem->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Labels, false);
yLeftScaleItem->attach(&plot);

QwtPlotScaleItem *yRightScaleItem = new QwtPlotScaleItem(QwtScaleDraw::LeftScale, 1000);
yRightScaleItem->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Labels, false);
yRightScaleItem->attach(&plot);

QwtPlotScaleItem *xBottomScaleItem = new QwtPlotScaleItem(QwtScaleDraw::TopScale, 0);
xBottomScaleItem->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Labels, false);
xBottomScaleItem->attach(&plot);

QwtPlotScaleItem *xTopScaleItem = new QwtPlotScaleItem(QwtScaleDraw::BottomScale, 1000);
xTopScaleItem->scaleDraw()->enableComponent(QwtAbstractScaleDraw::Labels, false);
xTopScaleItem->attach(&plot);

plot.updateCanvasMargins();

plot.resize( 500, 400 );
plot.show();

return a.exec();
}

关于c++ - 在QwtPlot scaleDraw中绘制向内刻度线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36597724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com