搜尋此網誌

2012年1月26日 星期四

const 指標變數修飾標的


int num1;
num1 是個 int

int *num2;
num2 是個指標, 指標所指的位置是個 int

const int *num3;
num3 是個指標, 指標所指的位置是個 const int,所以你不可以透過num3這個指標去改變num3所指到的位址的值.

int * const num4;
num4 是個 const 指標,你不能改變 num4 所指的位址,但可以透過num4這個指標去改變 num4所指到位址的值.

const int * const num5;
num5 是個 const 指標, 指標所指的位置是 const int,所以不能改變num5所指的位址,亦不能透過num5這個指標去改變num5所指到的位址的值.


判斷方法:
以 * 為基準點作劃分.
例如
const int * const num;

   如果 const 出現在*的左邊, 表示 num 這個指標所指到的位址的值是 const (不能透過num這個指標去改變num所指到的位址的值)

   如果 const 出現在*的右邊, 表示 num 這個指標是 const (不能改變num這個指標所指到的位址)

2011年11月22日 星期二

C++ 型別轉換


C++ 的型別轉換有以下四種轉換方式 分別是 static_cast, dynamic_cast,reinterpret_cast 以及const_cast .分述如下:

static_cast
可用於轉換基底類別指標為衍生類別指標,也可用於傳統的資料型態轉換。
舉例來說,在指定的動作時,如果右邊的數值型態比左邊的數值型態型態長度大時,
超出可儲存範圍的部份會被自動消去,例如將浮點數指定給整數變數,則小數的部份會被自動消去
Ex:
int num1 = 0;
double num2 = 1.11;
num1 = static_cast<int>(num2);
dynamic_cast
使用static_cast(甚至是傳統的C轉型方式)將基底類別指標轉換為衍生類別指標,
這種轉型方式稱為強制轉型,但是在執行時期使用強制轉型有危險性,
因為編譯器無法得知轉型是否正確.
Ex:
#include <iostream>
struct Base {
    virtual ~Base() {}
    virtual void name() {}
};
struct Derived: Base {
    virtual ~Derived() {}
    virtual void name() {}
};

struct Some {
    virtual ~Some() {}
};

int main()
{
    Some *s = new Some;
    Base* b1 = new Base;
    Base* b2 = new Derived;

    Derived *d1 = dynamic_cast<Derived*>(b1);
    Derived *d2 = dynamic_cast<Derived*>(b2);
    Derived *d3 = dynamic_cast<Derived*>(s);
    Base *d4 = dynamic_cast<Base*>(s);

    std::cout << "'b1' points to 'Derived'? : " << (bool) d1 << '\n';
    std::cout << "'b2' points to 'Derived'? : " << (bool) d2 << '\n';
    std::cout << "'s' points to 'Derived'? : " << (bool) d3 << '\n';
    std::cout << "'s' points to 'Base'? : " << (bool) d4 << '\n';
}

Output:
i = 4
type::i = 4
'b1' points to 'Derived'? : false
'b2' points to 'Derived'? : true
's' points to 'Derived'? : false
's' points to 'Base'? : false 
reinterpret_cast
用於將一種型態的指標轉換為另一種型態的指標,例如將char*轉換為int*
Ex:
#include  <iostream>
using namespace std;
int main() {
    int i;
    char* str = "test";
    i = reinterpret_cast(str);
    cout << i << endl;
    return 0;
}
const_cast
用於一些特殊場合可以覆寫變數的const屬性,利用cast後的指標就可以更改變數的內部。
const_cast是一危險的轉型"唯一使用"的場合是當物件資料屬性為const時但有時卻必須傳遞給非const參數的成員函數或一般全域函數時才可用它來移除const屬性因為唯有如此才可
符合型態相同(形式引數(參數))但使用時必須確定此轉型後不會藉此來更改其資料否則可能會造成無法預測的結果!!!
Ex:
#include <iostream>
struct type {
    type():i(3)
    {}

    void m1(int v) const 
    {
       const_cast<type*>(this)->i = v;
    }
    int i;
};
 
int main() 
{
    type t;
    t.m1(4);
    std::cout << "type::i = " << t.i << '\n';
}

Output:
type::i = 4

2011年11月4日 星期五

如何紀錄一個矩形區塊(rb)在一個矩形範圍(ra)的相對位置 ,並依比例還原至絕對位置及範圍

A. 紀錄相對位置及範圍
1. 利用 QPointF 分別紀錄rb在ra當中的左上角的點,與右下角的點的相對位置
ex:

   // 假設變數定義如下
   // s_topleft_x  (rb 左上角x在ra的絕對位置)
   // s_topleft_y  (rb 左上角y在ra的絕對位置)
   // s_bottomright_x  (rb 右下角x在ra的絕對位置)
   // s_bottomright_y   (rb 右下角y在ra的絕對位置)
   // picWidth    (ra的寬)
   // picHeight   (ra的高)
   
   QPointF topPF = QPointF((double)s_topleft_x/picWidth, (double)s_topleft_y/picHeight);
     QPointF bottomPF = QPointF((double)s_bottomright_x/picWidth, (double)s_bottomright_y/picHeight);
   
2. 設定 QRectF
ex:
    QRectF newRect(topPF, bottomPF);
   
   
以上newRect就記錄了rb在ra中的相對位置及範圍了


B.還原相對位址

  // 假設變數定義如下
  // newPicWidth  (欲還原ra的寬)
  // newPicHeight (欲還原ra的高)
  // newRect (之前紀錄的rb的相對位置的矩形範圍) 
 
  QRect mRect;
    mRect.setX((int)(((double)newPicWidth)* newRect.x()));
    mRect.setY((int)(((double)newPicHeight)* newRect.y()));
    mRect.setBottom((int)(((doublenewPicHeight)* newRect.bottom()));
    mRect.setRight((int)(((double)newPicWidth)* newRect.right()));
 
以上 mRect 就是 rb在新的ra當中的絕對位置及範圍了

2011年9月29日 星期四

在Qt中實現多語系的功能

1. 在程式中所有要翻譯的文字都必須在 QObject::tr()之中

ex:
QMessageBox msgb(this);
msgb.setWindowTitle(tr("Illegal"));
msgb.setText(tr("Illegal product!"));
msgb.setIcon(QMessageBox::Warning);
msgb.exec();


2. 在.pro檔中,設定預計產生的翻譯檔語系名稱及其位置 (本例名稱為 test.pro)

ex:
TRANSLATIONS += ./translations/test_en.ts ./translations/test_tw.ts ./translations/test_jp.ts ./translations/test_cn.ts


3. 執行 lupdate 產生 .ts檔

ex:
lupdate test.pro


4. 使用 Qt Linguist 編輯.ts檔

5. 使用 lrelease 產生 .qm檔

ex:
lrelease ./translations/*.ts


6. copy .qm檔到你所指定的目錄

ex: cp -ax ./translations/*.qm /home/daniel/language/qm/*


7. 在主程式 main當中,依據需求載入你所需的qm檔

ex:

QTranslator translator, defTrans;
QLocale g_sysLocale;

//繁體中文
translator.load("test_tw", "/home/daniel/language/qm");
defTrans.load("qt_zh_TW", "/home/daniel/language/qm");

app.installTranslator(&translator);
app.installTranslator(&defTrans);
g_sysLocale = QLocale(QLocale::Chinese, QLocale::Taiwan);

// 簡體中文
/*
translator.load("test_cn", "/home/daniel/language/qm");
defTrans.load("qt_zh_CN", "/home/daniel/language/qm");
app.installTranslator(&translator);
app.installTranslator(&defTrans);
g_sysLocale = QLocale(QLocale::Chinese, QLocale::China);
*/

QLocale::setDefault(g_sysLocale);
MainWindow mainWin(0);
mainWin.setLocale(g_sysLocale);
mainWin.start();
return app.exec();


8. 所有包在 QObject::tr()中的字串,就會依照所載入的qm檔,做文字置換動作

2011年9月19日 星期一

如何在QT中將QVariant轉換成定義在QtGui中的資料型態 (如 QColor...等),或自定型別

因為QVariant是QtCore庫的一部分,它不能夠提供定義在QtGui當中的類型(如 QColor)的轉換
如要轉換,方法式將其轉換至指定的樣版型別,如下所示,(PS:自訂型別方法相同):

QColor color = variant.value();

不過在轉換之前最好先用canConvert()的函式來檢查是否可提供該型別轉換,如下所示:

if (variant.canConvert()) {
QColor color = variant.value();
}

反向轉換(如把QColor轉成QVariant)則和一般的型態(非定義在QtCui或自訂型別)是一樣的

2011年6月14日 星期二

在QT中如何在QApplication中攔截X11 的事件

1. 繼承 QApplication, 並改寫 x11EventFilter virtual function .


class NewApplication : public QApplication
{
Q_OBJECT

public:
NewApplication(int& argc, char ** argv);

virtual bool x11EventFilter(XEvent *event);

private:

};



bool NewApplication::x11EventFilter(XEvent *event)
{
switch (event->type) {
case ButtonPress:
{
if (event->xbutton.button == 2) {
// 攔截中鍵 ... 寫你要的動作
}
}
case ButtonRelease:
case KeyPress:
case KeyRelease:
case MotionNotify:
};

return NewApplication::x11EventFilter(event);
}


2. 在mail裡面把建立QApplication改成建立NewApplication

QApplication app(argc, argv);

改成

NewApplication app(argc, argv);


3. start app

app.exec();

2011年3月18日 星期五

台灣新三寶

宅男有三寶:科技、女優、人真好 型男有三寶:親親、抱抱、再推倒 周董有三寶:唉呦、不錯、這個屌 韓劇有三寶:車禍、癌症、醫不好 台客有三寶:拎娘、G8、 跨啥小 威武有三寶:蜆精、大鵰、愛福好 謊言有三寶:天長、地久、愛到老 謊言再三寶:海枯、石爛、你最好 老師有三寶:劃線、重要、這會考 國小有三寶:起立、立正、老師好 國中有三寶:腦殘、白目、愛耍屌 高中有三寶:補習、熬夜、拼聯考 大學生有三寶:複製、貼上、過就好 大學男生有三寶:聯誼、把咩、學妹好 大學女生有三寶:化妝、打扮、穿的少 上班族有三寶:上班、下班、公司倒 海綿寶寶有三寶:小蝸、水母、愛蟹堡 海綿寶寶再三寶:章魚、海星、比奇堡 成人頻道有三寶:彩虹、星穎、新東寶 台灣的網路有三寶: 斷線、LAG、........修不好