简体   繁体   中英

Why a layout is required for multiple widgets recieving events?

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

So it's given:

  • 1 container can have :
    • n plot s 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 plot s which were (for debug purposes) added to the container in its constructor to catch the paint events.

All plot s 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 plot s 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM