轉載:http://www.verydemo.com/demo_c278_i1869.htmlhtml
使用Qt進行編程必須對 Qt 中經常使用的類有必定的瞭解。這些類能夠分紅兩種:一種不是從 QObject 類派生出來的,用來表示各類基本的數據對象,如字符串、圖像、字體等,這裏將它們通稱爲基本類;另外一種都是從 QWidget 類派生出來的,它們表示一個頂級窗口或者窗口部件,這裏將它們統稱爲窗口類。git
這裏介紹的基本類包括 QChar, QString, QPoint, QSize, QRect, QFont, QPixmap, Qlcon。編程
這裏介紹的窗口類包括 QWidget,QDialog,QLabel,QAbstratacButton,QPushButton, QCheckBox,QRadioButton,QLineEdit數組
QChar安全
QChar 類是 Qt 中用於表示一個字符的類,實如今 QtCore 共享庫中。QChar 類內部用2個字節的Unicode編碼來表示一個字符。app
構造less
QChar 類提供了多個不一樣原型的構造函數以方便使用,如:ide
[plain] view plaincopyprint?
- QChar(); // 構造一個空字符,即'\0'
- QChar(char ch); // 由字符數據ch構造
- QChar(uchar ch); // 由無符號字符數據ch構造
- QChar(ushort code); // 由無符號短整形數據code構造,code是Unicode編碼
- QChar(short code); //由 短整形數據code構造,code是Unicode編碼
- QChar(uint code); // 由無符號整型數據code構造,code是Unicode編碼
- QChar(int code); // 由整型數據code構造,code是Unicode編碼
- QChar();
- QChar(char ch);
- QChar(uchar ch);
- QChar(ushort code);
- QChar(short code);
- QChar(uint code);
- QChar(int code);
- QChar();
- QChar(char ch);
- QChar(uchar ch);
- QChar(ushort code);
- QChar(short code);
- QChar(uint code);
- QChar(int code);
實際使用時不多直接構造 QChar 類的對象,而是把這些構造函數當作類型轉換來用,讓編譯器自動構造所需的QChar類對象。也就是說,在全部須要QChar類做爲參數的地方均可以安全地使用各類整數類型。函數
判斷佈局
QChar 類提供了不少成員函數,能夠對字符的類型進行判斷,如:
[plain] view plaincopyprint?
- bool isDigit() const; // 判斷是不是十進制數字('0' - '9')
- bool isLetter() const; // 判斷是不是字母
- bool isNumber() const; // 判斷是不是數字,包括正負號、小數點等
- bool isLetterOrNumber(); // 判斷是不是字母或數字
- bool isLower() const; // 判斷是不是小寫字母
- bool isUpper() const; // 判斷是不是大寫字母
- bool isNull() const; // 判斷是不是空子符'\0'
- bool isPrint() const; // 判斷是不是可打印字符
- bool isSpace() const; // 判斷是不是分隔符,包括空格等
- bool isDigit() const;
- bool isLetter() const;
- bool isNumber() const;
- bool isLetterOrNumber();
- bool isLower() const;
- bool isUpper() const;
- bool isNull() const;
- bool isPrint() const;
- bool isSpace() const;
- bool isDigit() const;
- bool isLetter() const;
- bool isNumber() const;
- bool isLetterOrNumber();
- bool isLower() const;
- bool isUpper() const;
- bool isNull() const;
- bool isPrint() const;
- bool isSpace() const;
轉換
QChar 類提供了一些成員函數進行數據的轉換,如:
[plain] view plaincopyprint?
- char toAscii() const; // 獲得字符的ASCII碼
- QChar toLower() const; // 轉換成小寫字母
- QChar toUpper() const; // 轉換成大寫字母
- ushort unicode() const; // 獲得Unicode編碼
- char toAscii() const;
- QChar toLower() const;
- QChar toUpper() const;
- ushort unicode() const;
- char toAscii() const;
- QChar toLower() const;
- QChar toUpper() const;
- ushort unicode() const;
注意這幾個函數都不會改變對象自身,轉換的結果經過返回值反映出來。
比較
Qt 中定義了一些與 QChar 類相關的比較操做符, 如:
[plain] view plaincopyprint?
- bool operator != (QChar c1, QChar c2); // 判斷 c1 是否不等於 c2
- bool operator < (QChar c1, QChar c2); // 判斷 c1 是否小於 c2
- bool operator <= (QChar c1, QChar c2); // 判斷 c1 是否小於等於 c2
- bool operator == (QChar c1, QChar c2); // 判斷 c1
- 是否等於c2
- bool operator > (QChar c1, QChar c2); // 判斷 c1 是否大於 c2
- bool operator >= (QChar c1, QChar c2); // 判斷 c1
- 是否大於等於 c2
QString 類是 Qt 中用於表示字符串的類,實如今 QtCore 共享庫中。QString 類在實現上有如下特徵。
1)字符串採用 Unicode 內部編碼,能夠表示世界上大多數語言的文字。
2)字符串的存儲有引用計數,當一個 QString 對象被複製爲另外一個 QString 對象時,它們實際上指向相同的存儲空間,僅僅是增長一個引用計數。
3)採用 「按需複製」 的技術,當指向相同存儲空間的多個 QString 對象中的一個要被修改時,將真正複製一個新的字符串並進行修改。
構造
QString 類提供了不少不一樣原型的構造函數以方便使用。如:
[plain] view plaincopyprint?
- Qstring(); // 構造空字符串
- QString(QChar ch); // 由 QChar 對象 ch構造
- QString(const QChar *pch, int size); // 由 QChar 數組pch構造,size 是數組大小
- QString(const QString &obj); // 拷貝構造函數
- QString(const char *str); // 由字符串 str 構造,str是一個普通字符串
- Qstring();
- QString(QChar ch);
- QString(const QChar *pch, int size);
- QString(const QString &obj);
- QString(const char *str);
- Qstring();
- QString(QChar ch);
- QString(const QChar *pch, int size);
- QString(const QString &obj);
- QString(const char *str);
因爲存在這些構造函數,凡是能夠用 QString 類做爲參數的地方,均可以安全地使用 QChar 對象或普通的字符串。
判斷
能夠用下面的成員函數判斷 QString 對象是否爲空字符串:
[plain] view plaincopyprint?
- bool isEmpty() const; // 判斷是否爲空字符串
轉換
QString 類提供了不少函數用於將字符串轉換爲數值,如:
[plain] view plaincopyprint?
- double toDouble(bool *ok = 0) const; // 轉換爲高精度浮點數
- float toFloat(bool *ok = 0) cosnt; // 轉換爲浮點數
- int toInt(bool *ok, int base = 10) const; // 轉換爲整型數
- long toLong(bool *ok, int base = 10) cosnt; // 轉換爲長整型
- short toShort(bool *ok, int base = 10) const; // 轉換爲短整型
- uint toUInt(bool *ok = 0; int base = 10) const // 轉換爲無符號整型數
- ulong toLong(bool *ok = 0, int base = 10) const; // 轉換爲無符號長整型數
- ushort toUShort(bool *ok = 0, int base = 10) const; // 轉換爲無符號短整型數
- double toDouble(bool *ok = 0) const;
- float toFloat(bool *ok = 0) cosnt;
- int toInt(bool *ok, int base = 10) const;
- long toLong(bool *ok, int base = 10) cosnt;
- short toShort(bool *ok, int base = 10) const;
- uint toUInt(bool *ok = 0; int base = 10) const
- ulong toLong(bool *ok = 0, int base = 10) const;
- ushort toUShort(bool *ok = 0, int base = 10) const;
- double toDouble(bool *ok = 0) const;
- float toFloat(bool *ok = 0) cosnt;
- int toInt(bool *ok, int base = 10) const;
- long toLong(bool *ok, int base = 10) cosnt;
- short toShort(bool *ok, int base = 10) const;
- uint toUInt(bool *ok = 0; int base = 10) const
- ulong toLong(bool *ok = 0, int base = 10) const;
- ushort toUShort(bool *ok = 0, int base = 10) const;
這些函數可以解析 QString 對象的內容,將其轉換爲相應的數值。其中 ok 參數指向一個 bool 型變量, 這個參數用於輸出轉換是否成功的信息。base參數則是轉換爲整數類型時的基。這些函數都不會改變 QString 對象自身。
注意: 當字符串以 0x開頭時,轉換的基自動轉換爲16, 當字符串以0開頭時,轉換的基自動爲8。
下面這些成員函數能夠將一個數值轉換爲字符串並設爲 QString 對象的值:
[plain] view plaincopyprint?
- QString &setNum(int n, int base = 10); // 整型數
- QString &setNum(uint n, int base = 10); // 無符號整型數
- QString &setNum(long n, int base = 10); // 長整型
- QString &setNum(ulong n, int base = 10); // 無符號長整型數
- QString &setNum(short n, int base = 10); // 短整型數
- QString &setNum(ushort n, int base = 10); // 無符號短整型數
- QString &setNum(double n, char format = 'g', int precision = 6); // 高精度浮點數
- QString &setNum(float n, char format = 'g', int precision = 6); // 浮點數
- QString &setNum(int n, int base = 10);
- QString &setNum(uint n, int base = 10);
- QString &setNum(long n, int base = 10);
- QString &setNum(ulong n, int base = 10);
- QString &setNum(short n, int base = 10);
- QString &setNum(ushort n, int base = 10);
- QString &setNum(double n, char format = 'g', int precision = 6);
- QString &setNum(float n, char format = 'g', int precision = 6);
- QString &setNum(int n, int base = 10);
- QString &setNum(uint n, int base = 10);
- QString &setNum(long n, int base = 10);
- QString &setNum(ulong n, int base = 10);
- QString &setNum(short n, int base = 10);
- QString &setNum(ushort n, int base = 10);
- QString &setNum(double n, char format = 'g', int precision = 6);
- QString &setNum(float n, char format = 'g', int precision = 6);
將浮點數轉換爲字符串時,format 參數指定轉化格式,precision 參數指定轉換結果的精度,既有效數組的個數。注意這些函數會改變 QString 對象自己的值,而如下的函數則採用了不一樣的作法,它們返回一個新的臨時對象以供使用:
[plain] view plaincopyprint?
- QString number(int n, int base = 10);
- QString number(uint n, int base = 10);
- QString number(long n, int base = 10);
- QString number(ulong n ,int base = 10);
- QString number(double n, char format = 'q', int precision = 6);
- QString number(int n, int base = 10);
- QString number(uint n, int base = 10);
- QString number(long n, int base = 10);
- QString number(ulong n ,int base = 10);
- QString number(double n, char format = 'q', int precision = 6);
- QString number(int n, int base = 10);
- QString number(uint n, int base = 10);
- QString number(long n, int base = 10);
- QString number(ulong n ,int base = 10);
- QString number(double n, char format = 'q', int precision = 6);
這些函數都是靜態成員函數,於是與某個具體的對象無關,能夠直接經過類名調用。
QString 類也提供了大小寫轉換的函數,如:
[plain] view plaincopyprint?
- QString toLower() const; // 轉換爲小寫
- QString toUpper() const; // 轉換爲大寫
- QString toLower() const;
- QString toUpper() const;
- QString toLower() const;
- QString toUpper() const;
這些函數都不會改變 QString 對象自己,而是將轉換後的結果做爲返回值。
比較
QString 類提供了一個函數用於兩個 QString 對象的比較:
[plain] view plaincopyprint?
- int compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- int compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- int compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive);
這是一個靜態成員函數,它能夠比較 s1 和 s2 的大小,參數 cs 有如下兩個取值。
[plain] view plaincopyprint?
- Qt::CaseInsensitive: 表示對大小寫不敏感
- Qt::Casesensitive : 表示對大小寫敏感
- Qt::CaseInsensitive: 表示對大小寫不敏感
- Qt::Casesensitive : 表示對大小寫敏感
- Qt::CaseInsensitive: 表示對大小寫不敏感
- Qt::Casesensitive : 表示對大小寫敏感
返回值的含義以下:大於 0 表示 s1 大於 s2,等於 0 表示 s1 等於 s2, 小於 0 表示 s1 小於 s2。
爲了方便使用,QString 類還提供瞭如下重載函數用於比較:
[plain] view plaincopyprint?
- int compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
這個函數用於比較 QString 對象自身和 QString 對象 other。 實際上更爲直觀的是使用 QString 類的比較操做符,如:
[plain] view plaincopyprint?
- bool operator < (StringType other) const; // 比較是否小於 other人
- bool operator <= (StringType other) const; // 比較是否小於等於 other
- bool operator == (StringType other) const; // 比較是否等於 other
- bool operator > (StringType other) constt; // 比較是否大於 other
- bool operator >= (StringType other) const; // 比較是否大於等於 other
- bool operator != (StringType other) const; // 比較是否不等於 other
- bool operator < (StringType other) const;
- bool operator <= (StringType other) const;
- bool operator == (StringType other) const;
- bool operator > (StringType other) constt;
- bool operator >= (StringType other) const;
- bool operator != (StringType other) const;
- bool operator < (StringType other) const;
- bool operator <= (StringType other) const;
- bool operator == (StringType other) const;
- bool operator > (StringType other) constt;
- bool operator >= (StringType other) const;
- bool operator != (StringType other) const;
這裏的 StringType 指的是 (const QString &)或 (const char *),哥也就是說,這些操做副既能夠與 QString 對象比較,也能夠與普通的字符串比較。它們的侷限性是第一個操做數必須是 QString 對象,所以,Qt 中又定義瞭如下操做符:
[plain] view plaincopyprint?
- bool operator < (const char *s1, const QString &s2); // 比較 s1 是否小於 s2
- bool operator <= (const char *s1, const QString &s2); // 比較 s1 是否小於等於 s2
- bool operator == (const char *s1, const QString &s2); // 比較 s1 是否等於 s2
- bool operator > (const char *s1, const QString &s2); // 比較 s1 是否大於 s2
- bool operator >= (const char *s1, const QString &s2); // 比較 s1 是否大於等於 s2
- bool operator != (const char *s1, const QString &s2); // 比較 s1 是否不等於 s2
- bool operator < (const char *s1, const QString &s2);
- bool operator <= (const char *s1, const QString &s2);
- bool operator == (const char *s1, const QString &s2);
- bool operator > (const char *s1, const QString &s2);
- bool operator >= (const char *s1, const QString &s2);
- bool operator != (const char *s1, const QString &s2);
- bool operator < (const char *s1, const QString &s2);
- bool operator <= (const char *s1, const QString &s2);
- bool operator == (const char *s1, const QString &s2);
- bool operator > (const char *s1, const QString &s2);
- bool operator >= (const char *s1, const QString &s2);
- bool operator != (const char *s1, const QString &s2);
這些操做符不是 QString 類的成員,它們的第一個參數是普通字符串。
查找
用如下的成員函數能夠判斷 QString 對象是否包含指定的字符串或字符:
[plain] view plaincopyprint?
- bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- bool contains(cosnt ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- bool contains(cosnt ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- bool contains(cosnt ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
用如下的成員函數能夠獲得 QString 對象包含某個特定字符串或字符的個數:
[plain] view plaincopyprint?
- int count(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int count(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int count(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
用如下的成員函數能夠獲得 QString 對象中某個特定字符串或字符出現的位置:
[plain] view plaincopyprint?
- int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
這裏參數 from 是查找的起點,它能夠爲負數,-i 表示倒數第i個字符。查找的方向是從前日後。返回值是查找到的字符串或字符的位置,若是沒有找到則返回 -1。
QString 類中還有與此功能類似的函數用於從後往前查找字符串或字符:
[plain] view plaincopyprint?
- int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
- int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
- int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
字符串處理
QString 類支持用賦值操做符進行對象的複製,其賦值操做符的聲明以下:
[plain] view plaincopyprint?
- QString &operator = (const QString &other); // 複製另一個 QString 對象
- QString &operator = (const char *str); // 複製普通字符串
- QString &operator = (char ch); // 複製字符
- QString &operator = (QChar ch); // 複製 QChar 類對象
- QString &operator = (const QString &other);
- QString &operator = (const char *str);
- QString &operator = (char ch);
- QString &operator = (QChar ch);
- QString &operator = (const QString &other);
- QString &operator = (const char *str);
- QString &operator = (char ch);
- QString &operator = (QChar ch);
如下的成員函數能夠將另外一個字符串或字符接在 QString 對象後面,造成一個總體的字符串:
[plain] view plaincopyprint?
- QString &append(const QString &str); // 接續 QString 對象
- QString &append(const char *str); // 接續普通字符串
- QString &append(QChar ch); // 接續 QChar 對象
- QString &append(const QString &str);
- QString &append(const char *str);
- QString &append(QChar ch);
- QString &append(const QString &str);
- QString &append(const char *str);
- QString &append(QChar ch);
它們的返回值是 QString 對象本身的引用,也就是說,能夠用在這個返回值再次調用成員函數,造成連續的字符串接續操做。
爲了讓代碼更直觀, QString 類中還定義了一個操做符用於字符串的接續:
[plain] view plaincopyprint?
- QString &operator += (const QString &other); // 續接 QString 對象
- QString &operator += (const char *str); // 續接普通字符串
- QString &operator += (char ch); // 接續字符型數據
- QString &operator += (QChar ch); // 接續 QChar 對象
- QString &operator += (const QString &other);
- QString &operator += (const char *str);
- QString &operator += (char ch);
- QString &operator += (QChar ch);
- QString &operator += (const QString &other);
- QString &operator += (const char *str);
- QString &operator += (char ch);
- QString &operator += (QChar ch);
它們的功能與 append 相同。因爲 C++ 語言容許賦值操做符和複合賦值操做符的返回值做爲左值使用,所以它們的返回值也被設計爲 QString 對象本身的引用,故也能夠連續操做。但因爲複合賦值操做符的結合順序是從右往左,要想先計算左邊的操做數須要加括號。
與 append 函數功能相似,如下的成員函數也可以將另外一個字符串或字符與 QString 對象鏈接起來,可是接在原字符串的前面:
[plain] view plaincopyprint?
- QString &prepend(const QString &str); // 在前面接續 QString 對象
- QString &prepend(const char *str); // 在前面接續普通字符串
- QString &prepend(QChar ch); // 在前面接續 QChar 對象
- QString &prepend(const QString &str);
- QString &prepend(const char *str);
- QString &prepend(QChar ch);
- QString &prepend(const QString &str);
- QString &prepend(const char *str);
- QString &prepend(QChar ch);
功能更通常化的是在 QString 對象的任意位置插入另外一個字符串或字符,如:
[plain] view plaincopyprint?
- QString &insert(int position, const QString &str); // 插入字符串
- QString &insert(int position, const QChar *pch, int size); // 插入 QChar 數組
- QString &insert(int position, QChar ch); // 插入 QChar 對象
- QString &insert(int position, const QString &str);
- QString &insert(int position, const QChar *pch, int size);
- QString &insert(int position, QChar ch);
- QString &insert(int position, const QString &str);
- QString &insert(int position, const QChar *pch, int size);
- QString &insert(int position, QChar ch);
這裏 position 參數是要插入的位置,返回值也是對 QString 對象本身的引用。
與插入相反的操做是移除 QString 對象中的一部分,如:
[plain] view plaincopyprint?
- QString &remove(int position, int n);
- QString &remove(int position, int n);
- QString &remove(int position, int n);
這個函數能夠移除 QString 對象中從位置 position 開始的 n 個字符,下面兩個成員函數則能夠從 QString 對象中移除指定的字符串或字符:
[plain] view plaincopyprint?
- QString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive);
如下是 QString 對象的替換操做:
[plain] view plaincopyprint?
- QString &replace(int position, int n, const QString &after); // QString 對象
- QString &replace(int position, int n, const QChar *pch, int size); // QChar 數組
- QString &replace(int opsition, int n, QChar after); // QChar 對象
- QString &replace(int position, int n, const QString &after);
- QString &replace(int position, int n, const QChar *pch, int size);
- QString &replace(int opsition, int n, QChar after);
- QString &replace(int position, int n, const QString &after);
- QString &replace(int position, int n, const QChar *pch, int size);
- QString &replace(int opsition, int n, QChar after);
這三個函數的功能是將 QString 對象從 position 開始的 n 個字符替換爲新內容,新內容分別由 QString 對象、QChar 數組 和 QChar 對象表示。
如下成員函數則能夠搜索指定的字符串或字符並開始替換:
[plain] view plaincopyprint?
- QString &replace(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); // QString 替換爲 QString
- QString &replace(QChar ch, cosnt QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); // QChar 替換爲 QString
- QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive); // Qchar 替換爲 QChar
- QString &replace(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &replace(QChar ch, cosnt QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &replace(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &replace(QChar ch, cosnt QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
下面這個成員函數能夠清空一個 QString 對象的內容,使之成爲空字符串。
[plain] view plaincopyprint?
- void clear();
而下面這個成員函數能夠截斷 QStrring 對象,也就是去掉指定位置後的全部內容:
[plain] view plaincopyprint?
- void truncate(int position); // 從位置 position 截斷,位置從0開始編號
- void truncate(int position);
- void truncate(int position);
下面這個成員函數能夠截掉 QString 對象最後的若干個字符:
[plain] view plaincopyprint?
- void chop(int n); // 截掉最後的 n個字符
如下幾個成員函數能夠獲得 QString 對象的子字符串:
[plain] view plaincopyprint?
- QString left(int n) const; // 獲得左邊 n 個字符構成的子字符串
- QString right(int n) const; // 獲得右邊 n 個字符構成的子字符串
- QString mid(int position, int n = -1) const; // 從中間獲得子字符串
- QString left(int n) const;
- QString right(int n) const;
- QString mid(int position, int n = -1) const;
- QString left(int n) const;
- QString right(int n) const;
- QString mid(int position, int n = -1) const;
從中間獲得子字符串時,參數 position 是子字符串的起始位置,參數 n 是字符串的個數,若是n 爲 -1,則表示一直到原字符串的結尾。
注意上述三個函數並不修改 QString 對象自身,而是返回一個臨時對象以供使用。
下面這個成員函數能夠截去 QString 對象中頭部和尾部的空白字符:
[plain] view plaincopyprint?
- QString trimmed() const;
空白字符包括空格、回車、換行、製表符等字符。下面這個成員函數不只能去掉 QString 對象頭尾的空白字符,還能將中間的連續多個空白字符所有替換成一個空格:
[plain] view plaincopyprint?
- QString simlified() const;
- QString simlified() const;
- QString simlified() const;
加法操做符能夠將兩個字符串或字符鏈接起來並以 QString 臨時對象的方式返回:
[plain] view plaincopyprint?
- const QString operator+(const QString &s1, const QString &s2);
- const QString operator+(const QString &s1, const char *s2);
- const QString operator+(const char s1, const QString &s2);
- const QString operator+(const QString &s, char ch);
- const QString operator+(const QString &s1, const QString &s2);
- const QString operator+(const QString &s1, const char *s2);
- const QString operator+(const char s1, const QString &s2);
- const QString operator+(const QString &s, char ch);
- const QString operator+(const QString &s1, const QString &s2);
- const QString operator+(const QString &s1, const char *s2);
- const QString operator+(const char s1, const QString &s2);
- const QString operator+(const QString &s, char ch);
注意加法操做符的兩個操做數中必須至少有一個是 QString 對象,不然沒法重載操做符。顯然,加法操做副都不是 QString 類的成員。
索引
QString 類也像普通的字符串同樣能夠根據下標獲得某個位置上的字符:
[plain] view plaincopyprint?
- const QChar at(int position) const;
- const QChar at(int position) const;
- const QChar at(int position) const;
這是一個成員函數,更直觀的方法是用如下的操做符:
[plain] view plaincopyprint?
- const QChar operator[] (int position) const;
- const QChar operator[] (uint position) const;
- const QChar operator[] (int position) const;
- const QChar operator[] (uint position) const;
- const QChar operator[] (int position) const;
- const QChar operator[] (uint position) const;
這樣對 QString 對象的取字符操做就相似於對一個字符數組的操做。事實上,經過【】操做符獲得的字符還能夠被修改,要用到另外兩個重載的【】操做符:
[plain] view plaincopyprint?
- QCharRef operator[] (int position);
- QCharRef operator[] (uint position);
- QCharRef operator[] (int position);
- QCharRef operator[] (uint position);
- QCharRef operator[] (int position);
- QCharRef operator[] (uint position);
返回的 QCharRef 類是一個輔助類,對它的修改將反映到原字符串中去。
統計
如下兩個成員函數均可以獲得 QString 對象中字符的個數:
[plain] view plaincopyprint?
- int size() const;
- int length() const;
- int size() const;
- int length() const;
- int size() const;
- int length() const;
注意字符的個數並不必定等於字節數。
QPoint 類表明一個座標點,實如今 QtCore 共享庫中。它能夠認爲是一個整型的橫座標和一個整型的縱座標的組合。
構造
QPoint 類支持如下兩種構造方式:
[plain] view plaincopyprint?
- QPoint(); // 構造橫縱座標均爲 0 的 QPoint 對象
- QPoint(int x, int y); // 構造橫縱座標分別爲 x 和 y 的 QPont 對象
- QPoint();
- QPoint(int x, int y);
- QPoint();
- QPoint(int x, int y);
屬性
經過如下成員函數可獲得 QPoint 對象中的橫縱座標的引用:
[plain] view plaincopyprint?
- int &rx(); // 獲得橫座標的引用
- int &ry(); // 到到縱座標的引用
注意這些引用都不是隻讀的,也就是說能夠經過它們直接修改 QPoint。
經過如下的成員函數能夠設置 QPoint 對象中的橫縱座標:
[plain] view plaincopyprint?
- void setX(int x); // 設置橫座標爲 x
- void setY(int y); // 設置縱座標爲 y
- void setX(int x);
- void setY(int y);
- void setX(int x);
- void setY(int y);
下面兩個成員函數則是隻讀的,能夠得到 QPoint 對象中的橫縱座標:
[plain] view plaincopyprint?
- int x() const; // 得到橫座標
- int y() const; // 得到縱座標
- int x() const;
- int y() const;
- int x() const;
- int y() const;
操做符
QPoint 類支持加法和減法的複合賦值操做:
[plain] view plaincopyprint?
- QPoint &operator+=(const QPoint &point); // 加賦值
- QPoint &operator-=(const QPoint &point); // 減賦值
- QPoint &operator+=(const QPoint &point);
- QPoint &operator-=(const QPoint &point);
- QPoint &operator+=(const QPoint &point);
- QPoint &operator-=(const QPoint &point);
這兩個操做符是它的成員。而如下的操做符則不是它的成員:
[plain] view plaincopyprint?
- cosnt QPoint operator+(const QPoint &p1, const QPoint &p2); // 加法
- const QPoint operator-(const QPoint &p1, const QPoint &p2); // 減法
- const QPoint operator-(const QPoint &point); // 取負數
- bool operator==(const QPoint &p1, const QPoint &p2); // 判斷是否相等
- bool operator!=(const QPoint &p1, const QPoint); // 判斷是否不等
QSize 類表明一個矩形區域的大小,實如今 QtCore 共享庫中。它能夠認爲是由一個整型的寬度和整型的高度組合而成的。
構造
[plain] view plaincopyprint?
- QSize(); // 構造一個非法的 QSize 對象
- QSize(int width, int height); // 構造寬度爲 width、高度爲 height 的 QSize 對象
- QSize();
- QSize(int width, int height);
- QSize();
- QSize(int width, int height);
屬性
如下成員函數能夠獲得 QSize 對象中寬度和高度的引用:
[plain] view plaincopyprint?
- void setWidth(int width); // 設置寬度
- void setHeight(int height); // 設置高度
- void setWidth(int width);
- void setHeight(int height);
- void setWidth(int width);
- void setHeight(int height);
經過如下成員函數能夠得到 QSize 對象的寬度和高度:
[plain] view plaincopyprint?
- int width() const; // 得到寬度
- int height() const; // 得到高度
- int width() const;
- int height() const;
- int width() const;
- int height() const;
操做符
QSize 類支持和 QPoint 類類似的操做符。
QFont
類表明字體,實如今 QtGui 共享庫中。
構造
QFont 類有如下幾個經常使用的構造函數:
[plain] view plaincopyprint?
- QFont(); // 由應用程序的默認字體構造新字體對象
- QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false);
- QFont();
- QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false);
- QFont();
- QFont(const QString &family, int pointSize = -1, int weight = -1, bool italic = false);
其中第二個構造函數的各個參數的含義解釋以下。
1) family: 字體的名稱。
2) pointSize: 字體的點大小,若是這個參數小於等於0,則自動設爲 12。
3) weight: 字體的粗細。
4) italic: 字體是否爲斜體。
這些參數也能夠在字體對象構造之後經過屬性來修改。
屬性
QFont 類的經常使用屬性以下所示:
字體的屬性 獲取所用成員函數 設置全部成員函數
名稱 QString family() const; void setFamily(const QString &family);
點大小 int pointSize() const; void setPointSize(int pointSize);
像素大小 int pixelSize() const; void setPixelSize(int pixelSize);
粗細 int weight() const; void setWeight(int weight);
粗體 bool bold() const; void setBold(bool enable);
斜體 bool italic() const; void setItalic(bool enable);
下劃線 bool underline() const; void setUnderline(bool enable);
其中設置粗體屬性實際上就是將字體的粗細設爲一個肯定的值。點大小與像素大小是指定字體大小的兩種方式。若是指定了點大小,則像素大小屬性的值就是 -1;反之若是指定了像素大小,則點大小屬性的值就是 -1。
若是指定的字體在使用時沒有對應的字體文件,Qt 將自動選擇最接近的字體。若是要顯示的字符在字體中不存在,則字符會被顯示爲一個空心方框。
QPixmap 類表明圖像,實如今 QtGui 共享庫中。
構造
如下構造函數生成的 QPixmap 對象爲空圖像:
[plain] view plaincopyprint?
- QPixmap(); // 構造一個大小爲 0 的空圖像
如下構造函數生成大小的 QPixmap 對象,但圖像數據未初始化:
[plain] view plaincopyprint?
- QPixmap(const QSize &size); // 構造大小爲 size 的圖像,圖像數據未初始化
- QPixmap(int width, int height); // 等價於 QPixmap(QSize(width, height));
- QPixmap(const QSize &size);
- QPixmap(int width, int height);
- QPixmap(const QSize &size);
- QPixmap(int width, int height);
如下構造函數可以從指定的文件中加載圖像並生成 QPixmap 對象:
[plain] view plaincopyprint?
- QPixmap(const QString &filename, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
- QPixmap(const QString &filename, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
- QPixmap(const QString &filename, const char *format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor);
其各個參數的含義解釋以下。
1) filename: 文件名。
2) format: 字符串,表示圖像文件的格式,若是爲 0,將進行自動識別。
3) flags:表示顏色的轉換模式。
若是圖像文件加載失敗則產生空圖像,這裏 flags 參數有如下取值。
1) Qt::AutoColor:由系統自動決定。
2) Qt::ColorOnly:彩色模式。
3) Qt::MonoOnly:單色模式。
圖像參數
如下成員函數能夠得到 QPixmap 對象所表示的圖像的相關信息:
[plain] view plaincopyprint?
- int depth() const; // 顏色深度,既每像素所佔的比特數
- int width() const; // 圖像寬度,單位是像素
- int height() const; // 圖像高度,單位是像素
- QSize size() cosnt; // 圖像的大小,即 QSize(width(), height());
- QRect rect() const; // 圖像的矩形區域,即 QRect(QPoint(0,0),size());
- int depth() const;
- int width() const;
- int height() const;
- QSize size() cosnt;
- QRect rect() const;
- int depth() const;
- int width() const;
- int height() const;
- QSize size() cosnt;
- QRect rect() const;
加載和保存圖像
用下面的成員函數能夠從文件加載圖像:
[plain] view plaincopyprint?
- bool load(const QString &filename, const char *fornat = 0, Qt::ImageCoversionFlags flags = Qt::AutoColor);
- bool load(const QString &filename, const char *fornat = 0, Qt::ImageCoversionFlags flags = Qt::AutoColor);
- bool load(const QString &filename, const char *fornat = 0, Qt::ImageCoversionFlags flags = Qt::AutoColor);
這裏各個參數的含義與構造函數中同樣,返回值爲 true 表示加載成功,false 表示加載失敗。相反的操做是將 Qpixmap 表明的圖像保存到文件,可用如下成員函數:
[plain] view plaincopyprint?
- bool save(const QString &filename, const char *format = 0, int quality = -1) const;
- bool save(const QString &filename, const char *format = 0, int quality = -1) const;
- bool save(const QString &filename, const char *format = 0, int quality = -1) const;
其各個參數及返回值的含義解釋以下。
1) filename:文件名。
2) format:字符串,表示圖像文件的格式,若是爲 0,將根據文件名的後綴自動肯定文件格式。
3) quality:對於有損壓縮的文件格式來講,它表示圖像保存的質量,質量越低壓縮率越大。取值範圍爲 0~100,-1 表示採用默認值。
4) 返回值:true 表示保存成功,false 表示保存失敗。
判斷
如下成員函數能夠判斷 QPixmap 對象是否爲空圖像:
[plain] view plaincopyprint?
- bool isNull() const; // 判斷是否爲空圖像
QIcon
類表明圖標,實如今 QtGui 共享庫中。QIcon 對象能夠認爲是一系列圖像的組合,每一個圖像表明窗口在某種狀態下應該1顯示的圖標。
構造
QIcon 類支持如下構造函數:
[plain] view plaincopyprint?
- QIcon(); // 構造一個空圖像構成的圖標
- QIcon(const QPixmap &pixmap); // 從 Pixmap 對象構造函數
- QIcon(const QString &filename); // 從圖像文件構造圖標
- QIcon();
- QIcon(const QPixmap &pixmap);
- QIcon(const QString &filename);
- QIcon();
- QIcon(const QPixmap &pixmap);
- QIcon(const QString &filename);
當從 QPixmap 對象構造圖標時,系統會自動產生窗口不一樣狀態下對應的圖像,好比窗口在禁用狀態下其圖標爲灰色。從文件構造圖標時,文件並非馬上加載,而是當圖標要顯示時才加載。
添加圖像
下面的成員函數能夠從圖像文件中向 QIcon 對象添加圖像:
[plain] view plaincopyprint?
- void addFile(const QString &filename, const QSize &size = QSize(), Mode mode = Normal, State state = Off);
- void addFile(const QString &filename, const QSize &size = QSize(), Mode mode = Normal, State state = Off);
- void addFile(const QString &filename, const QSize &size = QSize(), Mode mode = Normal, State state = Off);
其中各個參數的含義解釋以下。
1)filename:文件名。
2)size:指定大小。
3)mode:指定使用模式,即窗口在何種狀態下使用這個圖像。
4)state:指定使用狀態。
Mode 爲 QIcon 類的成員類型,有如下取值。
1)QIcon::Normal:窗口爲使能狀態,但未激活。
2)QIcon::Disabled:窗口爲禁用狀態。
3)QIcon::Active:窗口爲激活狀態。
4)QIcon::Selected:窗口被選中。
當窗口的狀態切換時,默認的圖標繪製函數會自動根據窗口的狀態重繪圖標。若是窗口還有所謂的開關狀態(好比一個按鈕能夠有按下和彈起兩個狀態),則還能夠根據 state 參數來選擇不一樣的圖像。state 參數爲 State 類型,這也是一個 QIcon 類的成員類型,它有如下取值。
QIcon::Off:窗口在關狀態。
QIcon::On:窗口在開狀態。
另一個成員函數能夠直接將 QPixmap 對象添加到 QIcon 對象中:
[plain] view plaincopyprint?
- void addPixmap(const QPixmap &pixmap, Mode mode = Normal, State state = Off);
- void addPixmap(const QPixmap &pixmap, Mode mode = Normal, State state = Off);
- void addPixmap(const QPixmap &pixmap, Mode mode = Normal, State state = Off);
這裏的 pixmap 參數是要添加的 QPixmap 對象,mode 參數和 state 參數的含義與 addFile 函數中相同。
獲取圖像
下面的成員函數能夠獲取 QIcon 對象中的圖像:
[plain] view plaincopyprint?
- QPixmap pixmap(const QSize &size, Mode mode = Normal, State state = Off) const;
- QPixmap pixmap(const QSize &size, Mode mode = Normal, State state = Off) const;
- QPixmap pixmap(const QSize &size, Mode mode = Normal, State state = Off) const;
其中參數 size 是指定的大小,參數 mode 和 state 則是使用模式和狀態。這個函數還有如下重載版本:
[plain] view plaincopyprint?
- QPixmap pixmap(int w, int h, Mode mode = Normal, State state = Off) const;
- QPixmap pixmap(int extent, Mode mode = Normal, State state = Off) const;
- QPixmap pixmap(int w, int h, Mode mode = Normal, State state = Off) const;
- QPixmap pixmap(int extent, Mode mode = Normal, State state = Off) const;
- QPixmap pixmap(int w, int h, Mode mode = Normal, State state = Off) const;
- QPixmap pixmap(int extent, Mode mode = Normal, State state = Off) const;
它們分別等價於如下的函數調用:
[plain] view plaincopyprint?
- pixmap(QSize(w, h), mode, state);
- pixmap(QSize(extent,, extent), mode, state);
- pixmap(QSize(w, h), mode, state);
- pixmap(QSize(extent,, extent), mode, state);
- pixmap(QSize(w, h), mode, state);
- pixmap(QSize(extent,, extent), mode, state);
注意返回的圖像大小可能比指定的小,但不會比指定的大。
判斷
如下成員函數能夠判斷 QIcon 對象是否爲空圖像構成的圖標:
[plain] view plaincopyprint?
- bool isNull() const; // 判斷是否爲空圖像構成的圖標
QWidget
類表明通常的窗口,其餘窗口類都是從 QWidget 類繼承出來的。而 QWidget 類則同時繼承了 QObject 類 和 QPaintDevice 類,也就是說,窗口類都是 Qt 對象類。這裏的 QPaintDevice 類則是全部可繪製的對象的基類。
經常使用窗口類的繼承關係如圖所示:

構造
QWidget 類的構造函數以下:
[plain] view plaincopyprint?
- QWidget(QWidget *parent = 0, Qt::WindowFlags f = 0);
- QWidget(QWidget *parent = 0, Qt::WindowFlags f = 0);
- QWidget(QWidget *parent = 0, Qt::WindowFlags f = 0);
其中參數 parent 指向父窗口,若是這個參數爲 0,則窗口就成爲一個頂級窗口
參數 f 是構造窗口的標誌,主要用於控制窗口的類型和外觀等,有如下經常使用值。
1)Qt::FramelessWindowHint:沒有邊框的窗口。
2)Qt::WindowStaysOnTopHint:老是最上面的窗口。
3)Qt::CustomizeWindowHint:自定義窗口標題欄,如下標誌必須與這個標誌一塊兒使用纔有效,不然窗口將有默認的標題欄。
4)Qt::WindowTitleHint:顯示窗口標題欄。
5)Qt::WindowSystemMenuHint:顯示系統菜單。
6)Qt::WindowMinimizeButtonHint:顯示最小化按鈕。
7)Qt::WindowMaximizeButtonHint:顯示最大化按鈕。
8)Qt::WindowMinMaxbuttonHint:顯示最小化按鈕和最大化按鈕。
9)Qt::WindowCloseButtonHint:顯示關閉按鈕。
獨立窗口
窗口構造的時候若是有 Qt::Window 標誌,那麼它就是一個獨立窗口,不然就是一個依附於其餘獨立窗口的窗口部件。頂級窗口必定是獨立窗口,但獨立窗口不必定是頂級的,它能夠有父窗口,當父窗口被析構時它也會隨之被析構。獨立窗口通常有本身的外邊框和標題欄,能夠有移動、改變大小等操做。
一個窗口是否爲獨立窗口可用下面的成員函數來判斷:
[plain] view plaincopyprint?
- bool isWindow() const; // 判斷是否爲獨立窗口
下面這個函數能夠獲得窗口部件所在的獨立窗口。
[plain] view plaincopyprint?
- QWidget *window() const; // 所得所在的獨立窗口
固然,若是窗口自己就是獨立窗口,那麼獲得的就是本身。
而下面這個函數能夠獲得窗口的父窗口:
[plain] view plaincopyprint?
- QWidget *parentWidget() const; // 獲得父窗口
- QWidget *parentWidget() const;
- QWidget *parentWidget() const;
窗口標題
WindowTitle 屬性表示窗口的標題,與之相關的成員函數以下:
[plain] view plaincopyprint?
- QString windowTitle() const; // 得到窗口標題
- void setWindowTitle(const QString &text); // 設置窗口標題爲 text
- QString windowTitle() const;
- void setWindowTitle(const QString &text);
- QString windowTitle() const;
- void setWindowTitle(const QString &text);
幾何參數
這裏的幾何參數指的是窗口的大小和位置。一個窗口有兩套幾何參數,一套是窗口外邊框所佔的矩形區域,另外一套是窗口客戶區所佔的矩形區域。所謂窗口客戶區就是窗口中去除邊框和標題欄用來顯示內容的區域。
這兩套幾何參數分別由兩個 QRect 型的屬性表明,相關的成員函數以下:
[plain] view plaincopyprint?
- const QRect &geometry() const; // 獲取客戶區幾何參數
- void setGeometry(int x, int y, int w, int h); // 設置客戶取幾何參數
- void setGeometry(const QRect &rect); // 設置客戶區幾何參數
- QRect frameGeometry() const; // 獲取外邊框幾何參數
- const QRect &geometry() const;
- void setGeometry(int x, int y, int w, int h);
- void setGeometry(const QRect &rect);
- QRect frameGeometry() const;
- const QRect &geometry() const;
- void setGeometry(int x, int y, int w, int h);
- void setGeometry(const QRect &rect);
- QRect frameGeometry() const;
這裏雖然沒有直接設置外邊框幾何參數的函數,但客戶區幾何參數變化以後,外邊框的幾何參數也會隨之變化。設置幾何參數可能會使窗口的位置及大小發生變化,這時會發送窗口移動事件 QMoveEvent,若是大小有變化,還會發送窗口改變大小事件 QResizeEvent,事件的處理函數分別是 moveEvent 和 resizeEvent。注意這裏的座標都是相對於父窗口的,所以移動一個窗口並不致使它的全部部件都接收到移動事件。
注意:不要在 moveEvent 或 resizeEvent 兩個事件處理函數中設置幾何參數,不然將致使無限循環。
窗口的幾何參數也能夠由用戶的操做改變,這時也會發送相應的事件。
爲了方便使用,與幾何參數相關的成員函數還有如下這些:
[plain] view plaincopyprint?
- QPoint pos() const; // 得到窗口左上角的座標(外邊框幾何參數)
- QSize size() const; // 窗口大小 (客戶區幾何參數)
- int x() const; // 窗口左上角橫座標 (外邊框幾何參數)
- int y() const; // 窗口左上角縱座標 (外邊框幾何參數)
- int height() const; // 窗口高度 (客戶區幾何參數)
- int width() const; // 窗口寬度 (客戶區幾何參數)
- QPoint pos() const;
- QSize size() const;
- int x() const;
- int y() const;
- int height() const;
- int width() const;
- QPoint pos() const;
- QSize size() const;
- int x() const;
- int y() const;
- int height() const;
- int width() const;
能夠看出,座標所有是外邊框幾何參數,而大小所有是客戶區幾何參數。要得到外邊框的大小須要用下面這個成員函數:
[plain] view plaincopyprint?
- QSize frameSize() const; // 窗口大小 (外邊框幾何參數)
改變這些屬性能夠用下面這些成員函數:
[plain] view plaincopyprint?
- void move(int x, int y); // 將窗口左上角移動到座標(x, y)處;
- void move(const QPoint &pos); // 將窗口左上角移動到 pos 處;
- void resize(int w, int h); // 將窗口的寬度改成 w, 高度改成 h
- void resize(const QSize &size); // 將窗口的大小改成 size
- void move(int x, int y);
- void move(const QPoint &pos);
- void resize(int w, int h);
- void resize(const QSize &size);
- void move(int x, int y);
- void move(const QPoint &pos);
- void resize(int w, int h);
- void resize(const QSize &size);
一樣,這裏 move 函數用的是外邊框幾何參數,而 resize 函數用的是客戶區幾何參數。
還有一個屬性比較特殊,相關的成員函數以下:
[plain] view plaincopyprint?
- QRect rect() const; // 獲取窗口區域
它得到的座標都是相對於窗口本身的客戶區的,也就是說橫縱座標永遠爲 0。
注意: 對於一個窗口部件來講,它的兩套幾何參數是一致的。
可見性與隱藏
可見性指的是窗口是否顯示在屏幕上的屬性。被其餘窗口暫時遮擋住的窗口也屬於可見的。可見性由窗口的 visible 屬性表示,與之相關的成員函數以下:
[plain] view plaincopyprint?
- bool isVisible() const; // 判斷窗口是否可見
- bool isHidden() const; // 判斷窗口是否隱藏
- virtual void setVisible(bool visible); // 設置窗口是否隱藏
- void setHidden(bool hidden); // 等價於 setvisible(!hidedn);
- bool isVisible() const;
- bool isHidden() const;
- virtual void setVisible(bool visible);
- void setHidden(bool hidden);
- bool isVisible() const;
- bool isHidden() const;
- virtual void setVisible(bool visible);
- void setHidden(bool hidden);
visible 屬性爲 true 時表示窗口可見,爲 false 時表示窗口不可見。這裏要注意的是,setVisible 函數實際上設置的是窗口是否隱藏,而不是可見性。可見性與隱藏有以下關係。
1)隱藏的窗口必定是不可見的。
2)非隱藏的窗口在它的父窗口可見的狀況下也是可見的。
3)非隱藏的頂級窗口是可見的。
setVisible 和 setHidden 同時也是槽,它們通常並不直接使用,而是使用如下幾個槽:
[plain] view plaincopyprint?
- void show(); // 顯示窗口,等價於 setVisible(true);
- void hide(); // 隱藏窗口,等價於 setHidden(true);
- void show();
- void hide();
- void show();
- void hide();
當窗口顯示時,將發送 QShowEvent 事件;當窗口隱藏時,將發送 QHideEvent 事件。事件的處理函數分別是 showEvent 和 hideEvent。
窗口狀態
獨立窗口有正常、全屏、最大化、最小化幾種狀態,與之相關的成員函數以下:
[plain] view plaincopyprint?
- bool isMinimized() const; // 判斷窗口是否爲最小化
- bool isMaximized() const; // 判斷窗口是否爲最大化
- bool isFullScreen() const; // 判斷窗口是否爲全屏
- void showMinimized(); // 以最小化方式顯示窗口,這是一個槽
- void showMaximized(); // 以最大化方式顯示窗口,這是一個槽
- void showFullScreen(); // 以全屏方式顯示窗口,這是一個槽
- void showNormal(); // 以正常方式顯示窗口,這是一個槽
- bool isMinimized() const;
- bool isMaximized() const;
- bool isFullScreen() const;
- void showMinimized();
- void showMaximized();
- void showFullScreen();
- void showNormal();
- bool isMinimized() const;
- bool isMaximized() const;
- bool isFullScreen() const;
- void showMinimized();
- void showMaximized();
- void showFullScreen();
- void showNormal();
注意後 4 個函數同時也是槽。全屏方式與最大化的區別在於:全屏方式下窗口的邊框和標題欄消失,客戶區佔據整個屏幕。窗口的各類狀態僅對獨立窗口有效,對窗口部件來講沒有意義。
另外還有一個 windowState 屬性和窗口狀態有關,相關的成員函數以下:
[plain] view plaincopyprint?
- Qt::WindowStates windowState() const; // 獲取窗口狀態
- void setWindowState(Qt::WindowStates windowState); // 設置窗口狀態
- Qt::WindowStates windowState() const;
- void setWindowState(Qt::WindowStates windowState);
- Qt::WindowStates windowState() const;
- void setWindowState(Qt::WindowStates windowState);
這裏的 Qt::WindowStates 類型有如下幾個取值。
1)Qt::WindowNoState:無標誌,正常狀態。
2)Qt::WindowMinimized:最小化狀態。
3)Qt::WindowMaxmized:最大化狀態。
4)Qt::WindowFullScreen:全屏狀態。
5)Qt::WindowActive:激活狀態。
這裏取值能夠用 「按位或」 的方式組合起來使用。
須要注意的是,調用 setWindowState 函數將使窗口變爲隱藏狀態。
使能
處於使能狀態的窗口才能處理鍵盤和鼠標等輸入事件,反之,處於禁用狀態的窗口不能處理這些事件。窗口是否處於使能狀態由屬性 enabled 表示,相關成員函數以下:
[plain] view plaincopyprint?
- bool isEnabled() const; // 得到窗口的使能裝態
- void setEnabled(bool enable); // 設置窗口的使能狀態,這是一個槽
- void setDisabled(bool disabled); // 等價於 setEnabled(!disable),這是一個槽
- bool isEnabled() const;
- void setEnabled(bool enable);
- void setDisabled(bool disabled);
- bool isEnabled() const;
- void setEnabled(bool enable);
- void setDisabled(bool disabled);
其中兩個設置屬性的函數同時也是槽。窗口的使能狀態也可能影響外觀,好比處於禁用狀態的按鈕文本自己爲灰色。
使能狀態和窗口的可見性有類似的邏輯:禁用一個窗口同 時會使它的全部子窗口成爲禁用狀態。
激活狀態
當有多個獨立窗口同時存在時,只有一個窗口可以處於激活狀態。系統產生的鍵盤、鼠標等輸入事件將被髮送給處於激活狀態的窗口。通常來講,這樣的窗口會被提高到堆疊層次的最上面,除非其餘窗口有總在最上面的屬性。與激活狀態相關的成員函數以下:
[plain] view plaincopyprint?
- bool isActiveWindow() const; // 判斷窗口所在的獨立窗口是否激活
- void activateWindow(); // 設置窗口所在的獨立窗口爲激活狀態
- bool isActiveWindow() const;
- void activateWindow();
- bool isActiveWindow() const;
- void activateWindow();
注意:這裏操做的其實不是窗口自己,而是窗口所在的獨立窗口,由於窗口部件時沒有激活狀態的概念的。
焦點
焦點用來控制同一個獨立窗口內哪個部件能夠接受鍵盤事件,同一時刻只能有一個部件得到焦點。與焦點有關的成員函數以下:
[plain] view plaincopyprint?
- bool hasFocus() const; // 判斷窗口是否得到焦點
- void setFocus(); // 使窗口得到焦點,這是一個槽
- void clearFocus(); // 使窗口失去焦點
- QWidget *focusWidget() const; // 獲得窗口內得到焦點的子窗口
- bool hasFocus() const;
- void setFocus();
- void clearFocus();
- QWidget *focusWidget() const;
- bool hasFocus() const;
- void setFocus();
- void clearFocus();
- QWidget *focusWidget() const;
setFocus 函數同時又是一個槽。窗口部件獲得焦點之後,別忘了還須要它所在的獨立窗口處於激活狀態才能獲得鍵盤事件。
一個窗口得到焦點,同時意味着另外一個窗口失去焦點。當窗口得到或失去焦點時,將發送 QFocusEvent 事件,它有兩個處理函數:forceInEvent 和 focusOutEvent,分別對應得到焦點和失去焦點。
值得一提的是 editFocus 屬性,這是一個專門用於嵌入式系統的屬性。由於嵌入式系統一般鍵盤較小,沒有專門用於切換焦點的 Tab 鍵,因此上下方向鍵被用來切換焦點。若是一個窗口部件設置 editFocus 屬性爲 true,則上下方向鍵就再也不用於切換焦點,而是發送給這個窗口。與這個屬性相關的成員函數以下:
[plain] view plaincopyprint?
- bool hasEditfocus() const; // 判斷窗口是否有 editFocus 屬性
- void QWidget::setEditFocus(bool enable); // 設置窗口的 editFocus 屬性
- bool hasEditfocus() const;
- void QWidget::setEditFocus(bool enable);
- bool hasEditfocus() const;
- void QWidget::setEditFocus(bool enable);
捕獲鍵盤和鼠標事件
窗口部件即便得到焦點,也不必定能得到按鍵事件,由於其餘窗口可能會捕獲鍵盤事件。捕獲了鍵盤事件的窗口將獲得全部鍵盤事件,而其餘窗口將徹底獲得不到鍵盤事件,直到捕獲了鍵盤事件的窗口釋放鍵盤事件。與鍵盤事件捕獲相關的成員函數以下:
[plain] view plaincopyprint?
- void grabKeyboard(); // 捕獲鍵盤事件
- void releaseKeyboard(); // 釋放鍵盤事件
- void grabKeyboard();
- void releaseKeyboard();
- void grabKeyboard();
- void releaseKeyboard();
相似的還有鼠標事件的捕獲和釋放,其成員函數以下:
[plain] view plaincopyprint?
- void grabMouse(); // 捕獲鼠標事件
- void releaseMouse(); // 釋放鼠標事件
- void grabMouse();
- void releaseMouse();
- void grabMouse();
- void releaseMouse();
對鍵盤事件和鼠標事件的捕獲是相互獨立的。這裏要注意兩點:一是若是有另一個窗口進行了捕獲操做,則當前處於捕獲狀態的窗口將失去對事件的捕獲;二是隻有可見的窗口才能進行輸入事件捕獲。
如下的成員函數可以獲得應用程序中正在捕獲鍵盤或鼠標事件的窗口:
[plain] view plaincopyprint?
- QWidget *keyboardGrabber(); // 獲得正在捕獲鍵盤事件的窗口
- QWidget *mouseGrabber(); // 獲得正在捕獲鼠標事件的窗口
- QWidget *keyboardGrabber();
- QWidget *mouseGrabber();
- QWidget *keyboardGrabber();
- QWidget *mouseGrabber();
這兩個函數是靜態函數。
佈局
屬性 layout 表明窗口的頂級佈局,相關的成員函數以下:
[plain] view plaincopyprint?
- QLayout *layout() const; // 得到頂級佈局
- void setLayout(QLayout *layout); // 設置頂級佈局