QT開發(七)——QT按鈕組件

QT開發(七)——QT按鈕組件

    QT中有六種按鈕組件,分別是按壓按鈕QPushButton、工具按鈕QToolButton、單選按鈕QRadioButton、多選按鈕QCheckBox、命令連接按鈕QCommandLinkButton、按鈕盒QButtonBoxapp

1、QPushButton組件

1QPushButton組件簡介

QPushButton組件用於接受用戶點擊事件,可以顯示提示字符串,是功能性組件,須要父組件做爲容器,可以在父組件中進行定位,用於執行命令或觸發事件。框架

QPushButton的類繼承以下:ide

QPushButton public QAbstractButton pubic QWidget public QObject, public QPaintDevice函數

2QPushButton組件屬性

    QPushButton 組件屬性設置選項:工具

    A、name:組件對應源代碼中的名字。佈局

    B、text:組件對應圖形界面中顯示的名字。字體

    C、font:設置text的字體。ui

    D、enabled:組件是否可用。this

3QPushButton組件經常使用成員函數

QPushButton::QPushButtonconst QString &textQWidget *parentconst char *name = 0);spa

構造一個名稱爲name,父對象爲parent而且文本爲text的按壓按鈕。

void QAbstractButton::setTextconst QString &

設置按鈕上顯示的文本。

QString QAbstractButton::text()const

返回按鈕上顯示的文本。

void QAbstractButton::pressed()[signal]

當按下按鈕時,發射信號。

void QAbstractButton::clicked()[signal]

當單擊按鈕時,發射信號。

void QAbstractButton::released()[signal]

當釋放按鈕時,發射信號。

四、QPushButton實例

    QPushButton *button = new QPushButton("OK", this);

    connect(button, SIGNAL(clicked()), this, SLOT(onOK()));

2、QRadioButton組件

1QRaidoButton組件簡介

    QRaidoButton單選按鈕,用於提供兩個或多個互斥選項。

2QRaidoButton組件屬性

    QRaidoButton單選按鈕屬性設置選項:

    A、name:組件對應源代碼中所顯示的名字。

    B、text:組件對應圖形界面中所顯示的名字。

    C、font:設置text字體。

    D、enabled:組件是否可用,可用爲true,不可用爲false。

    E、checked:用來設置或返回是否選中單選按鈕,選中爲true,未選中爲false。

3QRaidoButton組件經常使用成員函數:

QRaidoButton::QRadioButtonconst QString &textQWidget *parentconst char *name = 0

構造一個名稱爲name、父對象爲parent而且文本爲text的單選按鈕。

bool QRadioButton::isChecked()const

返回是否選中單選按鈕,選中時返回true,沒有選中時返回false。

void QAbstractButton ::setTextconst QString &

設置組件上顯示的文本。

QString QAbstractButton ::text()const

返回該按鈕上顯示的文本。

 void QAbstractButton ::stateChangedint state[signal]

當更改checked屬性值時,將發射信號。

void QRadioButton::setCheckedbool check[virtual slot]

設置單選按鈕是否被選中爲checked

四、QRadioButton實例

Widget.h文件:
#ifndef WIDGET_H
#define WIDGET_H
#include <QtGui/QWidget>
#include <QButtonGroup>
#include <QRadioButton>
class Widget : public QWidget
{
    Q_OBJECT
    
public:
    Widget(QWidget *parent = 0);
    ~Widget();
private:
    QButtonGroup *group;
    QRadioButton *appleradio;
    QRadioButton *bananaradio;
    QRadioButton *pearradio;
public slots:
    void onRadioClick();
};
#endif // WIDGET_H

Widget.cpp文件:

#include "widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    group = new QButtonGroup(this);
    appleradio = new QRadioButton("apple", this);
    appleradio->move(5, 5);
    bananaradio = new QRadioButton("banana", this);
    bananaradio->move(5, 25);
    pearradio = new QRadioButton("pear", this);
    pearradio->move(5, 50);
    group->addButton(appleradio, 0);
    group->addButton(bananaradio, 1);
    group->addButton(pearradio, 2);
    appleradio->setChecked(true);
    //多個QRadioButton鏈接到onRadioClick()
    connect(appleradio, SIGNAL(clicked()), this, SLOT(onRadioClick()));
    connect(bananaradio, SIGNAL(clicked()), this, SLOT(onRadioClick()));
    connect(pearradio, SIGNAL(clicked()), this, SLOT(onRadioClick()));
}
Widget::~Widget()
{
    
}
void Widget::onRadioClick()
{
    switch(group->checkedId())
    {
    case 0:
        qDebug() << "apple";
        break;
    case 1:
        qDebug() << "banana";
        break;
    case 2:
        qDebug() << "pear";
        break;
    }
}

Main.cpp文件:

#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QButtonGroup group;
    Widget w;
    w.show();
    
    return a.exec();
}

3、QCheckBox組件

1QCheckBox組件簡介

    QCheckBox複選框,複選框提供多選多。

    QCheckBox有三種狀態:checked、unchecked和PartiallyChecked。

2QCheckBox組件屬性

    QCheckBox複選框屬性設置選項:

    A、name:組件對應源代碼中所顯示的名字。

    B、text:組件對應圖形界面中所顯示的名字。

    C、font:設置text字體。

 D、enabled:組件是否可用,可用爲true,不可用爲false。

    E、checked:用來設置或返回是否選中單選按鈕,選中爲true,未選中爲false。

3QCheckBox組件經常使用成員函數

QCheckBox::QCheckBoxconst QString &textQWidget *parentconst char *name = 0

構造一個名稱爲name、父對象爲parent而且文本爲text的複選框。

bool QCheckBox::isChecked()const

選中複選框,返回true,不然返回false。

void QAbstractButton ::setTextconst QString &

設置組件上顯示的文本。

 QString QAbstractButton ::text()const

返回組件上顯示的文本。

void QAbstractButton ::stateChangeint state[signal]

當更改checked屬性時,將發射這個信號。

void QCheckBox::setCheckedbool check[slot]

設置複選框是否選中,狀態爲check的值。

四、QCheckBox實例

#include <QtGui/QApplication>
#include <QWidget>
#include <QCheckBox>
#include <QVBoxLayout>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    QVBoxLayout *vlayout = new QVBoxLayout(&w);
    QCheckBox *check1 = new QCheckBox("check1", &w);
    QCheckBox *check2 = new QCheckBox("check2", &w);
    QCheckBox *check3 = new QCheckBox("check3", &w);
    vlayout->addWidget(check1);
    vlayout->addWidget(check2);
    vlayout->addWidget(check3);
    w.setLayout(vlayout);
    w.show();
    
    return a.exec();
}

4、QToolButton組件

1QToolButton組件簡介

    QToolButton工具按鈕,是一種用於命令或者選項的能夠快速訪問的按鈕,一般在ToolBar裏面。工具按鈕一般顯示的是圖標,而不是文本標籤。ToolButton支持自動浮起。在自動浮起模式中,按鈕只有在鼠標指向它的時候才繪製三維的框架。

2QToolButton組件屬性

    QToolButton工具按鈕設置選項:

 A、name:組件對應源代碼中的名稱。

    B、text:工具按鈕標籤文本。

    C、font:設置工具按鈕標籤的字體。

    D、autoRaise:自動浮起是否生效。

    E、iconSet:提供顯示在按鈕上的圖標的圖標集。

 F、on:工具按鈕是否爲開。

    G、textLabel:工具按鈕自動提示文本。

    H、usesTextLabel:自動提示文本textLabel是否工做,默認爲false。

3QToolButton組件經常使用成員函數

QToolButton::QToolButtonQWidget *parentconst char *name = 0

構造一個名字爲name,父對象爲parent的ToolButton。

QToolButton::QToolButtonconst QIconset &iconSetconst QString &textLabelconst QString &grouptextQObject *receiverconst char *slotQToolBar *parentconst char *name = 0

構造一個名稱爲name,父對象爲parent(必須爲QToolBar)的工具按鈕。工具按鈕將顯示iconSet,工具提示爲textLabel,狀態條信息爲grouptext,同時會將工具按鈕連接到receiver對象的槽函數。

QToolBButton::QToolButtonArrowType typeQWidget *parentconst char *name = 0

把工具按鈕構形成箭頭按鈕,type定義了箭頭的方向,可用的值有LeftArrow、RightArrow、UpArrow、DownArrow。

void QToolButton::setAutoRaisebool enable

根據參數enable值設置按鈕是否可自動浮起。

void QToolButton::setIconconst QIconSet &

設置顯示在工具按鈕上的圖標。

void QToolButton::setOnbool enable[virtual slot]

設置按鈕是否爲開,enable等於true則設置爲開,不然設置爲關。

void QToolButton::setTextLabelconst QString &[slot]

設置按鈕的提示標籤。

QString QToolButton::textLabel()const

返回按鈕的提示標籤。

void setToolButtonStyle ( Qt::ToolButtonStyle style )

設置ToolButton的樣式,有下列樣式:

Qt::ToolButtonIconOnly只顯示圖標

Qt::ToolButtonTextOnly只顯示文字

Qt::ToolButtonTextBesideIcon文字顯示在圖標旁

Qt::ToolButtonTextUnderIcon文字顯示在圖標下

Qt::ToolButtonFollowStyle根據QStyle::StyleHint進行設置

void setPopupMode ( ToolButtonPopupMode mode )

設置ToolButton的菜單彈出方式ToolButtonPopupMode,彈出方式以下:QToolButton::DelayedPopup延遲彈出

QToolButton::MenuButtonPopup菜單彈出

QToolButton::InstantPopup點擊當即彈出

四、QToolButton實例

#include <QtGui/QApplication>
#include <QWidget>
#include <QToolButton>
#include <QMenu>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    QToolButton *toolbutton = new QToolButton(&w);
    toolbutton->setText("ToolButton");
    QMenu *menu = new QMenu();
    menu->addMenu("1");
    menu->addMenu("2");
    menu->addMenu("3");
    toolbutton->setToolButtonStyle(Qt::ToolButtonTextOnly);
    toolbutton->setPopupMode(QToolButton::InstantPopup   );
    toolbutton->setMenu(menu);
    w.show();
    
    return a.exec();
}

5、QCommandLinkButton組件

1QCommandLinkButton組件簡介

    QCommandLinkButton命令連接按鈕,繼承自QPushButton,用於在互斥選項中選擇一項,QCommandLinkButton除帶有正常的按鈕上的文字描述文本外,默認狀況下,將攜帶一個箭頭圖標,代表按下按鈕將打開另外一個窗口或頁面。

2QCommandLinkButton組件屬性

    QCommandLinkButton組件屬性設置選項:

    A、name:組件對應源代碼中的名稱。

    B、text:組件對應圖形界面中所顯示的標籤。

    C、font:設置text的字體。

    D、enabled:組件是否可用。

    E、description:一個描述性的標籤,以配合按鈕上的文字。

3QCommandLinkButton組件經常使用成員函數

QCommandLinkButton::QCommandLinkButtonQWidget *parent = 0

構造一個父對象爲parent的命令連接按鈕。

QCommandLinkButton::QCommandLinkButtonconst QString &textQWidget *parent = 0

構造一個父對象爲parent、文本爲text的命令連接按鈕。

QCommandLinkButton::QCommandLinkButtonconst QString &textconst QString &descriptionQWidget *parent = 0

構造一個父對象爲parent、文本爲text和描述文本爲description的命令連接按鈕。

void QButton::clicked()[signal]

當單擊該按鈕時,發射信號。

 void QButton::pressed()[signal]

當按下該按鈕時,發射這個信號。

void QButton::released()[signal]

當釋放該按鈕時,發射這個信號。

void QButton::setTextconst QString &

設置改按鈕上顯示的文本。

QString QButton::text()cosnt

返回按鈕上顯示的文本。

6、QDialogButtonBox組件

1QDialogButtonBox組件簡介

    QDialogButtonBox按鈕盒,能夠快速地佈置一組按鈕,有水平和垂直樣式。

2QDialogButtonBox組件屬性

    QDialogButtonBox組件屬性設置選項:

    A、name:該控件對應源代碼中的名稱。

    B、font:設置text的字體。

    C、enabled:該控件是否可用。

    D、centerButtons:ButtonBox中的按鈕是否居中佈局,默認值爲false。

    E、orientation:按鈕佈局方向,Qt提供QT::Horizontal和QT::Vertical兩種。

    F、standardButtons:標準按鈕集合。

    QDialogButtonBox::Ok

    QDialogButtonBox::Open

    QDialogButtonBox::Save

    QDialogButtonBox::Cancel

    QDialogButtonBox::Close

    QDialogButtonBox::Discard

    QDialogButtonBox::Apply

    QDialogButtonBox::Reset

    QDialogButtonBox::RestoreDefaults

    QDialogButtonBox::Help

    QDialogButtonBox::SaveAll

    QDialogButtonBox::Yes

    QDialogButtonBox::YesToAll

    QDialogButtonBox::No

    QDialogButtonBox::NoToAll

    QDialogButtonBox::Abort

    QDialogButtonBox::Retry

    QDialogButtonBox::Ignore

    QDialogButtonBox::NoButton

3QDialogButtonBox組件經常使用成員函數

QDialogButtonBox組件經常使用成員函數:

QDialogButtonBox::QDialogButtonBoxQWidget *parent = 0

構造一個按鈕盒,父對象爲parent。

QDialogButtonBox::QDialogButtonBoxQT::Orientation orientationQWidget *parent = 0

構造一個按鈕盒,父對象爲parent,排列方向爲orientation,而且包含buttons。

QDialogButtonBox::QDialogButtonBoxStandardButton buttonsQT::Orientation orientation = QT::HorizontalQWidget *parent = 0

構造一個按鈕盒,父對象爲parent,排列方向爲orientation。

void QDialogButtonBox::accepted()[signal]

當單擊按鈕盒裏的定義爲AcceptRole和YesRole的按鈕時,發射信號。

void QDialogButtonBox::addButtonQAbstractButton *buttonButtonRole role

向按鈕盒裏添加按鈕button,定義按鈕button的角色爲role,若是role是無效的,則不添加按鈕,若是按鈕已添加,移除並在次添加爲新角色。

QPushButton *QDialogButtonBox::addButtonStandarButton button

向按鈕盒中添加一個標準按鈕button,並返回標準按鈕。若是按鈕無效,不添加,返回0.

QPushButton *QDialogButtonBox::addButtonconst QString &textButtonRole role

建立一個按鈕的文本爲text,以指定角色添加到按鈕盒,並返回相應的按鈕,若是role是無效的,則不建立,返回0.

void QDialogButtonBox::clear()

清空按鈕盒裏的全部按鈕。

void QDialogButtonBox::clickedQAbstractButton *button[signal]

當單擊按鈕盒裏的按鈕button時,發射這個信號。

void QDialogButtonBox::helpRequested()[signal]

當單擊按鈕盒裏的定義爲HelpRole的按鈕時,發射這個信號。

void QDialogButtonBox::rejected()[signal]

當單擊按鈕盒裏定義爲RejectRole和NoRole的按鈕時,發射這個信號。

void QDialogButtonBox::removeButtonQAbstractButton *button

移除按鈕盒裏的按鈕Button,可是不刪除,設置它的父母爲0

void setStandardButtons ( StandardButtons buttons )

設置按鈕盒中的按鈕,使用|設置多個按鈕。

void setOrientation ( Qt::Orientation orientation )

設置按鈕盒的樣式,分爲垂直和水平樣式

QPushButton * button ( StandardButton which ) const

根據StandardButton返回按鈕盒中的按鈕

四、QDialogButtonBox實例

#include <QtGui/QApplication>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QWidget>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QWidget w;
    QDialogButtonBox *button = new QDialogButtonBox(&w);
    button->setStandardButtons(QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok);
    button->button(QDialogButtonBox::Apply)->setText("apply");
    w.show();
    
    return a.exec();
}
相關文章
相關標籤/搜索