qtdesigner的init()函数

本文探讨了在未使用 Qt Designer 设计 UI 的情况下,如何添加 init() 函数进行初始化操作。虽然 Qt Designer 的文档指出 init() 方法会自动调用,但仅适用于由 Designer 生成的 UI,不适用于手动编写的代码。解决方案是在手动编写的构造函数中显式调用 init() 函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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. 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值