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

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

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

删ITEM方法:

把把ITEM的数据挂到指针上,先删ITEM,然后再删除指针

如果发生 no such file or directory not find(报Qt核心文件错)

有可能是project --properties--projects settings中的"This is a custom MakeFile"没有勾选;

检查.pro文件是   INCLUDEPATH += DEPENDPATH+= 有没加入文件所在的目录

检查.pro文件是否引入两个版本不同的相同文件名的文件;

枚举类型做为信号的参数,则需对枚举类型进行注册

在include中

复制

//定义Enum  typedef enum{      ProgressType,      StartType,      SuccessType,      StopType  }  SyncMsgType;    //定义结构  typedef struct  //实际使用中可以多增加些结构成员  {      SyncMsgType msgtype;  }SyncMsg;  Q_DECLARE_METATYPE(SyncMsg)
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

在应用程序.CPP中

复制

//连接之前再注册      qRegisterMetaType("SyncMsg");      connect(gpssyncthread, SIGNAL(syncMsgNotify(SyncMsg)),              this, SLOT(syncMsgEvent(SyncMsg)));    QList listItemDatas;   for (QList::iterator it=listItemDatas.begin(); it!=listItemDatas.end() ; ++it)      {          (*it)->colName;      }  error: multiple types in one declaration
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

自定义的类 {}后面没有";"

还有一种可能是pro文件中引用了两次单元文件;

expected unqualified-id before "int"前一句的";"误写为","

在Bulid工程时,qmake *.pro死循环,原因:pro文件里同一文件包含两次;

char *const p ; p所指向的值不能变;

char cont *p; P所指向的地址不能变;

error: `nameLineEdt\\\' was not declared in this scope 函数域没有写; (函数域::函数名())ifdef/define重覆

复制

int main(int argc, char *argv[])  {      Q_INIT_RESOURCE(qtdam);            QApplication app(argc, argv);      QSplashScreen *splash = new QSplashScreen;      QString path=app.applicationDirPath();      IDIOMA *lang = new IDIOMA();      lang->setfile_idioma(path+"/languages.lng");      if (lang->idioma_seleccionado=="Español")          splash->setPixmap(QPixmap(":/images/splash_espagnol.png"));      else          splash->setPixmap(QPixmap(":/images/splash.png"));      splash->show();      Qt::Alignment topRight = Qt::AlignRight | Qt::AlignTop;      splash->showMessage(lang->leer_idioma("1"),topRight, Qt::white);      MainWindow mainWin;      mainWin.show();      splash->finish(&mainWin);      delete splash;      return app.exec();  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

函数如果有返回值必须写,否则有造成一些不确定的错误,如:

复制

QString a()  {  }   QString str;  str = "abc";  str.append(a());  QMessageBox::warning(this, tr("呼叫"),str,QMessageBox::Ok);
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

上面的情况,对话框可以出来,但点击对话框中的"确定"后,程序会死在那;

进行信号连接时,要确保连接参数中的对象已经创建过,否则会报保护错;

图片加载不了,有可能是Qt库中的插件库没有拷贝;

加载路径指令:

复制

QCoreApplication::addLibraryPath(QObject::tr("%1%2plugins").arg(QCoreApplication::applicationDirPath()).arg("/"));
  • 1.

qDebug() << "插件加载的路径是(QCoreApplication::libraryPaths):" << QCoreApplication::libraryPaths()<        

有三个插件加载路径 1,应用程序路径;2,QTDIR环境路径,3,加入的路径;     

复制

TRACE_LEVEL=5 TRACE_SUBSYS=DB /d/study/umpcapp/umpcapp-dev-1.0.0/debug/gpsapp.exe     void DragWidget::mousePressEvent(QMouseEvent *event)   {       QLabel *child = static_cast(childAt(event->pos()));       if (!child)           return;        QPixmap pixmap = *child->pixmap();        QByteArray itemData;       QDataStream dataStream(&itemData, QIODevice::WriteOnly);       dataStream << pixmap << QPoint(event->pos() - child->pos());
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

 
取得应用程序所在路径,注:结果后面未加"/"

复制

QCoreApplication::applicationDirPath()
  • 1.

*.hpp文件,如果改动,Bulid后对改动后代码不起作用,必须ReBulid才可以;

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

18    2011-06-17 14:29:55    Qt