美化QTabWidget (2)

美化QTabWidget(2)

美化QTabWidget (2)_第1张图片

1.效果展示

2.用法展示

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setupUI();

    QWidget *widget = new QWidget();
    widget->setStyleSheet("background-color:#FF00FF00");
    ui->tabWidget->addTab2(widget, tr("this is first tab"));
}

MainWindow::~MainWindow()
{
    delete ui;
    delete frame_less_helper_;
}

void MainWindow::setupUI()
{
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
    frame_less_helper_ = new NcFramelessHelper();
    frame_less_helper_->activeOnWithChildWidget(this, ui->tabWidget->tabBar());

    connect(ui->tabWidget, SIGNAL(TabInserted(int)), this, SLOT(OnTabInserted(int)));
    connect(ui->tabWidget, SIGNAL(AddBtnClicked()), this, SLOT(OnAddBtnClicked()));
    connect(ui->tabWidget, SIGNAL(TabClosed(int)), this, SLOT(OnCloseTab(int)));
    connect(ui->tabWidget, SIGNAL(closeWnd()), this, SLOT(OnCloseWnd()));
    connect(ui->tabWidget, SIGNAL(minWnd()), this, SLOT(OnMinWnd()));
    connect(ui->tabWidget, SIGNAL(maxWnd()), this, SLOT(OnMaxWnd()));
    connect(ui->tabWidget, SIGNAL(restoreWnd()), this, SLOT(OnRestoreWnd()));
    connect(ui->tabWidget, SIGNAL(tabBarDoubleClicked()), this, SLOT(OnMaxOrRestore()));
    ui->tabWidget->setTabBKImage(":/images/default_100_precent/tab.png",
                                ":/images/default_100_precent/tab_hover.png", 
                                ":/images/default_100_precent/tab_check.png");
    ui->tabWidget->setTabBarBKImage(":/images/default_100_precent/caption_bg.png");
    ui->tabWidget->setTabTextColor(Qt::white, Qt::white, Qt::white);
}

void MainWindow::OnTabInserted(int index)
{
    QPushButton *button = new QPushButton();
    button->setFixedSize(this->iconSize());
    button->setStyleSheet("border-image: url(:/images/x-capture-options.png);");
    ui->tabWidget->setTabButton2(index, QTabBar::LeftSide, button);

    button = new QPushButton();
    button->setStyleSheet("QPushButton{border-image: url(:/images/close.png)}"
                          "QPushButton:hover{border-image: url(:/images/close_hover.png)}");
    ui->tabWidget->setTabButton2(index, QTabBar::RightSide, button);
}

void MainWindow::OnTabClosed(int index)
{
    //todo something
}

void MainWindow::OnAddBtnClicked()
{
    ui->tabWidget->addTab2(new QWidget(), tr("this is first tab"));
}

void MainWindow::OnCloseWnd()
{
    this->close();
}

void MainWindow::OnMinWnd()
{
    this->showMinimized();
}

void MainWindow::OnMaxWnd()
{
    this->showMaximized();
    ui->tabWidget->setMaxRestoreBtnVisible(false, true);
    ui->tabWidget->currentWidget()->setFocus();
}

void MainWindow::OnRestoreWnd()
{
    this->showNormal();
    ui->tabWidget->setMaxRestoreBtnVisible(true, false);
}

void MainWindow::OnMaxOrRestore()
{
    if (isMaximized()) {
        this->showNormal();
        ui->tabWidget->setMaxRestoreBtnVisible(true, false);
    }
    else {
        this->showMaximized();
        ui->tabWidget->setMaxRestoreBtnVisible(false, true);
    }
}

3.属性设置

  • 增加无边框窗口功能(缩放,移动)
  • 增加tab贴图功能
  • 双击关闭tab
  • 可以自定义tab中的左右button。
  • 可以自定义tab中文字的颜色。
  • 可以设置是否需要绘制“+”按钮。

4.todo list

  • 支持tab拖拽
  • 将绘制接口导出来,方便用户自定义绘制和贴图

关注微信,获取源码。

你可能感兴趣的:(Qt实战记录,qt,ui,开发语言,QTabWidget,QTabBar)