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. Help with multiple children widgets having the same parent
Qt 6.11 is out! See what's new in the release blog

Help with multiple children widgets having the same parent

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 4.8k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Well... Connect the first animations finished signal to the second animations start slot and that should do it :)

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        If you want to show a widget that has no parent, you have to at least call show()

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          b1gsnak3

          I'll give that a try and keep you posted. Thanks :)

          SGaist

          Both widgets have the same parent and they work fine when I call them from the constructor but don't show up when called from some other place.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              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).

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                @
                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.

                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