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. Is it recommended to include each widget explicitly?
Qt 6.11 is out! See what's new in the release blog

Is it recommended to include each widget explicitly?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 990 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.
  • A Offline
    A Offline
    Aaron Kim
    wrote on last edited by
    #1

    This might be about a practice, but I want to know how experts consider this issue.
    Basically, all the base widgets are included together if I include a derived widget (for example, QFrame and QWidget are included together if I include QLabel)
    And there is an example like below.
    If included QWidget-derived widgets are used only in the findChild syntaxes,

    this->findChild<QLineEdit*>("blahblah");
    this->findChild<QLabel*>("blahblah2");
    this->findChild<QPushButton*>("blahblah3");
    

    They all can be replaced with

    this->findChild<QWidget*>("blahblah");
    this->findChild<QWidget*>("blahblah2");
    this->findChild<QWidget*>("blahblah3");
    

    In this case, including all the QLineEdit, QLabel, QPushButton is redundant.

    On the contrary, if QLabel header was already included and if I have to find a QWidget like below,

    this->findChild<QWidget*>("blahblah");
    

    There is no need to include QWidget because it was included already when QLabel was included.

    As far as I know, there is no performance issue for this, but I want to know what would be a better practice.

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

      Hi,

      The best practice recommends to include everything that you are using and only what you are using. Don't rely on indirect includes.

      In your case, you are only using QWidget based classes and it's likely also the case where this code is called, so you already include QWidget in your class header, so including it again in the implementation would be redundant.

      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
      3
      • BuckwheatB Offline
        BuckwheatB Offline
        Buckwheat
        wrote on last edited by
        #3

        Hi @Aaron-Kim ,

        @SGaist is correct. Include only what you need. I actually take this one step farther by using forward declarations in my header and use reference and pointer in variables. This has two effects:

        1. Keep header lightweight and fast to compile because it doesn't include tons of stuff.
        2. Keeps the work in the CPP file.

        And, first and foremost, the usage depends on what your design intends to do.

        Dave Fileccia

        A 1 Reply Last reply
        3
        • BuckwheatB Buckwheat

          Hi @Aaron-Kim ,

          @SGaist is correct. Include only what you need. I actually take this one step farther by using forward declarations in my header and use reference and pointer in variables. This has two effects:

          1. Keep header lightweight and fast to compile because it doesn't include tons of stuff.
          2. Keeps the work in the CPP file.

          And, first and foremost, the usage depends on what your design intends to do.

          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @Buckwheat I do the same thing. Forward declarations and only including exactly what I need.

          @Aaron-Kim Implicit declarations are bound to happen unfortunately. And they do indeed cause issues when they are changed in the code that is including them. I have had to deal with these problems numerous times.

          Plan as best as you can and then just realize you will need to update your code to keep it current and building with new Qt's in the future. I've had projects that are only a few years old that have so many changes building them now would probably take me a day or 2 of time to "fix" the builds for newer Qt. In one case I was using a webkit that Qt didn't have any more. That one I just wrote off as a lost cause.

          All that is a good reason for build servers and CI. That way you know right away when builds are broken by a dependency and can be fixed with minimal effort. Saving it all for later will definitely bite you. :)

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Aaron Kim
            wrote on last edited by Aaron Kim
            #5

            Thanks for everyone for reply! I thought that forward declaration of QWidget-based headers is not a good practice, but I was wrong.

            1 Reply Last reply
            1

            • Login

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