Qt - 常用控制元件
按鈕抽象基類(QAbstractButton)
QAbstractButton類是按鈕部件的抽象基類,提供了按鈕所共有的功能。
QAbstractButton類實現了一個抽象按鈕,並且讓它的子類來指定如何處理使用者的動作,並指定如何繪製按鈕。
QAbstractButton提供了點選和勾選按鈕。QRadioButton和QCheckBox類只提供了勾選按鈕,QPushButton和QToolButton提供了點選按鈕,如果需要的話,它們還可以提供切換行為。
任何按鈕,都可以顯示一個包含文字和圖示的標籤。
-
setText(const QString&) 設定文字
-
setIcon(const QIcon&) 設定圖示
訊號與槽
signals
-
clicked,pressed,released訊號
QPushButton*btn = new QPushButton("Touch Me",this); btn->move(100,100); //按鈕按下釋放之後會觸發 connect(btn,&QPushButton::clicked,this,[](){qDebug()<<"clicked";}); //按鈕按下觸發 connect(btn,&QPushButton::pressed,this,[](){qDebug()<<"pressed";}); //按鈕釋放觸發 connect(btn,&QPushButton::released,this,[](){qDebug()<<"released";});
-
toggled訊號:每當切換按鈕(toggleButton)改變其狀態時,就會發出此訊號。
btn->setCheckable(true); //設定按鈕可選中
connect(btn,&QPushButton::toggled,this,[=]()
{
qDebug()<<"toggled"<<btn->isChecked();
});
slots
-
void animateClick(int msec = 100) 定時自動點選按鈕
-
void click() 自動點選按鈕
-
void setIconSize(const QSize &size) 設定圖示大小,較小的圖示可能會設定無效
-
void setChecked(bool) 設定是否選中按鈕(checkable必須被啟用)
-
void toggle() 切換按鈕的選中狀態
其他函式
序號 | 函式&描述 |
---|---|
1 | int void setAutoExclusive(bool) 可選中按鈕是否獨佔, 在獨佔按鈕組(同一父物件為同一組)中,任何時候只能選中一個按鈕 |
2 | void setAutoRepeat(bool) 如果啟用,按鈕按下不鬆開,pressed()、released()和clicked()訊號會定期發出 |
3 | void setAutoRepeatDelay(int) 如果啟用了autoRepeat,那麼autoRepeatDelay將定義自動重複生效前的初始延遲(以毫秒為單位)。 |
4 | void setAutoRepeatInterval(int) 如果啟用了autoRepeat,則autoRepeatInterval定義了自動重複間隔的長度,以毫秒為單位。 |
5 | void setCheckable(bool) 設定按鈕是否能夠被選中,預設是不能被選中的 |
6 | void setDown(bool) 設定按鈕是否被按下 |
7 | void setIcon(const QIcon &icon) 設定圖示 |
8 | void setShortcut(const QKeySequence &key) 設定快捷鍵 |
9 | void setText(const QString &text) 設定文字 |
1,按鈕(PushButton)
最常用的控制元件之一,應用場景十分廣泛。
訊號與槽
signals
Inherits:QAbstractButton 繼承自父類
slots
void showMenu() 如果有選單,彈出選單,否則啥也不做(這個槽貌似沒啥用)
常用函式
序號 | 函式&描述 |
---|---|
1 | int void setAutoDefault(bool) 設為自動預設按鈕,按下Enter鍵時會自動按下按鈕 |
2 | void setDefault(bool) 設為自動預設按鈕,按下Enter鍵時會自動按下按鈕 |
3 | void setFlat(bool) 去掉按鈕的邊框,讓PushButton按鈕跟背景色融為一體,在點選按鈕時,會出現原來按鈕背景。 |
4 | void setMenu(QMenu *menu) 設定選單。 這將把按鈕變成一個選單按鈕,在某些樣式中,它將在按鈕文字的右側產生一個小三角形。 |
QMenu* menu = new QMenu("Menu");
menu->addAction("大家好");
menu->addAction("我是頑石老師");
btn->setMenu(menu);
connect(menu,&QMenu::triggered,this,[=](QAction*act)
{
btn->setText(act->text());
});
2,工具按鈕(ToolButton)
QToolButton是一個特殊的Button, 提供快速訪問特定的命令或選項。與普通命令按鈕不同, QToolButton通常不顯示文字標籤, 而是顯示圖示。一般用在toolBar上
訊號與槽
signals
Inherits:QAbstractButton 繼承自父類
void triggered(QAction *action) 按鈕繫結的選單動作被觸發
slots
void setDefaultAction(QAction *action) 如果有選單,彈出選單,否則啥也不做(這個槽貌似沒啥用)
void setToolButtonStyle(Qt::ToolButtonStyle style) 設定工具按鈕是否僅顯示圖示、僅顯示文字,還是圖示旁邊/下面的文字。
void showMenu()
QToolButton*toolbtn = new QToolButton(this);
toolbtn->move(200,100);
toolbtn->setText("hello");
//設定圖示
toolbtn->setIcon(style()->standardIcon
(QStyle::StandardPixmap::SP_FileIcon));
//設定文字顯示位置
toolbtn->setToolButtonStyle
(Qt::ToolButtonStyle::ToolButtonTextUnderIcon);
常用函式
序號 | 函式&描述 |
---|---|
1 | int void setArrowType(Qt::ArrowType type) 此屬性用於儲存按鈕是否顯示箭頭而不是普通圖示 |
2 | void setAutoRaise(bool enable) 去掉邊框和背景,滑鼠在按鈕上面時,顯示選中效果,按下時有下沉效果 |
3 | void setMenu(QMenu *menu) 設定彈出選單 |
4 | void setPopupMode(QToolButton::ToolbuttonPopupMode mode) 描述如何將彈出選單與工具按鈕一起使用,預設設定為DelayedPopup |
彈出選單
QMenu*menu = new QMenu;
menu->addAction("C語言");
menu->addAction("C++");
toolbtn->setMenu(menu);
//設定彈出模式,DelayedPopup延時彈出 MenuButtonPopup在右側顯示一個箭頭 InstantPopup立即彈出
toolbtn->setPopupMode(QToolButton::DelayedPopup);
//設定按鈕選單之後,右下角有一個小箭頭,去掉箭頭
toolbtn->setStyleSheet("QToolButton::menu-indicator {image: none;}");
3,單選按鈕(RadioButton)
QRadioButton部件提供了一個帶有文字標籤的單選按鈕。
QRadioButton是一個可以切換選中(checked)或未選中(unchecked)狀態的選項按鈕。單選框通常呈現給使用者一個“多選一”的選擇。也就是說,在一組單選框中,一次只能選中一個單選框。
訊號與槽
signals
Inherits:QAbstractButton 繼承自父類
slots
Inherits:QAbstractButton 繼承自父類
常用函式
序號 | 函式&描述 |
---|---|
1 | void setCheckState(Qt::CheckState state) 將複選框的複選狀態設定為state。 如果不需要三狀態支援,還可以使用QAbstractButton::setChecked(),它接受布林值。 |
2 | void setTristate(bool) 該屬性儲存複選框是否是三狀態複選框,預設為false,即複選框只有兩個狀態 |
使用方法
-
同一組(同一父物件)的單選按鈕一次只能選中一個
QRadioButton*radiobtn = new QRadioButton("男",this);
QRadioButton*radiobtn1 = new QRadioButton("女",this);
//設定預設選中
radiobtn->setChecked(true);
-
同一組同時選中多個
QGroupBox* exampleGroup = new QGroupBox("esample",this);
QLabel*label = new QLabel("你喜歡以下哪些寵物?");
exampleGroup->move(300,300);
QRadioButton *dogbtn = new QRadioButton("狗");
QRadioButton *catbtn = new QRadioButton("貓");
QRadioButton *snakebtn = new QRadioButton("蛇");
QRadioButton *pigbtn = new QRadioButton("豬");
dogbtn->setAutoExclusive(false);
catbtn->setAutoExclusive(false);
snakebtn->setAutoExclusive(false);
pigbtn->setAutoExclusive(false);
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(label);
layout->addWidget(dogbtn);
layout->addWidget(catbtn);
layout->addWidget(snakebtn);
layout->addWidget(pigbtn);
exampleGroup->setLayout(layout);
-
不同組的單選按鈕可以同時選中
QGroupBox* sexGroup = new QGroupBox("性別",this);
QGroupBox* viewpointGroup = new QGroupBox("觀點",this);
sexGroup->move(200,300);
viewpointGroup->move(300,300);
QRadioButton*radiobtn = new QRadioButton("男",sexGroup);
QRadioButton*radiobtn1 = new QRadioButton("女",sexGroup);
QRadioButton*radiobtn2 = new QRadioButton("好",viewpointGroup);
QRadioButton*radiobtn3 = new QRadioButton("壞",viewpointGroup);
QHBoxLayout* sexLayout = new QHBoxLayout;
sexLayout->addWidget(radiobtn);
sexLayout->addWidget(radiobtn1);
QHBoxLayout* viewpointLayout = new QHBoxLayout;
viewpointLayout->addWidget(radiobtn2);
viewpointLayout->addWidget(radiobtn3);
sexGroup->setLayout(sexLayout);
viewpointGroup->setLayout(viewpointLayout);
4,複選框(CheckBox)
QCheckBox提供了一個帶文字標籤的複選框。
QCheckBox(複選框)和QRadioButton(單選框)都是選項按鈕。這是因為它們都可以在開(選中)或者關(未選中)之間切換。區別是對使用者選擇的限制:單選框定義了“多選一”的選擇,而複選框提供的是“多選多”的選擇。
儘管在技術上可以通過複選框來實現單選框的行為,反之亦然,但還是強烈建議使用眾所周知的約定。
訊號與槽
signals
Inherits:QAbstractButton 繼承自父類
void stateChanged(int state)
當複選框的狀態發生變化時,即當用戶選中或取消選中它時,就會發出這個訊號。
slots
Inherits:QAbstractButton 繼承自父類
使用方法
QGroupBox* exampleGroup = new QGroupBox(this);
exampleGroup->move(300,300);
QLabel*label = new QLabel("你喜歡以下哪些寵物?");
QCheckBox *dogbtn = new QCheckBox("狗");
QCheckBox *catbtn = new QCheckBox("貓");
QCheckBox *snakebtn = new QCheckBox("蛇");
QCheckBox *pigbtn = new QCheckBox("豬");
QVBoxLayout* layout = new QVBoxLayout;
layout->addWidget(label);
layout->addWidget(dogbtn);
layout->addWidget(catbtn);
layout->addWidget(snakebtn);
layout->addWidget(pigbtn);
exampleGroup->setLayout(layout);
5,行編輯器(LineEdit)
訊號與槽
signals
Inherits:QAbstractButton 繼承自父類
void clear() //清除行編輯的內容
void copy() const //將選中的文字複製到剪貼簿(如果有的話),並且echoMode()是Normal
void cut() //剪下
void paste() //貼上
void redo() //撤銷
void selectAll() //選中所有
void setText(const QString &)
void undo() //反撤銷
slots
void showMenu() 如果有選單,彈出選單,否則啥也不做(這個槽貌似沒啥用)
//這個訊號在游標移動時發出。前一個位置由oldPos給出,新位置由newPos給出
void cursorPositionChanged(int oldPos, int newPos)
//編輯完成,按下Return或Enter鍵或行編輯失去焦點時將發出此訊號
void editingFinished()
//當用戶按下一個不被認為是可接受輸入的鍵時,就會發出這個訊號。 例如,如果一個按鍵導致驗證器的validate()呼叫返回Invalid。 另一種情況是試圖輸入超過行編輯的最大長度的字元。
void inputRejected()
//編不編輯,當按下Return或Enter鍵時都發出此訊號,失去焦點不會發
void returnPressed()
//這個訊號在選擇改變時發出
void selectionChanged()
//每當文字發生變化時,就會發出這個訊號。與texttedited()不同,呼叫setText()改變文字,此訊號也會發出。
void textChanged(const QString &text)
//只要文字被編輯,就會發出這個訊號。
void textEdited(const QString &text)
設定顯示模式
edit->setEchoMode(QLineEdit::EchoMode::Password);
QLineEdit::Normal //顯示輸入的字元,這是預設值。
QLineEdit::NoEcho //不要顯示任何東西
QLineEdit::Password //顯示與平臺相關的密碼掩碼字元,而不是實際輸入的字元。
QLineEdit::PasswordEchoOnEdit //在編輯時顯示已輸入的字元,完成顯示掩碼字元
edit->setClearButtonEnabled(true); //啟用清除按鈕
設定輸入掩碼
掩碼字元 | 含義 |
---|---|
A | ASCII字母字元是必須的,A-Z,a-z |
a | ASCII 字母字元是允許的但不是必須的 |
N | ASCII字母字元是必須的,A-Z,a-z, 0-9 |
n | ASCII 字母字元是允許的但不是必須的 |
X | 任何字元都可以,是必須需要的 |
x | 任何字元都允許的,但不是必須需要的 |
9 | ASCII 數字是必須要的,0-9 |
0 | ASCII 數字是允許的,但不是必須要的 |
D | ASCII 數字是必須要的,1-9 |
d | ASCII 數字是允許的,但不是必須要的 |
# | ASCII 數字是或加減符號允許的,但不是必須要的 |
H | 十六進位制資料字元是必須要的,A-F, a-f, 0-9 |
h | 十六進位制資料字元是允許的,但不是必須要的 |
B | 二進位制資料字元是必須要的,0-1 |
b | 二進位制資料字元是允許的,但不是必須要的 |
> | 所有的字元字母都都大寫的 |
< | 所有的字元字幕都是小寫的 |
! | 關閉大小寫 |
;c | 終止輸入掩碼並將空白字元設定為c |
\ | 使用 \ 去轉義上面的字元,如果再需要顯示上述字元的時候 |
-
輸入日期
edit->setText(QDate::currentDate().toString("yyyy-MM-dd"));
edit->setInputMask("9999-99-99");
-
輸入祕鑰
edit->setInputMask(">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA");
設定驗證器
-
只能輸入整數
edit->setValidator(new QIntValidator(-90,90,this));
-
只能輸入浮點數,但是浮點數驗證器不能限制範圍,可以隨便輸入
edit->setValidator(new QDoubleValidator(-90.0,90.0,3,this));
新增動作
void QLineEdit::addAction(QAction *action, QLineEdit::ActionPosition position)
QAction *QLineEdit::addAction(const QIcon &icon, QLineEdit::ActionPosition position)
QLineEdit *edit = new QLineEdit(this);
edit->addAction(QIcon("://images/user.png"),QLineEdit::ActionPosition::LeadingPosition);
QAction *delAct = edit->addAction(QIcon("://images/delete.png"),QLineEdit::ActionPosition::TrailingPosition);
connect(delAct,&QAction::triggered,[]()
{
qDebug()<<"delAct";
});
6,標籤(Label)
QLabel小部件提供文字或影象顯示
訊號與槽
signals
//當用戶點選連結時會發出此訊號。
void linkActivated(const QString &link)
//當用戶將滑鼠懸停在連結上時會發出此訊號。
void linkHovered(const QString &link)
slots
void clear()
void setMovie(QMovie *movie)
void setNum(double num)
void setNum(int num)
void setPicture(const QPicture &picture)
void setPixmap(const QPixmap &)
void setText(const QString &)
公有函式
序號 | 函式&描述 |
---|---|
1 | void setAlignment(Qt::Alignment) 設定對齊方式 |
2 | void setBuddy(QWidget *buddy) 將此標籤的好友設定為buddy。當用戶按下此標籤指示的快捷鍵時,鍵盤焦點將轉移到標籤的好友小部件。夥伴機制僅適用於包含文字的 QLabel,其中一個字元以與號“&”為字首。 |
3 | void setIndent(int) 設定label的文字縮排,以畫素為單位 |
4 | void setMargin(int) 設定邊距 |
5 | void setOpenExtrenalLinks(bool open) 設定是否自動開啟超連結 |
6 | void setScaledContents(bool) 設定內容縮放,確定標籤是否將其內容縮放以填充所有可用空間。 |
7 | void setSelection(int start,int len) 設定對齊方式 |
8 | void setTextFormat(Qt::TextFormat) 設定標籤文字格式 |
9 | void setTextInteractionFlags(Qt::TextInteractionFlags flag) 設定對齊方式 |
10 | void setWordWrap(bool on) 設定是否在需要時自動換行 |
Example:
-
顯示文字
QLabel* label = new QLabel("我是萌萌噠的小可愛",this);
label->setAlignment(Qt::AlignmentFlag::AlignCenter);
label->setFixedWidth(100); //設定固定的寬度
label->setWordWrap(true); //當文字超過固定的寬度之後,自動換行
-
設定超連結:QLabel支援html文字
label->setText("<a href=\"www.baidu.com\">百度一下</a>");
connect(label,&QLabel::linkHovered,this,[=](const QString& link){qDebug()<<"linkHovered"<<link;});
connect(label,&QLabel::linkActivated,this,[=](const QString& link){qDebug()<<"linkActivated"<<link;});
//設定自動開啟超連結,而不是發出訊號自己處理,這個設定之後會自動在瀏覽器開啟連線
label->setOpenExternalLinks(true);
-
設定夥伴
QLabel* nameLabel = new QLabel("&Name",this);
QLineEdit * nameEdit = new QLineEdit;
nameLabel->setBuddy(nameEdit);
QLabel* phoneLabel = new QLabel("電話(&P)",this);
QLineEdit* phoneEdit = new QLineEdit;
phoneLabel->setBuddy(phoneEdit);
QGridLayout* layout = new QGridLayout;
layout->addWidget(nameLabel,0,0);
layout->addWidget(nameEdit,0,1);
layout->addWidget(phoneLabel,1,0);
layout->addWidget(phoneEdit,1,1);
setLayout(layout);
-
顯示圖片
QLabel* label = new QLabel(this);
label->setPixmap(QPixmap("://images/label_img.jpg"));
//如上所示,顯示的圖片是固定的大小,如何讓圖片按我們想要的大小顯示呢?
//1,設定Label的大小,如果有佈局,會隨著佈局動態變化
label->setFixedSize(340,180);
//2,設定內容縮放
label->setScaledContents(true);
-
顯示Gif動圖
QLabel* label = new QLabel(this);
QMovie *movie = new QMovie("F:/MyCode/QtCode/QtCourse/DisplayWidgets/images/label_gif.gif");
label->setMovie(movie);
movie->start();
7,分組框(GroupBox)
訊號與槽
signals
void clicked(bool checked = false)
void toggled(bool on)
slots
void setChecked(bool checked)
公有函式
序號 | 函式&描述 |
---|---|
1 | void setAlignment(Qt::Alignment) 設定對齊方式 |
2 | void setCheckAble(bool checkable) 該分組框是否可以被選擇 |
3 | void setflat(bool flat) 分組框通常由頂部有標題的包圍框組成。 如果啟用此屬性,則大多數樣式只繪製框架的頂部部分; 否則,將繪製整個框架。 |
4 | void setTitle(const QString& title) 設定分組框的標題 |
QGroupBox* groupBox = new QGroupBox(this);
groupBox->move(100,100);
groupBox->resize(320,320);
groupBox->setTitle("我是分組框");
//groupBox->setAlignment(Qt::AlignCenter);
//groupBox->setFlat(true);
groupBox->setCheckable(true);
connect(groupBox,&QGroupBox::clicked,this,[](){qDebug()<<"clicked";});
connect(groupBox,&QGroupBox::toggled,this,[](){qDebug()<<"toggled";});
QList<QCheckBox*> checkBoxs;
for (int i = 0;i<5;i++)
{
checkBoxs.push_back(new QCheckBox(QString("checkBox%1").arg(i),groupBox));
checkBoxs[i]->move(10,i*40+20);
}
groupBox->setChecked(true);
搜尋
複製