Results 1 to 9 of 9

Thread: subclassed const function problem

  1. #1

    Default subclassed const function problem

    Hi,

    i subclassed a QAbstractModel and subclassed the data function
    which is declared as const.
    However, i need to emit a signal from there and the compiler wont let me do that because of const...
    What should i do?

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: subclassed const function problem

    Simply you can't emit a signal inside a const method.

    This is because a signal is transformed by moc into a call to QMetaObject::activate() with "this" as "sender" argument

    Qt Code:
    1. void MyClass::mySignal()
    2. {
    3. QMetaObject::activate(this, &staticMetaObject, 0, 0);
    4. }
    To copy to clipboard, switch view to plain text mode 

    This method declare the first argument as "QObject* sender" and then it can't be const
    A camel can go 14 days without drink,
    I can't!!!

  3. #3

    Default Re: subclassed const function problem

    yes i know thats what the compiler tells me.
    But i NEED this signal, what should i do? create another object on which i then can call the signal from the method?

  4. #4
    Join Date
    Jun 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: subclassed const function problem

    redeclare it as a non-const in sub-class?

  5. #5

    Default Re: subclassed const function problem

    so how do i do that? just deleting "const" gives me errors related to qt.

  6. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: subclassed const function problem

    of course it does... inheritance doesn't work that way.
    what are you trying to do? there must be another way to do it.

  7. #7
    Join Date
    Jun 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: subclassed const function problem

    Qt Code:
    1. class CBase : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. CBase() : QObject(0) { someProtectedData = "something here"; };
    6. QString someConstFunction() const { return someProtectedData; };
    7. protected:
    8. QString someProtectedData;
    9. };
    10.  
    11. class CDerived : public CBase
    12. {
    13. Q_OBJECT
    14. public:
    15. QString someConstFunction() { emit someSignal(); someProtectedData = "changed"; return someProtectedData; };
    16. signals:
    17. void someSignal();
    18. };
    To copy to clipboard, switch view to plain text mode 
    Constness removed in derived class. Isn't that which you are trying to do?
    Another way is const_cast on "this".
    Code above compiles without errors for me. Am I wrong somewhere?

    qtneuling, probably you're trying to do something different.

  8. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: subclassed const function problem

    Yes, but QAbstractItemModel::data is pure virtual.

  9. #9

    Default Re: subclassed const function problem

    i need to emit this signal because my application needs to know when an item from my model was requested and which item it was

    i created a new object with a method which emits the signal, and call that from the const function. This sucks but i dont see another way.

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 17:33
  4. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 07:43
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

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.