Help with multiple children widgets having the same parent
-
Hey everybody!
I'm using _QPropertyAnimation_ to animate a child widget to pop in and out of the parent widget. On a button click, I want one child to pop out while the other child pops in. I'm doing _setParent_ and setting the mainwindow as the parent for both child widgets in the constructor. However, the child widgets are shown only when I initialize them in the parent's constructor and not anywhere else.
@
pb = new ProgramBanner();
pb->setParent(this);gb = new GenreBanner(); gb->setParent(this);
@
For the animation, I use
@
pb_ani->setDuration(500);
pb_ani->setEndValue(QPoint(this->pos().x(), widget_hidden));
pb_ani->setEasingCurve(QEasingCurve::InBack);
pb_ani->start();
@The animation works fine when both child widgets are initialized in the constructor. But one child widget overlaps the other. I am not able to display one child at a time. Say, the programbanner is displayed in the beginning, then, on the click of a button, I want the programbanner to pop out and the genrebanner to pop in and vice versa. I've tried many things but failed.
I know this a long post but please please help me out. Thanks!
-
Hi,
If you want to show a widget that has no parent, you have to at least call show()
-
b1gsnak3
Ok the finished signal idea doesn't work for me. Every time one child pops out, the other pops in. I want this to happen only if the user presses a key on the keyboard. Let me give you an example for better understanding.
Assume there are 3 widgets: A, B and C
Keys A, B and C will make their corresponding child widgets appear on the first click, and disappear on the second.
So no matter which child is currently "popped in", it will disappear and the new child will be shown.
By default, I want 'A' to be on screen. On a keypress event, say 'C' is pressed, I want 'A' to pop out and 'C' to pop in. But I'm not able to create a new instance of 'C' widget outside of the constructor. Why is that?
-
Without more code I cannot tell you why you cannot create a new instance outside of the constructor as this shouldn't be a problem. I can only assume (which would not help you very much). So post a minimal of code from your declarations of A, B, C widgets and any methods that use those widgets. You can rename them to whatever you want, I am just interested in seeing how you are using them. Thank you. Also, for your issue regarding if it it the first key pressed event you could try using different if statements (either you create a bool value just for the first widget, as the rest do not interest you, or you could create a integer variable with 3 values if you are interested which of the widgets is showing).
-
@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->resize(availableGeometry.size());pb = new ProgramBanner(); pb->setParent(this); gb = new GenreBanner(); //This part needs to be set from the keypress event gb->setParent(this); gb->hide(); installEventFilter(this);
}
bool MainWindow::eventFilter(QObject *target, QEvent *event)
{
if (event->type() == QEvent::KeyPress)
{
QKeyEvent keyEvent = static_cast<QKeyEvent>(event);pb->eventFilter(target,event); switch (keyEvent->key()) { case Qt::Key_Return: change_video(); return true; break; case Qt::Key_G: if(pb->return_pb_state()) { pb->eventFilter(target,event); gb->eventFilter(target,event); } break; } } return QObject::eventFilter(target, event);
}
@Explanation
I'm initializing programbanner in the constructor as I want it to be displayed in the beginning itself. When key 'G' is pressed, control is passed to eventfilter of programbanner class where the widget is hidden (popped out) using the following code:
@
pb_ani->setDuration(500);
pb_ani->setEndValue(QPoint(this->pos().x(), widget_hidden));
pb_ani->setEasingCurve(QEasingCurve::InBack);
ProgramBannerOnScreen = false;
pb_ani->start();
@and then the genrebanner is popped in using a similar function.