//添加
int addTab(QWidget *page, const QString &label)
//设置tab位置
void setTabPosition(QTabWidget::TabPosition)
//设置tab的形状
void setTabShape(QTabWidget::TabShape s)
这里简单的添加两个tab
m_tabWidget = new QTabWidget(this);
ui->horizontalLayout->addWidget(m_tabWidget);
//add page1
QFrame *page1 = new QFrame(m_tabWidget);
QHBoxLayout *h1 = new QHBoxLayout(page1);
QLabel *labelName = new QLabel(page1);
labelName->setText("姓名: ");
h1->addWidget(labelName);
QLineEdit *le1 = new QLineEdit(page1);
h1->addWidget(le1);
//add page2
QFrame *page2 = new QFrame(m_tabWidget);
QHBoxLayout *h2 = new QHBoxLayout(page2);
QLabel *labelName2 = new QLabel(page2);
labelName2->setText("性别: ");
h2->addWidget(labelName2);
QComboBox *cb2 = new QComboBox(page2);
cb2->addItems(QStringList()<<"男"<<"女");
h2->addWidget(cb2);
m_tabWidget->addTab(page1,"姓名");
m_tabWidget->addTab(page2,"性别");
运行截图:
这里,只做了一个右键删除当前page页
//开启右键菜单
m_tabWidget->setContextMenuPolicy(Qt::CustomContextMenu);
m_menu = new QMenu(this);
m_actionAdd = new QAction("添加",m_menu);
connect(m_actionAdd,&QAction::triggered,this,&Form::slotAdd);
m_actionDel = new QAction("删除",m_menu);
connect(m_actionDel,&QAction::triggered,this,&Form::slotDel);
m_menu->addAction(m_actionAdd);
m_menu->addAction(m_actionDel);
connect(m_tabWidget,&QTabWidget::customContextMenuRequested,this,&Form::slotPopMenu);
void Form::slotPopMenu(const QPoint &pos)
{
m_menu->exec(m_tabWidget->mapToGlobal(pos));
}
void Form::slotAdd()
{
//add page
}
void Form::slotDel()
{
m_tabWidget->removeTab(m_tabWidget->currentIndex());
}
以下style仅供参考
const QString styles = "\
QTabWidget::pane\
{\
border-top: 2px solid #C2C7CB;\
}\
QTabWidget::tab-bar\
{\
left: 5px;\
}\
QTabBar::tab\
{\
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,\
stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);\
border: 2px solid #C4C4C3;\
border-bottom-color: #C2C7CB;\
border-top-left-radius: 4px;\
border-top-right-radius: 4px;\
min-width: 8ex;\
padding: 2px;\
}\
QTabBar::tab:selected, QTabBar::tab:hover\
{\
background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,\
stop: 0 #fafafa, stop: 0.4 #f4f4f4,\
stop: 0.5 #e7e7e7, stop: 1.0 #fafafa);\
}\
QTabBar::tab:selected\
{\
border-color: #9B9B9B;\
border-bottom-color: #C2C7CB;\
}\
QTabBar::tab:!selected\
{\
margin-top: 2px;\
}";
源码:https://download.csdn.net/download/wzz953200463/13188035