0

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

programmingNoob
  • 141
  • 2
  • 3
  • 8

3 Answers3

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.

user1610015
  • 6,561
  • 2
  • 15
  • 18
  • Thanks for your reply. However, the code I have given already sets the position of the marker. What I want to do is to create a corresponding label using QwtPlotMarker's setLabel() and I want said label to then display the coordinates of where the marker is. – programmingNoob Sep 18 '12 at 12:49
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);

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.

programmingNoob
  • 141
  • 2
  • 3
  • 8
0

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());
Chris McGrath
  • 1,936
  • 16
  • 17