c++ - Qt how to create bitmap from data from QVector and show it on widget? -
i'm wondering how can create bitmap data , show on widget.
i have qvector vector, vector of points draw chart. how can repaint on widget using qbitmap? don't want draw on widget, prefer pass pixmap widget, show it.
how can this?
my code:
qpainter painter(pixmap); painter.setpen(qpen(qt::black, 2)); painter.drawpolyline(this->data.data(), this->data.size()); painter.drawline(qpointf(5,5),qpointf(50,50)); setpixmap(*pixmap);
here sample code. why it's not working? can't see on widget.
i have widget class
class widget : public qlabel { public: widget(qvector<qpointf> * data); ~widget(); protected: void paintevent(qpaintevent * event); private: qvector<qpointf> data; qpixmap *pixmap; };
in constructor have
widget::widget(qvector<qpointf> * data){ pixmap = new qpixmap(300,300); pixmap->fill(qt::red); }
and in paintevent
void waveform::paintevent(qpaintevent *event) { qpainter painter(pixmap); painter.setpen(qpen(qt::white, 2)); painter.drawpolyline(this->data.data(), this->data.size()); painter.drawline(qpointf(5,5),qpointf(50,50)); setpixmap(*pixmap); }
if replace qpainter painter(pixmap)
qpainter painter(this)
, can see chart. want use pixmap.
i think so, wasn't sure without full code, absolutely sure. should standard processing of paintevent. try this:
void waveform::paintevent(qpaintevent *e) { static const qpointf points[3] = { qpointf(10.0, 80.0), qpointf(20.0, 10.0), qpointf(80.0, 30.0), }; qpainter painter(pixmap); painter.setpen(qpen(qt::black, 2)); painter.drawpolyline(points, 3); painter.drawline(qpointf(5,5),qpointf(50,50)); setpixmap(*pixmap); qlabel::paintevent(e);//standard processing }
but think don't need paintevent @ all, can totally remove paintevent class or
void vertlabel::paintevent(qpaintevent *e) { qlabel::paintevent(e);//in case don't need paintevent @ all, remove cpp , header files }
and in constructor:
pixmap = new qpixmap(300,300); pixmap->fill(qt::red); this->resize(300,300); static const qpointf points[3] = { qpointf(10.0, 80.0), qpointf(20.0, 10.0), qpointf(80.0, 30.0), }; qpainter painter(pixmap); painter.setpen(qpen(qt::black, 2)); painter.drawpolyline(points, 3); painter.drawline(qpointf(5,5),qpointf(50,50)); setpixmap(*pixmap);
Comments
Post a Comment