QTabWidget多頁面部件的用法及程序示例

*重要函數:* 
1.void setTabText(int, QString); //設置頁面的名字. 
2.void setTabToolTip(QString); //設置頁面的提示信息. 
3.void setTabEnabled(bool); //設置頁面是否被激活. 
4.void setTabPosition(QTabPosition::South); //設置頁面名字的位置. 
5.void setTabsClosable(bool); //設置頁面關閉按鈕。 
6.int currentIndex(); //返回當前頁面的下標,從0開始. 
7.int count(); //返回頁面的數量. 
8.void clear(); //清空所有頁面. 
9.void removeTab(int); //刪除頁面. 
10.void setMoveable(bool); //設置頁面是否可被拖拽移動. 
11.void setCurrentIndex(int); //設置當前顯示的頁面.

signals: 
1.void tabCloseRequested(int). //當點擊第參數個選項卡的關閉按鈕的時候,發出信號. 
2.void tabBarClicked(int). //當點擊第參數個選項卡的時候,發出信號. 
3.void currentChanged(int). //當改變第參數個選項卡的時候,發出信號. 
4.void tabBarDoubleClicked(int). //當雙擊第參數個選項卡的時候,發出信號.


首先在Qt設計師中拖拽出如下的佈局: 
這裏寫圖片描述


以下是”c.cpp”下的代碼:

#include "c.h"

c::c(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);


    //連接信號與槽.
    connect(ui.insertButton, SIGNAL(clicked()), this, SLOT(addPageSlot()));    connect(ui.removeButton, SIGNAL(clicked()), this, SLOT(removePageSlot()));    connect(ui.dragButton, SIGNAL(clicked()), this, SLOT(dragPageSlot()));
}

c::~c()
{

}

void c::addPageSlot()
{
    //定義一個QWidget.
    QWidget *temp = new QWidget;
    //在當前頁面的後面插入一個新的頁面.
    ui.tabWidget->insertTab(ui.tabWidget->currentIndex() + 1, temp, QIcon("Icons/2.png"), QString::number(count));    //顯示新的頁面.
    ui.tabWidget->setCurrentIndex(ui.tabWidget->indexOf(temp));
    count++;
}

void c::removePageSlot()
{
    //刪除當前的頁面.
    ui.tabWidget->removeTab(ui.tabWidget->currentIndex());}
void c::dragPageSlot()
{
    //設置頁面項是可被移動的.
    ui.tabWidget->setMovable(true);}12345678910111213141516171819202122232425262728293031323334353637383940

然後是”c.h”下的代碼:

#ifndef C_H#define C_H#include <QtWidgets/QMainWindow>#include "ui_c.h"#include <QTabWidget>#include <QPushButton>class c : public QMainWindow
{
    Q_OBJECTpublic:    c(QWidget *parent = 0);
    ~c();private slots:    void addPageSlot();    void removePageSlot();    void dragPageSlot();private:
    Ui::cClass ui;    int count = 0;
};#endif // C_H123456789101112131415161718192021222324252627

最後是”main.cpp”下的代碼:

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


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