Qt 編程點(diǎn)滴 初學(xué)者必看 (3)
Qt 編程繼續(xù)為大家講解,還是接著文章 Qt 編程點(diǎn)滴 初學(xué)者必看 (2) ,繼續(xù)介紹,說編程那些細(xì)節(jié)。由于本話題是一節(jié)一節(jié)為大家介紹的,所以更多內(nèi)容請(qǐng)看末尾編輯推薦。
靜態(tài)成員變更量
- aa.h
 - class AA
 - {
 - static char p[13];
 - };
 - aa.cpp
 - char AA::p[13];
 
如果沒在cpp中增加"char AA::p[13];",則編譯時(shí)會(huì)提示"undefined reference to...."的錯(cuò)誤
b.h接口中引用a.h接口,使用時(shí)必須加上
- include "a.h"
 - include "b.h"
 
否則編譯時(shí)會(huì)出現(xiàn)"如果沒在cpp中增加"char AA::p[13];",則編譯時(shí)會(huì)提示"單例模式singleton單元要最先初始化(#include放到最前面)
QWidget類以模式窗體顯示:
- dailPage = new DailForm(0,tel);
 - dailPage->setWindowModality(Qt::ApplicationModal);
 - dailPage->show();
 
事件過濾寫法:
其實(shí)可以通過重載QWidget::keyPressEvent()獲得本類(假設(shè)是窗體)中的幾乎所有鍵盤事件,但焦點(diǎn)在文本框上,就不屬于窗體類啦,所以必須采用在窗體類中添加Event Filters:
- CustomerInfoDialog::CustomerInfoDialog(QWidget *parent)
 - : QDialog(parent)
 - {
 - ...
 - firstNameEdit->installEventFilter(this);
 - lastNameEdit->installEventFilter(this);
 - cityEdit->installEventFilter(this);
 - phoneNumberEdit->installEventFilter(this);
 - }
 
然后在eventFilter中處理相關(guān)鍵盤事件,通過target判斷是否是文本框發(fā)生的鍵盤事件
- bool CustomerInfoDialog::eventFilter(QObject *target, QEvent *event)
 - {
 - if (target == firstNameEdit || target == lastNameEdit
 - || target == cityEdit || target == phoneNumberEdit) {
 - if (event->type() == QEvent::KeyPress) {
 - QKeyEvent *keyEvent = static_cast(event);
 - if (keyEvent->key() == Qt::Key_Space) {
 - focusNextChild();
 - return true;
 - }
 - }
 - }
 - return QDialog::eventFilter(target, event);
 - }
 
去掉窗體標(biāo)題欄:
- setWindowFlags(Qt::FramelessWindowHint);
 - ld.exe cannot find -lSDL
 
處理:環(huán)境變量path加入"D:\QtDevelop\umpcapp\public\SDL-1.2.13\bin"
環(huán)境變量path的設(shè)置:
- D:\QtDevelop\umpcapp\public\STLport-5.1.3\bin;
 - D:\MinGW\bin;
 - D:\Qt\bin;
 - D:\QtDevelop\umpcapp\public\SDL-1.2.13\bin;
 - D:\QtDevelop\umpcapp\public\SDL_mixer-1.2.8\bin
 
注:STLport-5.1.3一定要放在MinGW前面,不然會(huì)出現(xiàn)"QImage: out of memory, returning null image"的錯(cuò)誤;如果要用到STLport庫,那么在配置.pro文件時(shí),一定要記住把stlport放在其它庫的前面,下面的寫法是正確的:
- INCLUDEPATH += . \
 - ../../public/STLport-5.1.3/stlport \ ###這句一定要放在前面
 - ../../public/SDL-1.2.13/include \
 - ../../public/common/include \
 - ../../public/qextserialport-1.1\
 - ../../public/boost-1.37.0/include
 
如果庫的依賴關(guān)系(*.dll)出錯(cuò),則應(yīng)用程序會(huì)出現(xiàn)報(bào)內(nèi)存的錯(cuò)誤,最簡(jiǎn)單的方法就是把應(yīng)用程序,所需要的庫直接加入環(huán)境變量path中,以造成如果庫更新,原來拷在應(yīng)用程序下的庫沒有及時(shí)更新,環(huán)境變更path的設(shè)置例子:
- path += D:\QtDevelop\umpcapp\public\boost-1.37.0\lib;
 - D:\QtDevelop\umpcapp\public\qextserialport-1.1\build
 
上面對(duì)應(yīng)的庫為:
- boost_system-mgw34-mt-1_37.dll;boost_thread-mgw34-mt-1_37.dll;
 - qextserialport.dll
 
編譯成功后,debug下的exe文件不能生成,請(qǐng)檢查.pro文件中,HEADERS與SOURCES參數(shù)配置是否有錯(cuò)誤,比如把.h文件加入SOURCES參數(shù)中,把.cpp加入HEADERS參數(shù)中.
- void MapScene::mouseMoveEvent ( QGraphicsSceneMouseEvent * mouseEvent )
 - {
 - QPointF scenepos;
 - scenepos = mouseEvent->scenePos();
 - //qDebug()<
 
QWebKit中支持Flash播放的代碼(Qt4.5才支持Flash)
- webView->page()->settings()->setAttribute(QWebSettings::PluginsEnabled,true);
 - listWidget->addItem(new QListWidgetItem(QIcon(":/notepaditem.png"), QFile(files[i]).fileName() ));
 
vector 引用的單元:
- #include
 - using namespace std;
 - QString 字符串換行:
 - QString str;
 - str = tr("133");
 - str.append(tr("
 - "));
 
注:br后要加一個(gè)空格;
Qss背景透明:
- QPushButton{
 - background-color: rgba( 255, 255, 255, 0% );
 - }
 
打開指定URL地址
- QUrl url("http://www.zzwtt.com");
 - QDesktopServices::openUrl(url);
 
可以打開任意URL
窗體置前:
- QWidget w;
 - w.setWindowFlags(Qt::WindowStaysOnTopHint);
 - w.show();
 
窗體不顯示在任務(wù)欄:
- setWindowFlags(Qt::Popup) ;
 
注:改變*.h的內(nèi)容,編譯時(shí)會(huì)沒有編譯過程,只有改變*.cpp才會(huì)進(jìn)行編譯;
小結(jié):本文主要介紹了在Qt 窗體的使用,通過Qt 編程點(diǎn)滴介紹,也給自己提高了編程過程中需要注意的細(xì)節(jié)問題,由于本話題是一節(jié)一節(jié)為大家展現(xiàn)的,所以更多內(nèi)容,請(qǐng)看編輯推薦。















 
 
 
 
 
 
 