0

I've been through a lot of material on the internet about this, but haven't found what I need. I want to make a reusable painting function in Qt. For example, I would have a

void paintRectangle(QPaintEvent*, int x, int y);

function, which I can call in a loop and draws a rectangle starting at the x and y coordinates. Is this possible? Could you please write down the draft/outline on how I should write it and how I can call it in the loop when it's ready? I really didn't find anything on this. Also, how would I call this function? What do I write in place of the QPaintEvent * when calling?

As I've noticed, paintevents get called before any class constructors. Is this correct? I'd like to have a certain amount of rectangles on the screen, which is depending on an n variable, which is being declared when a certain class gets instantiated. As of my current trials, it seemed that the n was undefined when my fuction tried to paint anything.

lte__
  • 7,175
  • 25
  • 74
  • 131
  • *What do I write in place of the `QPaintEvent *` when calling* => You presumably want to work with a [QPainter](http://doc.qt.io/qt-5/qpainter.html). *"As I've noticed, paintevents get called before any class constructors. Is this correct?"* Which class constructors? It seems you should work through some tutorials and have a concrete question about a piece of code (e.g. [MCVE](http://stackoverflow.com/help/mcve)) before asking. Tutorials here: http://doc.qt.io/qt-5/examples-painting.html – HostileFork says dont trust SE Mar 27 '16 at 16:42

1 Answers1

1

The only limitation is that when you paint on widgets, it must happen in that widget's paint event. If your paint device is not a widget, then it doesn't matter.

Other than that, there is nothing preventing you from calling any number of painting functions any number of times with any parameters you'd want, just make sure that in the case of a widget, they are called in that widget's paint event. For example:

void paintEvent(QPaintEvent *) {
    QPainter p(this);
    // setup painter
    for (int i = 0; i < 200; i += 10) drawFoo(i, p);
}

void drawFoo(int i, QPainter & p) {
    p.drawPoint(i, i);
}

As I've noticed, paintevents get called before any class constructors. Is this correct?

Where did you notice that? I highly doubt a widget would be painted before it is constituted ;) Your concern is groundless, no widget is painted before it is constructed, in fact, you can construct a widget without painting it - if you don't call show(). If you put a debug message in the constructor and paint event, you will see that the constructor is always executed before the paint event. In fact, it would be entirely "illegal" to invoke a member function of an object which is not yet constructed fully.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • *If your paint device is not a widget, then it doesn't matter. Other than that, there is nothing preventing you from calling any number of painting functions any number of times with any parameters you'd want* - There are some [constraints regarding threads](http://stackoverflow.com/a/19801659/211160), though the things you can't do seem to gradually decrease over time. – HostileFork says dont trust SE Mar 27 '16 at 16:58
  • @HostileFork - this restriction/limitation is not related to `QPainter` but to `QPixmap`. I don't think multithreading is in the context of the question, but who knows, it is a bit murky. – dtech Mar 27 '16 at 17:00
  • The threading issues are historically wider than that, as the link I provided explains. Consider [QFontDatabase::supportsThreadedFontRendering](http://doc.qt.io/qt-4.8/qfontdatabase.html#supportsThreadedFontRendering) and the statement from the documentation: *"a return value of false means that all QPainter::drawText() calls outside the GUI thread will not produce readable output."* There are little bits to bite you here and there that come up. – HostileFork says dont trust SE Mar 27 '16 at 17:05
  • You're right, what I said doesn't quite feel logical... What I am experiencing is as follows. I have two classes, A and B. Class A contains the variable n and it's value is 6 by default. Class B has an instant of class A. When I ran my code,in my QPaintEvent I wanted to use the n variable, but my program crashed. When I gave value to n in the QPaintEvent function, it worked. If not, it crashed. That's why I thought of it. But it doesn't make sense, you're right. :) – lte__ Mar 27 '16 at 17:24
  • Just post your code, I can't find much sense in what you say. – dtech Mar 27 '16 at 17:27
  • Right, because there isn't. I've made a mistake, but now corrected it. It also had nothing to do with when the painter and when the constructor gets called. Thank you! – lte__ Mar 27 '16 at 17:57
  • 1
    @HostileFork @ddriver `supportsThreadedFontRendering` is obsolete. It always returns true in Qt 5. It *used* to be a design limitation in ancient times :) – Kuba hasn't forgotten Monica Mar 29 '16 at 13:41