Qt 编程点滴 初学者必看 (9)

本文介绍的是Qt 编程点滴,作为一名新手,我建议必须看一看。编程那些事,只有编程人员自己明白!所以推
首页 新闻资讯 行业资讯 Qt 编程点滴 初学者必看 (9)

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

QString怎么转换成char

复制

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

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

调用 Q_DECLARE_METATYPE  报以下错

复制

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

  • 2.

  • 3.

  • 4.

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

复制

class ContactsInfoTabItemData  {  public:    ContactsInfoTabItemData(QString name,QString caption);  };  Q_DECLARE_METATYPE(ContactsInfoTabItemData);
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

正确的写法应为:

复制

class ContactsInfoTabItemData  {  public:    ContactsInfoTabItemData(QString name=QString(),QString caption=QString());  };  Q_DECLARE_METATYPE(ContactsInfoTabItemData);
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

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

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

复制

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

产生此错误的原因:

复制

  g++ gives this message if you\\\'ve forward-declared a type, like this  class MyClass;  and then you try and access one of its members, like maybe:  MyClass::doSomething()  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.  (In C++ jargon, an "incomplete type" is a type that\\\'s been forward-declared but not yet defined.)
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

互斥用法:

复制

QMutex mutex;   void GlobalVar::setUserInfo(const GlobalVar::UserInfo &userInfo)  {      QMutexLocker locker(&mutex);      this->userinfo = userInfo;  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

自定义事件方法:

复制

a.h:   #include "event.h"   typedef void ( EventDelegater::*SetWidgetParent )(QWidget*,QString  );   class test  {  public:    Event OnSetWidgetParent;   private:    inline void invokeSetWidgetParent(QWidget *parentWidget,QString widgetName);    };   a.cpp:   inline void test::invokeSetWidgetParent(QWidget *parentWidget,QString widgetName)  {       if ( !OnSetWidgetParent.m_EventList.empty() )      {          // 循环事件列表          Event< SetWidgetParent >::EventIterator iter;          for ( iter = OnSetWidgetParent.m_EventList.begin();                  iter != OnSetWidgetParent.m_EventList.end();                  ++iter )          {              // 调用事件              InvokeEvent( parentWidget, widgetName );           }      }  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

触发事件:

复制

invokeSetWidgetParent(NULL,QString());
  • 1.

绑定事件方法:

复制

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

 
自定义宏的用法:

复制

*.pro  DEBUGSAVETOFILE = 1 isEmpty(DEBUGSAVETOFILE){      win32:debug {          CONFIG += console      }  }  else{      DEFINES += __DEBUGSAVETOFILE__  }    main.cpp:  #ifdef __DEBUGSAVETOFILE__  #pragma message( "__DEBUGSAVETOFILE__ is defined.")      qInstallMsgHandler( messageOutput );  #else  #pragma message("win32:debug is defined.")  #endif
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

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

30    2011-06-17 15:37:42    Qt