How to dynamically add button and assign action to it in child class



  • I have a BaseClass and many ChildClasses in my project.
    The BaseClass provided a method to dynamically add button :
    @void BaseClass::registerButton(const QString & caption, QAction * action)
    {
    QPushButton * btn = new QPushButton(m_oButtonArea); // m_oButtonArea is a QWidget
    btn->addAction(action);
    btn->setText(caption);
    }@

    ChildClass will use this function to add a button to the main frame

    What I expected is that once the button is pressed, the function(ChildClass's non-static member function) connected to the action will be invoked. But it just didn't work.

    Any ideas would be appreciated



  • I'm guessing, but don't you mean parent class and child classes. Base class is something totally different.
    Or do all the children have the same Base class?



  • All children have the same Parent Class(BaseClass)



  • Hi,
    Oh, then it's not a base class. A base class is a class that all children inherit. A base class is not the parent class.

    So, when all children need access to the parent class, there are two ways to go. Either open up the parent class to the outside world (making stuff public), but I wouldn't recommend it. Or you should utilize the QObject parent-> pointer in your children to access parent members functions.
    Then you still need to connect the action to a slot in your child class.



  • It's solved, thank you!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.