QT 入門之常用控制項屬性設定

2021-10-06 05:45:45 字數 2334 閱讀 3594

這裡我在這講一下,我在開發專案的時候常用的使用方式。qt的功能很強大,建構函式的方式也很多。以下方式可供初學者參考。後續會繼續更新!

//需要新增標頭檔案

#include //標籤

//常見屬性設定

//建立物件

this->inte***cetitle = new qlabel(this);

//設定標籤的位置,注意我傳入的是qrect

inte***cetitle->setgeometry(qrect(154, 63, 170, 15));

//設定標籤文字

inte***cetitle->settext("設定標籤裡面的文字");

//設定文字的排版方式為水平居中

inte***cetitle->setalignment(qt::aligncenter);

/*(1)qt::alignleft::水平方向靠左。

(2)qt::alignright:水平方向靠右。

(3)qt::alignhcenter:水平方向居中。

(4)qt::alignjustify:水平方向調整間距兩端對齊。

(5)qt::aligntop:垂直方向靠上。

(6)qt::alignbutton:垂直方向靠下。

(7)qt::alignvcenter:垂直方向居中。

(8)qt::aligncenter:水平、垂直方向居中

*///設定為標籤的背景括號裡面傳入pixmap物件

//inte***cetitle->setpixmap(pixmap_gamelogo);

//可以使用pixmap物件的scaled來設定大小

//pixmap_gamelogo = pixmap_gamelogo.scaled(20,20);

inte***cetitle->setstylesheet(qstring::fromutf8("qlabel");

//設定標籤屬性,

/* font: bold :字型加粗

font-size:15px :字型大小為15

color: yellow:字型顏色為黃色

background-color: green:標籤背景設定為綠色

*/

補充:上面的**中有用到qstring::fromutf8,舉例一下:

qstring a = qstring::fromutf8(「括號裡面的東西太長的的話可以這樣\n」

「接下去寫自己想寫的東西,但是末尾要用\n記得雙引號也要再括起來」);

//需要的標頭檔案

#include //設定位置、大小

my_pushbutton = new qpushbutton(this);

my_pushbutton->setgeometry(qrect(150, 290, 150, 45));

//設定為按鈕背景

qpixmap pixmap_musicbutto("image/btn/login.png");

my_pushbutton->seticon(pixmap_musicbutton);

my_pushbutton->seticonsize(qsize(150, 45));

//除去透明的部分

my_pushbutton->setstylesheet("qpushbutton");

//需要的標頭檔案

#include //建立物件,並且設定位置

this->pwdlineedit = new qlineedit(this);

pwdlineedit->setgeometry(qrect(170, 310, 165, 31));

//限制編輯框裡字元長度

pwdlineedit->setmaxlength(8);

//設定屬性

portlineedit->setstylesheet("qlineedit");

//設定成密文,

pwdlineedit->setechomode(qlineedit::password);

其中setechomode(qlineedit::password);還可以是一下其中一種

1、qlineedit::normal 顯示輸入編輯框內的字元

2、qlineedit::noecho 不顯示任何內容

3、qlineedit::password 顯示星號,暗文顯示

4、qlineedit::passwordechoonedit 在編輯時顯示輸入的字元,否則顯示星號

以上就是我總結的常用控制項,後續我也會繼續更新其中的內容,其中qlineedit 編輯框會常常用到正規表示式,由於篇幅過大,我會在下篇講解。

Qt常用控制項設定

視窗大小設定 重置視窗大小 resize 600 400 按鈕設定,需要新增 qpushbutton 標頭檔案 按鈕參考文章 選單欄設定,需要新增 qmenubar 標頭檔案 選單欄最多只有乙個 選單欄建立 qmenubar bar menubar 將選單欄放入視窗中 setmenubar bar ...

Qt筆記 設定控制項的基本屬性

morph是變換按鈕型別的意思。比如說button型別按鈕變換到checkbox按鈕等。qt中控制項的顏色屬性在stylesheet中修改,其可修改的專案包括 add resourse 等4項 另一方面,qt為了做到能夠自動變換圖示的大小,使用了稱之為layout的控制項容器,layout在總體上控...

Qt常用控制項

qpushbutton的新增。iocn 新增資源檔案 顯示效果為 前面icon後面文字。一般用來新增文字。qtoolbutton的新增。iocn 新增資源檔案 顯示效果toolbuttonstyle中更改,預設toolbuttonicononly toolbuttontextbesidicon。文字...