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. trouble using qobject_cast (MOC not seeing Q_OBJECT)
Forum Updated to NodeBB v4.3 + New Features

trouble using qobject_cast (MOC not seeing Q_OBJECT)

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 1.7k 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.
  • devDawgD Offline
    devDawgD Offline
    devDawg
    wrote on last edited by
    #1

    Hi Everyone,

    I've got a class that I am defining as a subclass of QAbstractProxyModel, which I will be instantiating inside of QML.

    Here's some info I pulled from DataFilter.h:

    class DataFilter: public QAbstractProxyModel {
    
        Q_OBJECT
     Q_PROPERTY(QObject* source READ getSource WRITE setSourceModel NOTIFY sourceChanged)
    explicit DataFilter(QObject * parent = nullptr);
    

    And here are my setter and constructor definitions for reference:

    DataFilter::DataFilter(QObject *parent):
        QAbstractProxyModel(parent),
        choice(-1),
        source(nullptr),
        mOutputs()
        
    { }
    
    void DataFilter::setSourceModel(QObject *model)
    {
    
        QObject * tester = qobject_cast<DataModel*>(model);
        if (tester) {
            source = qobject_cast<DataModel&>(model);
        }
        else return;
    
    }
    

    This setter is what I am struggling with. BOTH DataFilter and DataModel have been defined with Q_OBJECT (I've been down this road before, so I know the errors), but for some reason, the moc is still not happy. My reason for this approach is to avoid failed casts.

    /home/ghguest/Qt5.9.3/5.9.3/gcc_64/include/QtCore/qglobal.h:738: error: static assertion failed: qobject_cast requires the type to have a Q_OBJECT macro #define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
    ^
    On this same thought, the source member variable I am setting here is a reference (DataModel& source;). I am unsure if I am choosing the right cast target.

    Overall, I am very confused by this. MOC is not happy, even though I've defined the target of the cast with Q_OBJECT. Here is a code chunk for you non-believers:

    class DataModel: public QAbstractListModel {
    
    Q_OBJECT
        explicit DataModel(const DataModel &model);
    
        explicit DataModel(DataModel &&);
    
        DataModel& operator=(const DataModel &);
    
        DataModel& operator=(DataModel &&);
    

    As you can see, I've also defined my copy & move constructors/assignment operators.

    Any ideas of things to try would be greatly appreciated.

    Thanks in advance!

    Anything worthwhile is never achieved easily.

    1 Reply Last reply
    0
    • devDawgD Offline
      devDawgD Offline
      devDawg
      wrote on last edited by
      #2

      I have solved the issue, and the result makes sense. First I'm going to reveal what I changed to complete an error-free build, then I'm going to explain why this makes sense.

      I fixed my error by changing source; instead of storing a reference, I am now storing a pointer.
      Meaning I went from this:

      DataModel &source;
      

      to this:

      DataModel *source;
      

      When a person stores a reference, they are storing the literal address in memory of that object. Now, because the address of the object in memory is probably tied to a bunch of other locations in memory, it is a time-costly and undesirable operation to copy by reference, because that would include making all the necessary ties to that new memory location.

      Storing a pointer completely avoids the heavy operation of copying all of the arguments data. So instead of copying the object's address and mimicking all of its ties, you just pass a pointer that points to that object's address in memory.

      I hope I was clear and also correct. C++ is a relatively new language to me, but I have done my fair share of reading about move and copy semantics.

      If I have made an error in what I've said here, PLEASE tell me! Eager learner here.

      Anything worthwhile is never achieved easily.

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        There's one main major point you missed: you can not copy QObject based classes.

        See the QObject documentation on the why.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        devDawgD 1 Reply Last reply
        3
        • SGaistS SGaist

          Hi,

          There's one main major point you missed: you can not copy QObject based classes.

          See the QObject documentation on the why.

          devDawgD Offline
          devDawgD Offline
          devDawg
          wrote on last edited by
          #4

          @SGaist Makes total sense.

          Thanks man.

          Anything worthwhile is never achieved easily.

          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