Results 1 to 13 of 13

Thread: Qt unable to add widget dynamically in clicked slot

  1. #1
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Qt unable to add widget dynamically in clicked slot

    Here is my situation. I have a QFrame, inside which I have a QPushButton. When I click on the button, it invokes a slot, where I eventually find out which button was clicked, from that its parent widget (which here is the QFrame), and then add some other widget to it. This is what I have so far:

    Qt Code:
    1. QFrame *base = new QFrame(parent);
    2. base->setGeometry(50,50,160,30);
    3. base->setStyleSheet("background: #ffffff; border-radius: 5px;");
    4. base->setCursor(Qt::PointingHandCursor);
    5.  
    6. QPushButton *button = new QPushButton("Some text",base);
    7. button->setGeometry(0,0,160,30);
    8. button->setCheckable(false);
    9. button->setStyleSheet("background: transparent; border-radius: 5px; padding-left: 9px; text-align: left; ");
    10. connect(button,SIGNAL(clicked()),this,SLOT(myslot()));
    11.  
    12. void MyClass::myslot()
    13. {
    14. QObject* pObject = sender();
    15. QPushButton *button = qobject_cast<QPushButton* >(pObject);
    16. QFrame *container = qobject_cast<QFrame* >(button->parentWidget());
    17. int x = container->rect().height();
    18.  
    19. qDebug() << "Height : " << x << endl; //prints correct value
    20. qDebug() << "Text: " << button->text() << endl; // prints correctly as well
    21. button->setVisible(false); //this works
    22.  
    23. QFrame *newWidget = new QFrame(container);
    24. newWidget->setGeometry(0,0,160,150);
    25. newWidget->setStyleSheet("background: #ffffff; border-radius: 5px;");
    26. newWidget->setCursor(Qt::PointingHandCursor); //doesn't work
    27. container->stackUnder(newWidget);
    28. }
    To copy to clipboard, switch view to plain text mode 

    As you see, I ma getting the parent container QFrame and the button all right, as evident from getting the correct values for them. But the child widget which I am now adding doesn't show up at all. What am I doing wrong, and how do I fix this?
    Last edited by Cupidvogel; 25th February 2015 at 18:18.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt unable to add widget dynamically in clicked slot

    Where would it "show up"? Do container and newWidget have the same parent? If not, stackUnder() will not do anything.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Qt unable to add widget dynamically in clicked slot

    Umm, no, newWidget is supposed to belong to container. The sender is the button, from which I get the parent (named 'container' in slot, 'base ' where it was defined). So newWidget should ideally be appended to the 'base' widget. Essentially the button and the newWidget both share the same parent - base, which we get in the slot as container.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt unable to add widget dynamically in clicked slot

    Quote Originally Posted by Cupidvogel View Post
    Umm, no, newWidget is supposed to belong to container.
    So "container" and not "this" (as you had in your original code) should be its parent.

    The sender is the button, from which I get the parent (named 'container'
    Why don't you just store container as a pointer in your class? You are risking that if whenever you (or someone else) choose to change the widget hierarchy, your code will break.

    So newWidget should ideally be appended to the 'base' widget.
    So "base" or "container"?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Qt unable to add widget dynamically in clicked slot

    'Base' and 'container' are same, right? First I name it as base while constructing, then in the slot, when I obtain it by referring to the sender's parent, I name it container. So the hierarchy is Container > Button. I want to append newWidget to base (renamed to container in slot), so that will make newWidget and button siblings, and children of base (container).

    I can't store container as a pointer. There will be lots of elements of that type in my application.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt unable to add widget dynamically in clicked slot

    Quote Originally Posted by Cupidvogel View Post
    'Base' and 'container' are same, right? First I name it as base while constructing, then in the slot, when I obtain it by referring to the sender's parent, I name it container. So the hierarchy is Container > Button. I want to append newWidget to base (renamed to container in slot), so that will make newWidget and button siblings, and children of base (container).
    Ok.

    1. Container should have a layout unless you want to position all the items manually
    2. Create the widget you want to add without a parent
    3. Add the widget to the layout mentioned in (1). QWidget::layout() returns a widget's layout which you can downcast to the proper type if you need.

    I can't store container as a pointer. There will be lots of elements of that type in my application.
    Sure you can. You should have proper encapsulation anyway instead of reaching with your right hand to your left ear under your left leg.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Qt unable to add widget dynamically in clicked slot

    I will try your solution. But why isn't mine working?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt unable to add widget dynamically in clicked slot

    One obvious thing is the stackUnder() call which doesn't make sense. But it shouldn't be breaking things. Or are you simply missing a show() call to the new widget? You have it there somewhere, don't you?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Qt unable to add widget dynamically in clicked slot

    Yes, I doubted that stackUnder might be causing problems (though like you said should not break things), so I commented it out. Same problem still. I didn't use show anywhere. Why do I need to show the widget explicitly if I already assigned a parent while constructing it?

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt unable to add widget dynamically in clicked slot

    Quote Originally Posted by Cupidvogel View Post
    I didn't use show anywhere.
    And that's why you don't see the widget

    Why do I need to show the widget explicitly if I already assigned a parent while constructing it?
    Because visibility is a three state mechanism. The docs on QWidget::setVisible() should explain that. If not then you can come back here and ask.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Qt unable to add widget dynamically in clicked slot

    Hmmmph. Works. My bad. On a kinda unrelated note, imagine that the new widget has to exactly cover the original widget (what we defined as base). Now in the slot, we get the button as sender, from it we get the parent widget through parentWidget(), from there we call ->geometry().topLeft().x() to get the x coordinate of the base frame. And width, height and y in the same way. Then I use these values to set the geometry of the new widget so that it has the same dimensions and position as the base widget and thus covers it up.

    Question is, will it ever fail? Can the topLeft().x() value be different in the slot compared to what I originally set , due to some layout nuances?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Qt unable to add widget dynamically in clicked slot

    Quote Originally Posted by Cupidvogel View Post
    Now in the slot, we get the button as sender, from it we get the parent widget through parentWidget(), from there we call ->geometry().topLeft().x() to get the x coordinate of the base frame.
    You got me completely lost. For a widget only its direct parent matters.

    Question is, will it ever fail? Can the topLeft().x() value be different in the slot compared to what I originally set , due to some layout nuances?
    You should (almost) never be setting geometry manually. That's what you have layouts for - give your widget a vertical or horizontal layout, set its margins to 0 and add a child widget to the layout. Done and always works.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    May 2014
    Posts
    136
    Thanks
    72
    Qt products
    Qt3 Qt4 PyQt3 PyQt4
    Platforms
    MacOS X Windows

    Default Re: Qt unable to add widget dynamically in clicked slot

    Good idea. Will do.

Similar Threads

  1. Replies: 12
    Last Post: 17th June 2013, 21:20
  2. use value from dynamically created widget in a slot
    By Cyrebo in forum Qt Programming
    Replies: 55
    Last Post: 23rd April 2013, 14:51
  3. Replies: 10
    Last Post: 8th January 2013, 20:30
  4. How to connect a slot(QWidget*) with signal(clicked())
    By revellix in forum Qt Programming
    Replies: 10
    Last Post: 5th August 2011, 16:19
  5. Replies: 6
    Last Post: 5th December 2007, 22:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.