[SOLVED] What is exactly a parent widget?
-
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.
-
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?
-
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_OBJECTpublic:
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_OBJECTpublic:
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_OBJECTpublic:
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 -
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...
-
Trying to isolate the behavior is always a good exercise in finding what's wrong. Good that you found the issue.