Qt TabWidget添加多个窗口窗体,可关闭与打开
点击按钮可判断是否打开,避免重复打开
使用Qt中的TabWidget组件创建一个简单的分页窗体布局。点击按钮时,会新增一个窗体并添加到TabWidget中。每个子窗体能动态获取父窗体指针以进行操作
分别创建一个mainwindow和两个widget窗体文件
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
void closeEvent(QCloseEvent* e);
int IsOpened(QString title);
QString GetTableNumber();
private slots:
void on_pushButton_clicked();
void on_tabWidget_tabCloseRequested(int index);
void on_tabWidget_currentChanged(int index);
private:
Ui::MainWindow *ui;
int cur = 1;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "form2.h"
#include "form3.h"
#include <QMessageBox>
#include <QPushButton>
#include <QToolButton>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tabWidget->setVisible(true);
ui->tabWidget->clear();//清除所有页面
//ui->tabWidget->tabsClosable(); //Page有关闭按钮,可被关闭
ui->tabWidget->setTabsClosable(true);
QPushButton *btn1 = new QPushButton;
btn1->setText("开始");
btn1->setIcon(QIcon(":/new/prefix1/button_Log_icon2.ico"));
// ui->toolBar->addWidget(btn1);
QPushButton *btn2 = new QPushButton;
btn2->setText("暂停");
// ui->toolBar->addWidget(btn2);
QToolButton *tbtn = new QToolButton;
tbtn->setText("复位");
tbtn->setIcon(QIcon(":/new/prefix1/button_Log_icon2.ico"));
tbtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); //图像在上,文字在下
ui->toolBar->addWidget(tbtn);
QToolButton *tbtn2 = new QToolButton;
tbtn2->setText("设置");
tbtn2->setIcon(QIcon(":/new/prefix1/best_quality_2.png"));
tbtn2->setToolButtonStyle(Qt::ToolButtonTextUnderIcon); //图像在上,文字在下
ui->toolBar->addWidget(tbtn2);
connect(tbtn,&QToolButton::clicked,this,[=]{
Form2 *ptr =new Form2(this);//关闭时自动销毁
ptr->setAttribute(Qt::WA_DeleteOnClose);
Form3 *ptr3 =new Form3(this);//关闭时自动销毁
ptr3->setAttribute(Qt::WA_DeleteOnClose);
int tb_index =IsOpened(tbtn->text());
if(tb_index>=0)
{
ui->tabWidget->setCurrentIndex(tb_index) ;
return;
}
// if(cur%2==0)
{
cur = ui->tabWidget->addTab(ptr,tbtn->text());//QString::asprintf("192.168.1.%d",ui->tabWidget->count()));
}
// else
{
//cur = ui->tabWidget->addTab(ptr3,QString::asprintf("test%d",ui->tabWidget->count()));
}
ui->tabWidget->setTabIcon(cur,QIcon(":/new/prefix1/button_Log_icon2.ico"));
ui->tabWidget->setCurrentIndex(cur) ;
});
connect(tbtn2,&QToolButton::clicked,this,[=]{
Form2 *ptr =new Form2(this);//关闭时自动销毁
ptr->setAttribute(Qt::WA_DeleteOnClose);
Form3 *ptr3 =new Form3(this);//关闭时自动销毁
ptr3->setAttribute(Qt::WA_DeleteOnClose);
int tb_index =IsOpened(tbtn2->text());
if(tb_index>=0)
{
ui->tabWidget->setCurrentIndex(tb_index) ;
return;
}
// if(cur%2==0)
{
// cur = ui->tabWidget->addTab(ptr,QString::asprintf("192.168.1.%d",ui->tabWidget->count()));
}
// else
{
cur = ui->tabWidget->addTab(ptr3,tbtn2->text());//QString::asprintf("test%d",ui->tabWidget->count()));
}
ui->tabWidget->setTabIcon(cur,QIcon(":/new/prefix1/best_quality_2.png"));
ui->tabWidget->setCurrentIndex(cur) ;
});
}
/**********************************************
* @projectName %{CurrentProject:Name}
* @brief function_name...
* @param void
* @return void
* @author cdtxw@foxmail.com/ by txwtech
* @date 2024-09-05
**********************************************/
// 定义函数来获取当前Table名字
QString MainWindow::GetTableNumber()
{
QString ref = QString(ui->tabWidget->currentIndex());
return ref;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeEvent(QCloseEvent* e)
{
QMessageBox::information(this,"title","main window will be closed");
}
/**********************************************
* @projectName %{CurrentProject:Name}
* @brief 是否打开
* @param void
* @return void
* @author cdtxw@foxmail.com/ by txwtech
* @date 2024-09-05
**********************************************/
int MainWindow::IsOpened(QString title)
{
int tab_count = ui->tabWidget->count();
int window_count =0;
if(window_count==0)
{
window_count = -1;
}
for(int x=0;x<tab_count;x++)//获取出每个菜单的标题
{
QString tab_name = ui->tabWidget->tabText(x);
if(tab_name==title)
{
window_count = x;
break;
}
else if(x==tab_count)
{
window_count = -1;
}
}
return window_count;
}
/**********************************************
* @projectName %{CurrentProject:Name}
* @brief 添加窗口
* @param void
* @return void
* @author cdtxw@foxmail.com/ by txwtech
* @date 2024-09-05
**********************************************/
void MainWindow::on_pushButton_clicked()
{
//新建选项卡
Form2 *ptr =new Form2(this);//关闭时自动销毁
ptr->setAttribute(Qt::WA_DeleteOnClose);
Form3 *ptr3 =new Form3(this);//关闭时自动销毁
ptr3->setAttribute(Qt::WA_DeleteOnClose);
if(cur%2==0)
{
cur = ui->tabWidget->addTab(ptr,QString::asprintf("192.168.1.%d",ui->tabWidget->count()));
}
else
{
cur = ui->tabWidget->addTab(ptr3,QString::asprintf("test%d",ui->tabWidget->count()));
}
ui->tabWidget->setTabIcon(cur,QIcon(":/new/prefix1/button_Log_icon2.ico"));
ui->tabWidget->setCurrentIndex(cur) ;
ui->tabWidget->setVisible(true);
}
void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
if (index<0)
return;
auto aa= QMessageBox::question(this,"tips","window will be closed",QMessageBox::Yes,QMessageBox::No);
if(aa==QMessageBox::No)
{
return;
}
QWidget* aForm=ui->tabWidget->widget(index);
aForm->close();
}
void MainWindow::on_tabWidget_currentChanged(int index)//
// 在无Tab页面是默认禁用
{
Q_UNUSED(index);
bool en=ui->tabWidget->count()>0;
// ui->tabWidget->setVisible(en);
}
工程代码: