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. How to dynamically add button and assign action to it in child class

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.3k 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.
  • B Offline
    B Offline
    banyudu
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      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?

      Greetz, Jeroen

      1 Reply Last reply
      0
      • B Offline
        B Offline
        banyudu
        wrote on last edited by
        #3

        All children have the same Parent Class(BaseClass)

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          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.

          Greetz, Jeroen

          1 Reply Last reply
          0
          • B Offline
            B Offline
            banyudu
            wrote on last edited by
            #5

            It's solved, thank you!

            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