Просмотр исходного кода

修改了新建标签页的显示

master
鹏鹏 李 20 часов назад
Родитель
Сommit
4f7615deb4
3 измененных файлов: 68 добавлений и 34 удалений
  1. +1
    -1
      editor.pro.user
  2. +65
    -33
      mainwindow.cpp
  3. +2
    -0
      mainwindow.h

+ 1
- 1
editor.pro.user Просмотреть файл

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.11.1, 2025-08-12T14:54:10. -->
<!-- Written by QtCreator 4.11.1, 2025-08-12T22:16:53. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>


+ 65
- 33
mainwindow.cpp Просмотреть файл

@@ -21,8 +21,6 @@ MainWindow::MainWindow(QWidget *parent)
, modbusManager(new ModbusManager(registerManager, this)) , modbusManager(new ModbusManager(registerManager, this))
{ {
ui->setupUi(this); ui->setupUi(this);
// registerManager = new RegisterManager(this);
// modbusManager = new ModbusManager(registerManager, this);
// 初始化PLC标签页 // 初始化PLC标签页
ui->plc_tab_widget->setTabsClosable(true); ui->plc_tab_widget->setTabsClosable(true);
ui->plc_tab_widget->setMovable(true); ui->plc_tab_widget->setMovable(true);
@@ -42,8 +40,8 @@ MainWindow::MainWindow(QWidget *parent)


createComponents(); createComponents();


connect(ui->plc_tab_widget, &QTabWidget::tabCloseRequested,this,&MainWindow::onTabCloseRequested);
connect(ui->hmi_tab_widget, &QTabWidget::tabCloseRequested,this,&MainWindow::onTabCloseRequested);
connect(ui->plc_tab_widget, &QTabWidget::tabCloseRequested, this, &MainWindow::onTabCloseRequested);
connect(ui->hmi_tab_widget, &QTabWidget::tabCloseRequested, this, &MainWindow::onTabCloseRequested);
connect(ui->listWidget,&QListWidget::currentTextChanged,this,&MainWindow::onListwidgetCurrenttextchanged); connect(ui->listWidget,&QListWidget::currentTextChanged,this,&MainWindow::onListwidgetCurrenttextchanged);


connect(ui->action_plc,&QAction::triggered,this,&MainWindow::plcChange); connect(ui->action_plc,&QAction::triggered,this,&MainWindow::plcChange);
@@ -64,7 +62,7 @@ MainWindow::MainWindow(QWidget *parent)
// this, &PLC::handleModbusError); // this, &PLC::handleModbusError);


ui->stackedWidget->setCurrentIndex(0); ui->stackedWidget->setCurrentIndex(0);
setWindowTitle("未命名项目 - PLC编辑器");
setWindowTitle("PLC编辑器");
} }


MainWindow::~MainWindow() MainWindow::~MainWindow()
@@ -90,6 +88,7 @@ void MainWindow::newPage(bool isPlc)
if (isPlc) { if (isPlc) {
// 添加到PLC标签页 // 添加到PLC标签页
int newIndex = ui->plc_tab_widget->addTab(newView, QString("页面 %1").arg(plcScenes.size() + 1)); int newIndex = ui->plc_tab_widget->addTab(newView, QString("页面 %1").arg(plcScenes.size() + 1));
plcPageTitles.append(""); // 初始化为空字符串
ui->plc_tab_widget->setCurrentIndex(newIndex); ui->plc_tab_widget->setCurrentIndex(newIndex);


// 保存场景和视图 // 保存场景和视图
@@ -102,6 +101,7 @@ void MainWindow::newPage(bool isPlc)
} else { } else {
// 添加到HMI标签页 // 添加到HMI标签页
int newIndex = ui->hmi_tab_widget->addTab(newView, QString("页面 %1").arg(hmiScenes.size() + 1)); int newIndex = ui->hmi_tab_widget->addTab(newView, QString("页面 %1").arg(hmiScenes.size() + 1));
hmiPageTitles.append(""); // 初始化为空字符串
ui->hmi_tab_widget->setCurrentIndex(newIndex); ui->hmi_tab_widget->setCurrentIndex(newIndex);


// 保存场景和视图 // 保存场景和视图
@@ -411,7 +411,7 @@ void MainWindow::plcChange()
currentIsPLC_ = true; currentIsPLC_ = true;
createComponents(); createComponents();
ui->stackedWidget->setCurrentIndex(0); // 显示PLC页面 ui->stackedWidget->setCurrentIndex(0); // 显示PLC页面
// setWindowTitle((plcFilePath_.isEmpty() ? "未命名项目" : QFileInfo(plcFilePath_).fileName()) + " - PLC编辑器");
setWindowTitle("PLC编辑器");
} }




@@ -420,18 +420,30 @@ void MainWindow::hmiChange()
currentIsPLC_ = false; currentIsPLC_ = false;
createComponents(); createComponents();
ui->stackedWidget->setCurrentIndex(1); // 显示HMI页面 ui->stackedWidget->setCurrentIndex(1); // 显示HMI页面
// setWindowTitle((hmiFilePath_.isEmpty() ? "未命名项目" : QFileInfo(hmiFilePath_).fileName()) + " - HMI编辑器");
setWindowTitle("HMI编辑器");
} }


void MainWindow::newProject() void MainWindow::newProject()
{ {
// 检查当前tabwidget是否只有一个页面且页面内容为空
if (currentIsPLC_ && plcScenes.size() == 1 && plcScenes[0]->items().isEmpty()) {
// 复用第一个页面
clearScene();
setWindowTitle("PLC编辑器");
return;
}
if (!currentIsPLC_ && hmiScenes.size() == 1 && hmiScenes[0]->items().isEmpty()) {
clearScene();
setWindowTitle("HMI编辑器");
return;
}
newPage(currentIsPLC_); newPage(currentIsPLC_);


// 更新窗口标题 // 更新窗口标题
if (currentIsPLC_) { if (currentIsPLC_) {
setWindowTitle("未命名项目 - PLC编辑器");
setWindowTitle("PLC编辑器");
} else { } else {
setWindowTitle("未命名项目 - HMI编辑器");
setWindowTitle("HMI编辑器");
} }
} }


@@ -452,16 +464,31 @@ void MainWindow::openProject()


Project project; Project project;
if (project.loadFromFile(filePath)) { if (project.loadFromFile(filePath)) {
// 在当前模块新建一个页面
newPage(currentIsPLC_);
int newPageIndex = currentPageIndex();
// // 在当前模块新建一个页面
// newPage(currentIsPLC_);
// int newPageIndex = currentPageIndex();
int newPageIndex = -1;
if (currentIsPLC_ && plcScenes.size() == 1 && plcScenes[0]->items().isEmpty()) {
newPageIndex = 0;
} else if (!currentIsPLC_ && hmiScenes.size() == 1 && hmiScenes[0]->items().isEmpty()) {
newPageIndex = 0;
} else {
newPage(currentIsPLC_);
newPageIndex = currentPageIndex();
}


// 将项目应用到新页面 // 将项目应用到新页面
applyProjectToScene(project, newPageIndex); applyProjectToScene(project, newPageIndex);


// 更新窗口标题
setWindowTitle(QFileInfo(filePath).fileName() +
(currentIsPLC_ ? " - PLC编辑器" : " - HMI编辑器"));
// 设置标签页标题为文件名
QString fileName = QFileInfo(filePath).fileName();
if (currentIsPLC_) {
plcPageTitles[newPageIndex] = fileName;
ui->plc_tab_widget->setTabText(newPageIndex, fileName);
} else {
hmiPageTitles[newPageIndex] = fileName;
ui->hmi_tab_widget->setTabText(newPageIndex, fileName);
}
} else { } else {
QMessageBox::critical(this, "错误", "无法打开项目文件"); QMessageBox::critical(this, "错误", "无法打开项目文件");
} }
@@ -495,9 +522,17 @@ void MainWindow::saveProject()
extractSceneToProject(project, currentPageIndex()); extractSceneToProject(project, currentPageIndex());


if (project.saveToFile(filePath)) { if (project.saveToFile(filePath)) {
// 更新窗口标题
setWindowTitle(QFileInfo(filePath).fileName() +
(currentIsPLC_ ? " - PLC编辑器" : " - HMI编辑器"));
// 设置标签页标题为文件名
QString fileName = QFileInfo(filePath).fileName();
int currentIndex = currentPageIndex();

if (currentIsPLC_) {
plcPageTitles[currentIndex] = fileName;
ui->plc_tab_widget->setTabText(currentIndex, fileName);
} else {
hmiPageTitles[currentIndex] = fileName;
ui->hmi_tab_widget->setTabText(currentIndex, fileName);
}
} else { } else {
QMessageBox::critical(this, "错误", "保存项目失败"); QMessageBox::critical(this, "错误", "保存项目失败");
} }
@@ -534,22 +569,19 @@ void MainWindow::onListwidgetCurrenttextchanged(const QString &currentText)


void MainWindow::onTabCloseRequested(int index) void MainWindow::onTabCloseRequested(int index)
{ {
if (currentIsPLC_) {
if (plcScenes.size() > 1) {
delete plcScenes.takeAt(index);
delete plcViews.takeAt(index);
ui->plc_tab_widget->removeTab(index);
} else {
QMessageBox::information(this, "提示", "至少保留一个PLC页面");
}
if (plcScenes.size() == 1) {
QMessageBox::information(this, "提示", "至少保留一个页面");
return;
}
if (currentIsPLC_)
{
plcPageTitles.removeAt(index);
delete plcScenes.takeAt(index);
delete plcViews.takeAt(index);
} else { } else {
if (hmiScenes.size() > 1) {
delete hmiScenes.takeAt(index);
delete hmiViews.takeAt(index);
ui->hmi_tab_widget->removeTab(index);
} else {
QMessageBox::information(this, "提示", "至少保留一个HMI页面");
}
hmiPageTitles.removeAt(index);
delete hmiScenes.takeAt(index);
delete hmiViews.takeAt(index);
} }
} }




+ 2
- 0
mainwindow.h Просмотреть файл

@@ -49,10 +49,12 @@ private:
// PLC状态 // PLC状态
QList<QGraphicsScene*> plcScenes; QList<QGraphicsScene*> plcScenes;
QList<MyGraphicsView*> plcViews; QList<MyGraphicsView*> plcViews;
QStringList plcPageTitles;


// HMI状态 // HMI状态
QList<QGraphicsScene*> hmiScenes; QList<QGraphicsScene*> hmiScenes;
QList<MyGraphicsView*> hmiViews; QList<MyGraphicsView*> hmiViews;
QStringList hmiPageTitles;


QString selectedComponentType; QString selectedComponentType;
bool currentIsPLC_ = true; bool currentIsPLC_ = true;


Загрузка…
Отмена
Сохранить