Change some code in ui_xxx.h



  • 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.



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



  • 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]



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



  • 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.



  • 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.



  • 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'
    @



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



  • 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]



  • [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.



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



  • [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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.