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)
在應用程序.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
自定義的類 {}后面沒有";"
還有一種可能是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();
- }
函數如果有返回值必須寫,否則有造成一些不確定的錯誤,如:
- QString a()
- {
- }
- QString str;
- str = "abc";
- str.append(a());
- QMessageBox::warning(this, tr("呼叫"),str,QMessageBox::Ok);
上面的情況,對話框可以出來,但點擊對話框中的"確定"后,程序會死在那;
進行信號連接時,要確保連接參數中的對象已經創建過,否則會報保護錯;
圖片加載不了,有可能是Qt庫中的插件庫沒有拷貝;
加載路徑指令:
- QCoreApplication::addLibraryPath(QObject::tr("%1%2plugins").arg(QCoreApplication::applicationDirPath()).arg("/"));
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());
取得應用程序所在路徑,注:結果后面未加"/"
- QCoreApplication::applicationDirPath()
*.hpp文件,如果改動,Bulid后對改動后代碼不起作用,必須ReBulid才可以;
小結:通過Qt 編程點滴介紹,也給自己提高了編程過程中需要注意的細節問題,由于本話題是一節一節為大家展現的,所以更多內容,請看編輯推薦。