最近项目需要做一个类似于浏览器标签的界面,方便用户知道自己打开过哪个窗口,所以这里整理了一下思路,写成一个小demo,希望能为类似需求的童鞋提供一点思路。废话不多:上代码
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form.h"
#include "form2.h"
#include "form3.h"
#include "form4.h"
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tabWidget->setUsesScrollButtons(true);
ui->tabWidget->setTabsClosable(true);//可以关闭tab
ui->tabWidget->setMovable(true);//可以移动
on_pushButton_clicked();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
//只是删除了tab这里不需要调用了。因为close动作内部已经自己删除tab
//ui->tabWidget->removeTab(index);
qDebug()<<"sss "<
QWidget *pItemWidget = ui->tabWidget->widget(index);
if (0 != pItemWidget)
{
delete pItemWidget;
pItemWidget = NULL;
}
}
void MainWindow::on_pushButton_clicked()
{
QWidget *widget = NULL;
widget = HasWidget(0);
if(widget == NULL)
{
Form *f = new Form();
f->setProperty("id",0);
ui->tabWidget->addTab(f,"行情");
ui->tabWidget->setCurrentWidget(f);
}else
{
ui->tabWidget->setCurrentWidget(widget);
}
}
void MainWindow::on_pushButton_2_clicked()
{
QWidget *widget = NULL;
widget = HasWidget(1);
if(widget == NULL)
{
Form2 *f = new Form2();
f->setProperty("id",1);
ui->tabWidget->addTab(f,"交易");
ui->tabWidget->setCurrentWidget(f);
}else
{
ui->tabWidget->setCurrentWidget(widget);
}
}
void MainWindow::on_pushButton_3_clicked()
{
QWidget *widget = NULL;
widget = HasWidget(2);
if(widget == NULL)
{
Form3 *f = new Form3();
f->setProperty("id",2);
ui->tabWidget->addTab(f,"咨询");
ui->tabWidget->setCurrentWidget(f);
}else
{
ui->tabWidget->setCurrentWidget(widget);
}
}
void MainWindow::on_pushButton_4_clicked()
{
QWidget *widget = NULL;
widget = HasWidget(3);
if(widget == NULL)
{
Form4 *f = new Form4();
f->setProperty("id",3);
ui->tabWidget->addTab(f,"账号");
ui->tabWidget->setCurrentWidget(f);
}else
{
ui->tabWidget->setCurrentWidget(widget);
}
}
QWidget *MainWindow::HasWidget(int id)
{
QList
qDebug()<<"2132132 "< QWidget *widget = NULL;
foreach (QWidget *w, list) {
if(w->property("id") == id)
{
widget = w;
break;
}
}
return widget;
}
**qss:**
QTabBar::tab::selected{background-color:rgb(85,158,251);}
QTabBar::tab::!selected{background-color:white;}
QTabBar::tab::hover{background-color:rgb(255, 255, 127);}
QTabWidget::pane{
border-width:2px;
border-top-left-radius:15px;
border-color:rgb(214,241,214);
border-style: inset;
background-color: white;
}