QTabWidget隐藏或者使能其中的tab标签页

QTabWidget隐藏或者使能其中的tab标签页

  • 第一种方式
  • 第二种方式

第一种方式

通过QTabWidget的setTabEnabled(int index, bool enable)方法,使能或者禁止对应的tab页面点击。

void QTabWidget::setTabEnabled(int index, bool enable)
If enable is true, the page at position index is enabled; otherwise the page at position index is disabled. The page's tab is redrawn appropriately.
QTabWidget uses QWidget::setEnabled() internally, rather than keeping a separate flag.
Note that even a disabled tab/page may be visible. If the page is visible already, QTabWidget will not hide it; if all the pages are disabled, QTabWidget will show one of them.

效果图:
QTabWidget隐藏或者使能其中的tab标签页_第1张图片
QTabWidget隐藏或者使能其中的tab标签页_第2张图片

void MainWindow::on_pushButton_6_clicked()
{
    ui->tabWidget->setTabEnabled(0, true);
    ui->tabWidget->setTabEnabled(1, false);
}

void MainWindow::on_pushButton_7_clicked()
{
    ui->tabWidget->setTabEnabled(0, false);
    ui->tabWidget->setTabEnabled(1, true);
}

第二种方式

通过QTabWidget的removeTab(int index)和insertTab(int index, QWidget *page, const QString &label)方法来移除和插入tab页。
void QTabWidget::removeTab(int index)
Removes the tab at position index from this stack of widgets. The page widget itself is not deleted.

*int QTabWidget::insertTab(int index, QWidget page, const QString &label)
Inserts a tab with the given label and page into the tab widget at the specified index, and returns the index of the inserted tab in the tab bar. Ownership of page is passed on to the QTabWidget.
The label is displayed in the tab and may vary in appearance depending on the configuration of the tab widget.
If the tab’s label contains an ampersand, the letter following the ampersand is used as a shortcut for the tab, e.g. if the label is “Bro&wse” then Alt+W becomes a shortcut which will move the focus to this tab.
If index is out of range, the tab is simply appended. Otherwise it is inserted at the specified position.
If the QTabWidget was empty before this function is called, the new page becomes the current page. Inserting a new tab at an index less than or equal to the current index will increment the current index, but keep the current page.
Note: If you call insertTab() after show(), the layout system will try to adjust to the changes in its widgets hierarchy and may cause flicker. To prevent this, you can set the QWidget::updatesEnabled property to false prior to changes; remember to set the property to true when the changes are done, making the widget receive paint events again.
测试代码如下:

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->tabWidget->setTabEnabled(0, false);
    ui->tabWidget->removeTab(1);
    ui->tabWidget->removeTab(2);
}

void MainWindow::on_pushButton_6_clicked()
{
    int index = ui->lineEdit_2->text().toInt();
    ui->tabWidget->removeTab(index);

}

void MainWindow::on_pushButton_7_clicked()
{
    int index = ui->lineEdit_2->text().toInt();
    switch (index) {
        case 0: ui->tabWidget->insertTab(0, ui->tab, "tab 1");
            break;
        case 1: ui->tabWidget->insertTab(1, ui->tab_2, "tab 2");
            break;
        case 2: ui->tabWidget->insertTab(2, ui->tab_3, "tab 3");
            break;
        case 3: ui->tabWidget->insertTab(3, ui->tab_4, "tab 4");
            break;
    }
}

你可能感兴趣的:(Qt经验总结,c++,qt)