0

I'm actually writing a module in qt (container) inerhiting from QWidget which is containing multiple plots presenting a graph where all of them inerhit from QWidget.

So it's given:

  • 1 container can have :
    • n plots where each of them contains
    • 1 graph.

I had yesterday a lot of time spent for figuring out a bug. It happened that just the n plots which were (for debug purposes) added to the container in its constructor to catch the paint events.

All plots that were added by the same method while the container was constructed allready, were not able to receive any events.

later on, a coworker after I requested assistance explained me, that my container (which is located in the mainwindow-form) needs to get asigned a layout in the Qtdesigner. I gave it a try and was suprised that it did solve my problem. After adding the dynamicly generated plots to the container and aswell adding them to the layout, all widgets were receiving the events as expected.

But since I wasn't able to understand his explanation and don't want to bother him with it further, I'm asking it here.

So why dynmacially generated widgets which are child objects of other dynamically generated QWidgets require the parent which is a child of the mainwindow to have a layout asigned? And if this is not just a exceptional case, why it isn't asigned by default and additional I wasn't able to find cases on the web, of tohers having such problems?

dhein
  • 6,431
  • 4
  • 42
  • 74
  • 1
    imho you should edit the question to remove any reference to your coworker. It is not really relevant for the question to mention that someone else could not answer it and imagine he will read this .... – 463035818_is_not_an_ai May 04 '16 at 09:20
  • @tobi303: it wasn't even my intention to let him appear bad more of making clear that I'm not that much into qt, therefor I expected it to be kinda relevant. But you're probaly right. – dhein May 04 '16 at 09:29
  • 1
    What events? I'd like to see an [SSCCE](http://sscce.org) that demonstrates this problem. – thuga May 04 '16 at 09:38
  • @thuga: any event of the qt eventloop. Hm, not quite sure what to put into, since I'm creating the objects dynamically and I'm not aware of what could have been causing the problem, yet. While just doing an `layout->addWidget(fop);` made things work. So the main part of my problem was done in qt-designer, while the other part would be alot of lines just configuring widgets. Can you help me determine whats required for the SSCCE here? – dhein May 04 '16 at 09:42
  • 1
    As small and compact code as possible that replicates the problem. You can use some dummy widget's that don't really do anything, except listen to some events. Everything you do in the designer, can be done in code. You can take a look at [this answer](http://stackoverflow.com/a/19367454/2257050) for a good example of an `SSCCE`. – thuga May 04 '16 at 09:49

1 Answers1

0

You're likely seeing the side effects of overlapping sibling widgets, or perhaps widgets having zero size. All that a layout does is resize them and ensure they don't overlap.

added to the container in its constructor to catch the paint events

What do you mean by "cathing" the paint events and what has that got to do with any constructors? You claim that all of your classes (the container, the plot, and the graph) derive from QWidget. They should all implement paintEvent, unless they are designed to have no content of their own other than a default background brush.

I imagine that you're somehow complicating things too much, but without self contained piece of code that we can compile to replicate your problem, we'll be going around in circles. Feel free to look around in my answers: they all consist of a single main.cpp, sometimes with quite a bit of functionality squeezed in. Almost universally, there's no code other than necessary to demonstrate a given technique. Your question should include similarly minimized, compileable example.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313