qtdesigner的init()函数

来源:互联网 发布:c语言或者符号 编辑:程序博客网 时间:2024/05/01 18:45
I did NOT use qt designer to design the ui,I just use code to generate the myprog.cpp and myprog.h and main.cpp,

This is the important point.
quote:
NOW I wanna add init() function to make initailize some variations on running,so I generate another file called myprog.ui.h and put the follow code into it:

Whatever name you give your file, it's not important. The problem is elsewhere. Your method and declaration is fine too.
quote:
Then I compile the program and run it. THE textlabel did NOT display anything!!WHY?

Designer manual states that init() method is automatically called if present. Yes, right, but only for designer generated UI and NOT for hand generated ones.
What uic does looks like the following(edit: comments added to explain uic job):
code:

MyClass::MyClass( QObject * parent, const char * name )
: QWidget( parent, name )
{
if ( !name )
setName( "MyClass" ) ;
// Some or huge widget building stuff

// Calls the protected method for internationalization.
// LanguageChange contains all the needed tr() for all widgets.
languageChange() ;
// Final size management.
resize( QSize(303, 530).expandedTo(minimumSizeHint()) );
// Sets correct state
clearWState( WState_Polished );
// Often a huge connect list
connect( resetBtn, SIGNAL( clicked() ), this, SLOT( resetData() ) );

// Maybe some tab ordering if needed.
setTabOrder( abridged, cname );

// And now, ladies and gentlemen, just before
// finishing the constructor....
init() ;
}



When you code the hard way, the compiler cannot rely on uic to generate this kind of constructor. So you have to insert init() call in your constructor by yourself, after standard constructor methods. 
 
原创粉丝点击