0

有没有办法做到这一点?我正在尝试创建一个标签以对应于我使用 QwtPlotMarker 创建的标记。我的目标是显示一个标签,其中包含我的点击坐标 m_xPos 和 m_yPos,在以下示例中包含我到目前为止的代码:

QwtPlotMarker *testMarker = new QwtPlotMarker();
testMarker->setLineStyle(QwtPlotMarker::HLine);
testMarker->setLabelAlignment(Qt::AlignRight | Qt::AlignBottom);
testMarker->setLinePen(QPen(QColor(200,150,0), 0, Qt::DashDotLine));
testMarker->setSymbol( QwtSymbol(QwtSymbol::Diamond, QColor(Qt::yellow), QColor(Qt::green), QSize(7,7)));
testMarker->setXValue(m_xPos);
testMarker->setYValue(m_yPos);
testMarker->show();
testMarker->attach(d_graph->plotWidget());
testMarker->setLabel(....)

m_xPos 和 m_yPos 是 std::string

4

3 回答 3

1

Looking at the docs for QwtText, there's one constructor that takes a QString. Is this Qt's QString? If so, you can convert it like this:

wstring xPos2(m_xPos.begin(), m_xPos.end());
testMarker->setXValue(QString(xPos2.data(), xPos2.length()));

wstring yPos2(m_yPos.begin(), m_yPos.end());
testMarker->setYValue(QString(m_yPos.data(), m_yPos.length()));

Note that this just converts bytes to Unicode characters by having the wstring constructor simply assign each char to a wchar_t. A more robust way would be actually performing an encoding conversion, with something like Windows's MultiByteToWideString.

于 2012-09-18T11:51:21.170 回答
1
QwtPlotMarker *testMarker = new QwtPlotMarker();
testMarker->setLineStyle(QwtPlotMarker::HLine);
testMarker->setLabelAlignment(Qt::AlignRight | Qt::AlignBottom);
testMarker->setLinePen(QPen(QColor(200,150,0), 0, Qt::DashDotLine));
testMarker->setSymbol( QwtSymbol(QwtSymbol::Diamond, QColor(Qt::yellow), QColor(Qt::green), QSize(7,7)));
testMarker->setXValue(m_xPos);
testMarker->setYValue(m_yPos);
testMarker->show();
testMarker->attach(d_graph->plotWidget());

QwtText markerLabel = QString::fromStdString(+ m_xPos + ", " + m_yPos +);
testMarker->setLabel(markerLabel);

是我需要做的。我已经想通了,谢谢。我没有看到使用 QString 的构造函数;我用它来获取连接的字符串,转换为 QString,然后我能够在绘图上显示它。

于 2012-09-18T13:10:36.110 回答
0

QwtText对象可以用QString对象构造,对象可以用 C 字符串构造。因此,以下应该有效:

std::string my_string("Hello, World!");
QwtText markerLabel(my_string.c_str());
于 2013-11-08T05:45:26.793 回答