Solved Can't paint points on my widget
-
begin(ui->liveFeedWidget);
is in the code you provided. That is the widget within my ui that I want to paint to. I don't want to paint directly to "this" as "this" has buttons, and labels. I want to paint to the QWidget inside it's form ui->liveFeedWidget
My header file
protected: void paintEvent(QPaintEvent *) override;
My source file
void PointViewer::paintEvent(QPaintEvent *) { QPen pen(Qt::green, 20, Qt::SolidLine, Qt::RoundCap); QPainter painter(this); painter.begin(ui->liveFeedWidget); painter.setPen(pen); painter.drawPoint(100,200); painter.end(); }
-
Hi
Sorry didnt spot it before.
So goal is to paint from
PointViewer to ui->liveFeedWidget
or IS the ui->liveFeedWidget a PointViewer instance?If liveFeedWidget is completely other widget , then it need to have the paintEvent and not
PointViewer.You cannot paint from other widget to other widgets.
-
@mrjj Ah, ok
So, liveFeedWidget is a widget I added to the PointViewer class's pointviewer.ui form file. Is it possible to access it from PointViewer and override it's paintEvent without my making another class and promoting it to that class? I don't like to make extra classes if I can avoid it, it makes the code harder to follow for those that come behind me. Self contained classes are best if it can be achieved.
-
@graniteDev said in Can't paint points on my widget:
liveFeedWidget
But is it a custom control ?
What type is it ? -
@mrjj it's just a QWidget
-
@graniteDev
Ok, you could use QLabel and paint to a pixmap and assign pixmap
to the label.
Else only way is to add paintEvent to custom widget.
PaintEvent is virtual and only way to override is to subclass. -
This still didn't work
void PointViewer::paintEvent(QPaintEvent *) { QPen pen(Qt::green, 20, Qt::SolidLine, Qt::RoundCap); QPainter painter(this); painter.setPen(pen); painter.drawPoint(100,200); }
I'm expecting, that at 20 pixels I should be able to see SOME dot on the screen, but I see nothing
-
There should be a point at 100,200
insert qDebug() to be sure its called.
If you really dont want a custom widget for drawing, you can do like
QPixmap pix(200, 200); pix.fill(Qt::blue); QPainter painter(&pix); painter.drawpoint(50, 50); ui->label->setPixmap(pix);
-
@mrjj WE HAVE A DOT!!!!!
UGH, I have no idea why the previous code did not work. But this looks like it does. I'm going to see if this works for me. I'm hoping that as it's updated with new data it does not flicker.
-
@graniteDev
\o/
Long live the DOT.Qwidgets uses double buffer system internally so often there is no flicker.
Did you use pixmap or paintEvent ?
-
@mrjj Crud, now I have a different problem, how do I draw different points with different sizes at the same time? Drawing many points of the same size is easy, that's readily handled, but I need to draw different sized points to denote how far or near a marker is that the point represents.
-
@mrjj said in Can't paint points on my widget:
@graniteDev
\o/
Long live the DOT.Qwidgets uses double buffer system internally so often there is no flicker.
Did you use pixmap or paintEvent ?
void PointViewer::paintMarkers(int x, int y, int z) { QPen pen(Qt::green, z, Qt::SolidLine, Qt::RoundCap); QPixmap pix(800,600); pix.fill(Qt::black); QPainter painter(&pix); painter.setPen(pen); painter.drawPoint(x,y); ui->pointViewerLabel->setPixmap(pix); }
and I got a large green dot about where I expected to
-
@graniteDev
Just include pointSize with the points.
like
struct PointData {
int x;
int y;
int z;
int DotSize
}and give that to drawing routine.
And in paintEvent / for image, simply set penSize from data member
Its easy to handle as a struct as else u get many loose variables.Since you clear the pixmap pr run.
pix.fill(Qt::black);
you need to draw all points each time. or keep pixmap as member in class and
draw on top each time.where does the points come from ?
-
@mrjj There is a tracking camera that provides live x-y-z data. I'll have to apply a transform to make it fit in my viewing space...but that feels like the easy part right now, as I already have ready access to that data.
I don't quite follow what your saying, won't that only draw one dot at a time with the specified size? Then clear, and paint another dot of a different size, etc?
I'm using the z data to determine size, but the issue as I'm looking at the pen, is I can't assign a size to the point, because the size is applied to the pen, and the pen is applied to the painter, not the individual points that "drawPoints" draws.
-
@graniteDev
maybe i didnt understand properlyCurrently one x,y,z is painted. if you call
paintMarkers again, it will only show the last point.
If plan it to show many sets of x,y,z, you must handle that.
One options is to move pixmap to member and not clear it in
paintMarkers. that way you can draw over/keep the other points.
That is the goal correct ?
To draw many 3d points on same pixmap?
Alternatively, it should store the points and redraw all each time. -
@mrjj Yes that is the goal, to have many points on a member variable pixMap that are all a unique size and position as determined by their x-y-z values.
-
@mrjj Interesting, so I added this code to my header file:
QPixmap m_pix;
This to my constructor:
PointViewer::PointViewer(QWidget *parent) : QWidget(parent), ui(new Ui::PointViewer), m_pix(800,600) { ui->setupUi(this); qDebug() << "test 11"; m_pix.fill(Qt::black); ui->pointViewerLabel->setPixmap(m_pix); }
And this to the paintMarkers function
void PointViewer::paintMarkers(int x, int y, int z) { QPen pen(Qt::green, z, Qt::SolidLine, Qt::RoundCap); QPainter painter(&m_pix); painter.setPen(pen); painter.drawPoint(x,y); ui->pointViewerLabel->update(); }
and then called paintMarkers() with a button
void PointViewer::on_pushButton_clicked() { paintMarkers(200, 300, 50); }
and this did not work, so I'm not sure how to use a pixmap as a member variable and still get this to work. The pixmap did appear as a black 800x600 rectangle, so that part is working, but no green dot.
Thank you for you help btw!
-
Hi
You are most welcome.
I think the error is
ui->pointViewerLabel->update();
as that would just make it draw the copy of pixmap it has.
you must call setPixmap again. (no need for update as that it will do it self) -
oh...ok so
ui->pointViewerLabel->update() doesn't work, it needs to be ui->pointViewerLabel->setPixmap(m_pix); -
@graniteDev
Yes as update will just make it draw the COPY of the m_pix u give it in ctor.