Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Why can't this line be compiled? QWindow *w = qobject_cast<QWindow *>(engine->rootObjects()[0]); uint win_width = w->width();
Forum Updated to NodeBB v4.3 + New Features

Why can't this line be compiled? QWindow *w = qobject_cast<QWindow *>(engine->rootObjects()[0]); uint win_width = w->width();

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 4 Posters 398 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.
  • S Offline
    S Offline
    senmx
    wrote on last edited by
    #1

    Using cmake in CLion.

    //engine is QQmlApplicationEngine
    QWindow *w = qobject_cast<QWindow *>(engine->rootObjects()[0]);
    uint win_width = w->width();
    

    compile error:

    error: invalid use of incomplete type ‘class QWindow’
      107 |         uint win_width = w->width();
          |                           ^~
    

    No problem when using qtcreator and qmake before.

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

      Hi,

      Did you forget

      #include <QWindow>
      

      in that cpp file ?

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

      1 Reply Last reply
      4
      • S Offline
        S Offline
        senmx
        wrote on last edited by
        #3

        Yes, is indeed the problem, but why doesn't it say that QWindow cannot be found? Thanks.

        jsulmJ 1 Reply Last reply
        0
        • S senmx

          Yes, is indeed the problem, but why doesn't it say that QWindow cannot be found? Thanks.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @senmx said in Why can't this line be compiled? QWindow *w = qobject_cast<QWindow *>(engine->rootObjects()[0]); uint win_width = w->width();:

          but why doesn't it say that QWindow cannot be found?

          Well, "incomplete type" means compiler does not know what it is, but it can't know in which header this type is defined, so it can't tell you which exact header you forgot to include.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • S Offline
            S Offline
            SimonSchroeder
            wrote on last edited by
            #5

            "Incomplete type" means that QWindow is already declared as a type, but the full declaration of it cannot be found. Essentially, somewhere in the code (either your own or more likely another Qt header you already included) there is a line:

            class QWindow;
            

            This tells the compiler that QWindow is a type. But it does not say what it looks like. For this you need the full declaration of the type which can be found in the QWindow header file. So, the compiler is correct in saying "incomplete type" as it is known as type, but incomplete.

            BTW this is known as forward declaration in C++ and helps to reduce compile times when the full type does not have to be known everywhere it occurs. It can also help to reduce circular dependencies.

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved