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 access ?
Qt 6.11 is out! See what's new in the release blog

How to access ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 932 Views 2 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    Can anyone help me access a pointer in a class?

    I have a bunch of classes derived from Qt widget classes, for example I have a class called clsQtRadioButton which is derived from QRadioButton:

        class clsQtRadioButton : public QRadioButton {
        Q_OBJECT
            friend class clsXMLnode;
    

    I am adding an instance of this class to a layout using another of my functions:

    void clsQtLayout::addButton(QAbstractButton* pobjButton, const QString& crstrGroup) {
    

    I can see in the debugger that pobjButton has visibility over a protected member mpobjNode this is common to all my derived classes and is defined as private in the class. I have an access method pobjGetNode, however I cannot call this from pobjButton. Is there anyway to access mpobjNode from pobjButton ?

    I thought I could add clsXMLnode as one of the parent classes however because this class is also derived from Q_OBJECT I cannot do that.

    SPlattenS 1 Reply Last reply
    0
    • M Offline
      M Offline
      mchinand
      wrote on last edited by mchinand
      #3

      Use qobject_cast and check if the cast worked (the result is not null). Also, don't expect a cast to work if it's not derived from the type you are casting to (i.e., just adding a class as friend should not make it work). Adding a class as a friend should work for what you are trying do, you may have to cast the object to the type that it's a friend of first. For example, if the object is coming in as a QAbstractButton but was created as your custom button type, cast it to your type first, then try to access private methods from an object that is of the friend type.

      SPlattenS 1 Reply Last reply
      1
      • SPlattenS SPlatten

        Can anyone help me access a pointer in a class?

        I have a bunch of classes derived from Qt widget classes, for example I have a class called clsQtRadioButton which is derived from QRadioButton:

            class clsQtRadioButton : public QRadioButton {
            Q_OBJECT
                friend class clsXMLnode;
        

        I am adding an instance of this class to a layout using another of my functions:

        void clsQtLayout::addButton(QAbstractButton* pobjButton, const QString& crstrGroup) {
        

        I can see in the debugger that pobjButton has visibility over a protected member mpobjNode this is common to all my derived classes and is defined as private in the class. I have an access method pobjGetNode, however I cannot call this from pobjButton. Is there anyway to access mpobjNode from pobjButton ?

        I thought I could add clsXMLnode as one of the parent classes however because this class is also derived from Q_OBJECT I cannot do that.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #2

        @SPlatten , wow, found something and tried, it works:

        clsXMLnode* pobjNode(reinterpret_cast<clsXMLnode*>(pobjButton));
        

        [edit] However strange results although I can now access and call methods, the results are not right, in the debugger I can see the contents of the class which are correct but stepping through its not working. For example in one of the clsXMLnode methods:

        QString clsXMLnode::strGetAttribute(QString strAttr) {
        //Does the attribute contain an object Delmtr?
            clsXMLnode* pobjNode(this);
            QString strData;
            if ( mlstAttrPrs.length() > 0 ) {
        

        In the debugger I can see mlstAttrPrs contains 6 items:
        Screenshot 2021-11-30 at 17.40.45.png
        However it will not step into the if condition and skips right over it ???

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mchinand
          wrote on last edited by mchinand
          #3

          Use qobject_cast and check if the cast worked (the result is not null). Also, don't expect a cast to work if it's not derived from the type you are casting to (i.e., just adding a class as friend should not make it work). Adding a class as a friend should work for what you are trying do, you may have to cast the object to the type that it's a friend of first. For example, if the object is coming in as a QAbstractButton but was created as your custom button type, cast it to your type first, then try to access private methods from an object that is of the friend type.

          SPlattenS 1 Reply Last reply
          1
          • M mchinand

            Use qobject_cast and check if the cast worked (the result is not null). Also, don't expect a cast to work if it's not derived from the type you are casting to (i.e., just adding a class as friend should not make it work). Adding a class as a friend should work for what you are trying do, you may have to cast the object to the type that it's a friend of first. For example, if the object is coming in as a QAbstractButton but was created as your custom button type, cast it to your type first, then try to access private methods from an object that is of the friend type.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #4
            This post is deleted!
            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