Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] What is exactly a parent widget?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] What is exactly a parent widget?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 14.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aureshinite
    wrote on last edited by
    #1

    This may sounds stupid, but ca n somebody explain me what is a child widget?

    I created two custom widgets classes in the goal of them being shown at the same location in my window (overlay):
    WidgetParent and WidgetChild. I create both of them in the same location, and WidgetParent has the main window as
    "parent" (what I passed in the constructor) and WidgetChild has WidgetParent as "parent". When I call parent widget from
    WidgetChild I got WidgetParent(0xaddress) as expected. but in WidgetParent every single child-related method behave as if
    there is no child. Plus the child is not paint on the screen.

    So long story short, How do the Child\Parent relationship works? Do you have to add a widget B to a widget A layout for A to be
    a real parent??

    Thanks in advance.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      Basically a parent widget is a widget on which the child widget is drawn.

      Layouts aren't required, but you have to tell one of them that the relationship is there. Layouts just add some convenience (showing, resizing). It is probably easier to take on the point where your confusion starts. Could you post the offending code?

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        aureshinite
        wrote on last edited by
        #3

        Oh Gosh I tried to write an example to simulate my applcation, and it works.. while my application doesn't. I may have mess up something. This is the behaviour I expect:
        @
        #include <QPainter>
        #include <QWidget>
        #include <QImage>
        #include <QVBoxLayout>
        class VideoDisplay;
        class VideoOverlay;
        class BasicGUI;

        class VideoDisplay: public QWidget
        {
        Q_OBJECT

        public:
        explicit VideoDisplay(QWidget *parent = 0)
        : QWidget(parent)
        {
        img = QImage(QSize(200,200),QImage::Format_RGB888);
        img.fill(0);
        QPainter painter(&img);
        QRect rect(0,0,200,200);
        painter.fillRect(rect,QColor(0,255,0));
        painter.drawRect(rect);
        resize(img.size());
        }

        QSize minimumSizeHint () const{
        

        return img.size();
        }

        void paintEvent(QPaintEvent *event){
        

        QPainter painter;
        painter.begin(this);
        painter.drawImage(0,0,img);
        painter.end();
        }

        private:
        QImage img;
        };

        class VideoOverlay: public QWidget
        {
        Q_OBJECT

        public:
        explicit VideoOverlay(QWidget *parent = 0)
        : QWidget(parent)
        {
        img = QImage(QSize(200,200),QImage::Format_ARGB32);
        img.fill(0);
        //QPen p(QColor(200,0,0,128));
        QPainter painter(&img);

        QRect rect1(0,0,100,200);
        QRect rect2(100,0,100,200);
        painter.fillRect(rect1,QColor(255,0,0,128));
        painter.drawRect(rect1);
        painter.fillRect(rect2,QColor(0,0,255,128));
        painter.drawRect(rect2);
        resize(img.size());
        }

        void paintEvent(QPaintEvent *event){
        

        QPainter painter;
        painter.begin(this);
        painter.drawImage(50,50,img);
        painter.end();
        }

        private:
        QImage img;
        };

        class BasicGUI: public QWidget
        {
        Q_OBJECT

        public:
        explicit BasicGUI(QWidget *parent = 0)
        : QWidget(parent)
        {
        mainLayout = new QVBoxLayout(this);
        display = new VideoDisplay(this);
        overlay = new VideoOverlay(display);
        mainLayout->addWidget(display);
        mainLayout->setMargin(0);
        resize(250,250);
        }
        private:
        VideoDisplay *display;
        VideoOverlay *overlay;

        QVBoxLayout *mainLayout;
        

        };
        @

        In the real app BasicGui has many embedded layouts, other custom widgets and objects.
        And it doesn't do the job... I only see the Display, and not the Overlay. How does the others objects can influence this?

        And I draw first in an image because images are created by others objects.
        PS: My code can't be posted here

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aureshinite
          wrote on last edited by
          #4

          I found the issue... I tried to see what I do differently in my app and in this example relative to the 3 widgets and there was only one: I created the Overlay after the call QWidget::show() of the top level widget, and it was hidden.
          I also didn't resized the overlay at its creation (at this point I still don't know its size), so the first time I tried to put some show() in the code it appeared, but unfortunately the tiny default area displayed had complete blank there :|.

          Now I resize each time I got new image data, and with show() everything works just fine, call to child functions in VideoDisplay succeed...

          1 Reply Last reply
          0
          • F Offline
            F Offline
            Franzk
            wrote on last edited by
            #5

            Trying to isolate the behavior is always a good exercise in finding what's wrong. Good that you found the issue.

            "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved