Results 1 to 13 of 13

Thread: Qt unable to add widget dynamically in clicked slot

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.