Qt QTabWidget的簡單應用

Tab控件的簡介:

在這裏插入圖片描述

選項卡由選項欄和頁面區域兩部分組成,一個選項卡關聯一個相關的頁面內容;選項欄默認放置在頁面區域的上方,可以通過 TabPosition接口函數設置選項欄的位置。選項卡與對應的頁面進行關聯,顯示當前頁面時,其他頁面的內容被隱藏。 用戶可以通過單擊其選項卡或按Alt +字母快捷鍵(如果有的話)來顯示其他頁面。

創建一個簡單的Tab 控件:
在這裏插入圖片描述

對應qt 的官方幫助手冊的指導,實現代碼如下:

tabtest.h

#ifndef TABTEST_H
#define TABTEST_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class TabTest; }
QT_END_NAMESPACE

class TabTest : public QMainWindow
{
    Q_OBJECT

public:
    TabTest(QWidget *parent = nullptr);
    ~TabTest();

private:
    Ui::TabTest *ui;
};
#endif // TABTEST_H

tabtest.cpp

#include "tabtest.h"
#include "ui_tabtest.h"
#include <QHBoxLayout>
#include <QRadioButton>

TabTest::TabTest(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::TabTest)
{
    ui->setupUi(this);
    //創建一個tab控件
    QTabWidget * tab = new QTabWidget(this);
    setCentralWidget(tab);
    tab->setTabPosition(QTabWidget::East);//設置選項欄的位置
    //內容頁中添加控件
    QHBoxLayout *hlayout = new QHBoxLayout;
    QRadioButton *continusModeRadioButton = new QRadioButton(tr("連續"));
    QRadioButton *triggerModeRadioButton = new QRadioButton(tr("觸發"));
    hlayout->addWidget(continusModeRadioButton);
    hlayout->addWidget(triggerModeRadioButton);
    QWidget *widget = new QWidget(this);
    widget->setLayout(hlayout);
    
    tab->addTab(widget, "Rabbit");
    QWidget *tabCalibration = new QWidget(this);
    QWidget *tabImage = new QWidget(this);
    tab->addTab(tabCalibration, "Duck");
    tab->addTab(tabImage, "Monkey");
}

TabTest::~TabTest()
{
    delete ui;
}

Tab控件的相關接口函數:

tabPosition:決定選項卡的位置
tabShape:設置選項卡的形狀
currentChanged()當頁面被改變,發送該信號

currentIndex():當前頁面索引
currentWidget():當前頁面控件
indexOf():使用widget檢索具有給定索引的頁面小部件的指針,並可以使用indexOf()查找小部件的索引位置。
setCurrentWidget() or setCurrentIndex() :顯示特定頁面。
setTabText() or setTabIcon():修改選項卡文字或圖標
removeTab():移除選項卡
setTabEnabled():設置選項卡使能,默認是使能狀態

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章