Reputation: 141
Is there a way to do this? I am trying to create a label to correspond to a marker I have created using QwtPlotMarker. My aim is to display a label containing the coordinates of my click, m_xPos and m_yPos, in the following example containing the code I have so far:
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 and m_yPos are std::string
Upvotes: 0
Views: 964
Reputation: 1936
QwtText
objects can be constructed with QString
objects, which can be constructed from C strings. Thus, the following should work:
std::string my_string("Hello, World!");
QwtText markerLabel(my_string.c_str());
Upvotes: 0
Reputation: 141
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);
is what I needed to do. I have figured it out, thanks. I had not seen the constructor that took QString; I used that to take concatenated strings, convert to QString and I was then able to display that on a plot.
Upvotes: 1
Reputation: 6678
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.
Upvotes: 1