Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Change some code in ui_xxx.h
Forum Update on Monday, May 27th 2025

Change some code in ui_xxx.h

Scheduled Pinned Locked Moved C++ Gurus
12 Posts 5 Posters 4.5k Views
  • 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.
  • I Offline
    I Offline
    ibingow
    wrote on last edited by
    #1

    In ui_myclass.h we can find these lines:
    @namespace UI {
    class MyClass : public Ui_MyClass {}
    } // namespace Ui@

    I think we can have a better way to do this. That's my code:
    @namespace UI {
    typedef Ui_MyClass MyClass;
    } // namespace Ui@

    Both of them do the same things. But the former will call one more constructor than the latter.
    I don't know why qt doesn't use the latter. It's much easier.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      I hope you know, that ui_xxx.h is recreated by UIC?
      It dioes not make sense to change in that file.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • I Offline
        I Offline
        ibingow
        wrote on last edited by
        #3

        of cause i know. i hope uic can generate my code in the future :) i think mine is better
        [quote author="Gerolf" date="1301459734"]I hope you know, that ui_xxx.h is recreated by UIC?
        It dioes not make sense to change in that file.[/quote]

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          In that case, I suggest you write a patch for uic, and submit a merge request for it.

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jorj
            wrote on last edited by
            #5

            It would mean you cannot do a

            @class MyClass;@

            definition somewhere in a header, and I imagine there would be no extra constructors called once the app was compiled in release... but apart from that, it is neater.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              giesbert
              wrote on last edited by
              #6

              Hi Jori,

              you just did a predefine, not a class definition.
              What UIC does is really creating a class.

              @
              class MyClass : public Ui_MyClass {}
              @

              is different from

              @
              class MyClass;
              @

              In your case, the class must be defined somewhere else.

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                A forward declaration of a class (to avoid #including the header) is not possible with a typedef.

                In a header:

                @
                class Foo {
                public:
                Foo() { }
                ~Foo() { }

                int foobar;
                

                };

                typedef Foo FooAlias;
                @

                and later on in another header:

                @
                class FooAlias;

                class MainWindow : public QMainWindow
                {
                Q_OBJECT

                public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();

                private:
                FooAlias *f;
                };
                @

                and in the implementation of the MainWindow:

                @
                #include "mainwindow.h"
                #include "foo.h"
                @

                If you try to compile the .cpp of the MainWindow class you get an error message like this:

                @
                foo.h:9: error: conflicting declaration 'typedef class Foo FooAlias'
                mainwindow.h:1: error: 'struct FooAlias' has a previous declaration as 'struct FooAlias'
                @

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jorj
                  wrote on last edited by
                  #8

                  Gerolf, I was saying what Volker said, but possibly less eloquently! ;)

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    ibingow
                    wrote on last edited by
                    #9

                    i test it with gcc 3.3. Forward declaration are supported.

                    [quote author="Volker" date="1301482447"]A forward declaration of a class (to avoid #including the header) is not possible with a typedef.

                    In a header:

                    @
                    class Foo {
                    public:
                    Foo() { }
                    ~Foo() { }

                    int foobar;
                    

                    };

                    typedef Foo FooAlias;
                    @

                    and later on in another header:

                    @
                    class FooAlias;

                    class MainWindow : public QMainWindow
                    {
                    Q_OBJECT

                    public:
                    explicit MainWindow(QWidget *parent = 0);
                    ~MainWindow();

                    private:
                    FooAlias *f;
                    };
                    @

                    and in the implementation of the MainWindow:

                    @
                    #include "mainwindow.h"
                    #include "foo.h"
                    @

                    If you try to compile the .cpp of the MainWindow class you get an error message like this:

                    @
                    foo.h:9: error: conflicting declaration 'typedef class Foo FooAlias'
                    mainwindow.h:1: error: 'struct FooAlias' has a previous declaration as 'struct FooAlias'
                    @[/quote]

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      [quote author="ibingow" date="1303381454"]i test it with gcc 3.3. Forward declaration are supported.
                      [/quote]

                      Forward declaration in general is supported, yes. But not the construct mentioned above.

                      You can try yourself, this is a complete example:

                      ------ foo.h ------
                      @
                      #ifndef FOO_H
                      #define FOO_H

                      class Foo {
                      public:
                      Foo() { }
                      ~Foo() { }

                      int foobar;
                      

                      };

                      typedef Foo FooAlias;

                      #endif // FOO_H
                      @

                      ------ using.h ------
                      @
                      #ifndef USING_H
                      #define USING_H

                      class FooAlias;

                      class UsingFoo
                      {

                      public:
                      explicit UsingFoo();
                      ~UsingFoo();

                      private:
                      FooAlias *f;
                      };

                      #endif // USING_H
                      @

                      ------ using.cpp ------
                      @
                      #include "using.h"
                      #include "foo.h"

                      UsingFoo::UsingFoo()
                      {
                      f = new FooAlias;
                      }

                      UsingFoo::~UsingFoo()
                      {

                      }
                      @

                      ------ main.cpp ------
                      @
                      #include "using.h"

                      int main(int argc, char *argv[])
                      {
                      UsingFoo uf;
                      return 0;
                      }
                      @

                      Compile with

                      @

                      g++ -c -o main.o main.cpp
                      g++ -c -o using.o using.cpp
                      g++ -o typedeftest main.o using.o
                      @

                      It definitely fails with gcc 4.2.x and 3.4.x, all with the very same error message I posted earlier.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0
                      • I Offline
                        I Offline
                        ibingow
                        wrote on last edited by
                        #11

                        Yes, you're right. But it's strange that my gcc 3.3.1 can compile your example without warning and error.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          [quote author="ibingow" date="1309876061"]Yes, you're right. But it's strange that my gcc 3.3.1 can compile your example without warning and error. In fact, in your example, we can write UsingFoo like that
                          [/quote]

                          I just don't believe you. My code fails with gcc 3.2.2 with the same error messages. It is very unlikely that it failed on 3.2.x, compiled on 3.3.x and failed again on 3.4.x. You must have modified the code or tweaked some compiler switches.

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          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