Qt 編程點(diǎn)滴 初學(xué)者必看 (2)
Qt 編程繼續(xù)為大家講解,還是接著文章 Qt 編程點(diǎn)滴 初學(xué)者必看 (1) ,繼續(xù)介紹,說編程那些細(xì)節(jié)。由于本話題是一節(jié)一節(jié)為大家介紹的,所以更多內(nèi)容請(qǐng)看末尾編輯推薦。
刪ITEM方法:
把把ITEM的數(shù)據(jù)掛到指針上,先刪ITEM,然后再刪除指針
如果發(fā)生 no such file or directory not find(報(bào)Qt核心文件錯(cuò))
有可能是project --properties--projects settings中的"This is a custom MakeFile"沒有勾選;
檢查.pro文件是 INCLUDEPATH += DEPENDPATH+= 有沒加入文件所在的目錄
檢查.pro文件是否引入兩個(gè)版本不同的相同文件名的文件;
枚舉類型做為信號(hào)的參數(shù),則需對(duì)枚舉類型進(jìn)行注冊(cè)
在include中
- //定義Enum
- typedef enum{
- ProgressType,
- StartType,
- SuccessType,
- StopType
- }
- SyncMsgType; //定義結(jié)構(gòu)
- typedef struct //實(shí)際使用中可以多增加些結(jié)構(gòu)成員
- {
- SyncMsgType msgtype;
- }SyncMsg;
- Q_DECLARE_METATYPE(SyncMsg)
在應(yīng)用程序.CPP中
- //連接之前再注冊(cè)
- 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
自定義的類 {}后面沒有";"
還有一種可能是pro文件中引用了兩次單元文件;
expected unqualified-id before "int"前一句的";"誤寫為","
在Bulid工程時(shí),qmake *.pro死循環(huán),原因:pro文件里同一文件包含兩次;
char *const p ; p所指向的值不能變;
char cont *p; P所指向的地址不能變;
error: `nameLineEdt\\\' was not declared in this scope 函數(shù)域沒有寫; (函數(shù)域::函數(shù)名())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();
- }
函數(shù)如果有返回值必須寫,否則有造成一些不確定的錯(cuò)誤,如:
- QString a()
- {
- }
- QString str;
- str = "abc";
- str.append(a());
- QMessageBox::warning(this, tr("呼叫"),str,QMessageBox::Ok);
上面的情況,對(duì)話框可以出來,但點(diǎn)擊對(duì)話框中的"確定"后,程序會(huì)死在那;
進(jìn)行信號(hào)連接時(shí),要確保連接參數(shù)中的對(duì)象已經(jīng)創(chuàng)建過,否則會(huì)報(bào)保護(hù)錯(cuò);
圖片加載不了,有可能是Qt庫(kù)中的插件庫(kù)沒有拷貝;
加載路徑指令:
- QCoreApplication::addLibraryPath(QObject::tr("%1%2plugins").arg(QCoreApplication::applicationDirPath()).arg("/"));
qDebug() << "插件加載的路徑是(QCoreApplication::libraryPaths):" << QCoreApplication::libraryPaths()<
有三個(gè)插件加載路徑 1,應(yīng)用程序路徑;2,QTDIR環(huán)境路徑,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());
取得應(yīng)用程序所在路徑,注:結(jié)果后面未加"/"
- QCoreApplication::applicationDirPath()
*.hpp文件,如果改動(dòng),Bulid后對(duì)改動(dòng)后代碼不起作用,必須ReBulid才可以;
小結(jié):通過Qt 編程點(diǎn)滴介紹,也給自己提高了編程過程中需要注意的細(xì)節(jié)問題,由于本話題是一節(jié)一節(jié)為大家展現(xiàn)的,所以更多內(nèi)容,請(qǐng)看編輯推薦。