t3

本文介绍了如何在Qt项目中使用Q_OBJECT宏实现信号槽机制,并展示了典型的qrc资源文件配置方法,包括图标资源的嵌入及国际化文件的引用。此外,还提到了按钮文字显示的问题及其解决办法。

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

The Q_OBJECT macro at the beginning of the class definition is necessary for all classes that define signals or slots

只有加入了Q_OBJECT,你才能使用QT中的signal和slot机制

 

一个典型的qrc文件

 

  1 <RCC>
  2     <qresource prefix="/" >
  3         <file>images/brushedmetal.png</file>
  4         <file>images/qtlogo-16.png</file>
  5         <file>images/branch-closed.png</file>
  6         <file>images/branch-open.png</file>
  7         <file>images/qphonebutton.png</file>
  8         <file>images/update.png</file>
  9         <file>images/config.png</file>
 10         <file>images/query.png</file>
 11         <file>images/live.png</file>
 12         <file>images/system.png</file>
 13         <file alias="upIcon">images/up.png</file>
 14         <file alias="downIcon">images/down.png</file>
 15         <file alias="rightIcon">images/right.png</file>
 16         <file alias="leftIcon">images/left.png</file>
 17     <file>translations/catalog_ch.qm</file>
 18     </qresource>
 19 </RCC>

 

而我在代码中使用为liveButton->setIcon(QIcon(":/images/live.png"));

应该在pro文件里加入 RESOURCES += catalog.qrc
 嵌入资源的好处是把这些图片也编辑为代码了,可以创建真正独立的可执行文件

 

 

代码中按钮的文字没有显示,怀疑是不能显示中文的原因,于是把代码

button = new QPushButton(tr("Seat"));

改为button = new QPushButton("Seat");

可以显示正常了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值