Qt 编程点滴 初学者必看 (9)
创始人
2024-08-01 02:31:02
0

Qt 编程继续为大家讲解,还是接着文章 Qt 编程点滴 初学者必看 (8) ,继续介绍,说编程那些细节。由于本话题是一节一节为大家介绍的,所以更多内容请看末尾编辑推荐。

QString怎么转换成char

  1. QString str ="123456";  
  2. str.toAscii().data(); //this return a char* or const char*  
  3. str.toAscii() return a QByteArray  
  4.  
  5. QString Str; //Str = "asdfasdfasdf";  
  6. Str->toString().c_str(); 

调用 Q_DECLARE_METATYPE  报以下错

  1. ..\..\..\..\Qt\src\corelib\kernel\qmetatype.h||In function \\\'void* qMetaTypeConstructHelper(const T*) [with T = ContactsInfoTabItemData]\\\':|  
  2. ..\..\..\..\Qt\src\corelib\kernel\qmetatype.h|152|instantiated from \\\'int qRegisterMetaType(const char*, T*) [with T = ContactsInfoTabItemData]\\\'|  
  3. src\contactsinfotabitemdata.h|62|instantiated from here|  
  4. ..\..\..\..\Qt\src\corelib\kernel\qmetatype.h|126|error: no matching function for call to \\\'ContactsInfoTabItemData::ContactsInfoTabItemData()\\\'| 

如果报以上相类似的错误,请对构造函数中的每个参数赋初值,下面的写法是错误的

  1. class ContactsInfoTabItemData  
  2. {  
  3. public:  
  4.   ContactsInfoTabItemData(QString name,QString caption);  
  5. };  
  6. Q_DECLARE_METATYPE(ContactsInfoTabItemData); 

正确的写法应为:

  1. class ContactsInfoTabItemData  
  2. {  
  3. public:  
  4.   ContactsInfoTabItemData(QString name=QString(),QString caption=QString());  
  5. };  
  6. Q_DECLARE_METATYPE(ContactsInfoTabItemData); 

如果程序莫名奇妙的退出,也不报DLL找不到的错误,请仔细检查Main函数体有没直接Return的语句,以造成不提示,直接退出的错误;

在Qt中计算文本的宽度与高度  ( http://www.cuteqt.com/blog/?p=1029 )

  1. error: incomplete type %u2018nsIDOMComment%u2019 used in nested name specifier 

产生此错误的原因:

  1.   g++ gives this message if you\\\'ve forward-declared a type, like this  
  2. class MyClass;  
  3. and then you try and access one of its members, like maybe:  
  4. MyClass::doSomething()  
  5. g++ is complaining that it hasn\\\'t seen the body of class MyClass yet, so it has no way to know what MyClass::doSomething is.  
  6. (In C++ jargon, an "incomplete type" is a type that\\\'s been forward-declared but not yet defined.) 

互斥用法:

  1. QMutex mutex;  
  2.  
  3. void GlobalVar::setUserInfo(const GlobalVar::UserInfo &userInfo)  
  4. {  
  5.     QMutexLocker locker(&mutex);  
  6.     this->userinfo = userInfo;  

自定义事件方法:

  1. a.h:  
  2.  
  3. #include "event.h"  
  4.  
  5. typedef void ( EventDelegater::*SetWidgetParent )(QWidget*,QString  );  
  6.  
  7. class test  
  8. {  
  9. public:  
  10.   Event OnSetWidgetParent;  
  11.  
  12. private:  
  13.   inline void invokeSetWidgetParent(QWidget *parentWidget,QString widgetName);    
  14. };  
  15.  
  16. a.cpp:  
  17.  
  18. inline void test::invokeSetWidgetParent(QWidget *parentWidget,QString widgetName)  
  19. {  
  20.  
  21.     if ( !OnSetWidgetParent.m_EventList.empty() )  
  22.     {  
  23.         // 循环事件列表  
  24.         Event< SetWidgetParent >::EventIterator iter;  
  25.         for ( iter = OnSetWidgetParent.m_EventList.begin();  
  26.                 iter != OnSetWidgetParent.m_EventList.end();  
  27.                 ++iter )  
  28.         {  
  29.             // 调用事件  
  30.             InvokeEvent( parentWidget, widgetName );  
  31.  
  32.         }  
  33.     }  

触发事件:

  1. invokeSetWidgetParent(NULL,QString());   

绑定事件方法:

  1. test->OnSetWidgetParent.Bind(this, &MainWindow::setWidgetParent);   

 
自定义宏的用法:

  1. *.pro  
  2. DEBUGSAVETOFILE = 1 
  3. isEmpty(DEBUGSAVETOFILE){  
  4.     win32:debug {  
  5.         CONFIG += console  
  6.     }  
  7. }  
  8. else{  
  9.     DEFINES += __DEBUGSAVETOFILE__  
  10. }    
  11. main.cpp:  
  12. #ifdef __DEBUGSAVETOFILE__  
  13. #pragma message( "__DEBUGSAVETOFILE__ is defined.")  
  14.     qInstallMsgHandler( messageOutput );  
  15. #else  
  16. #pragma message("win32:debug is defined.")  
  17. #endif 

小结:本文主要介绍了在Qt 事件的使用,通过Qt 编程点滴介绍,也给自己提高了编程过程中需要注意的细节问题,由于本话题是一节一节为大家展现的,所以更多内容,请看编辑推荐。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...