当前位置: 首页 > news >正文

Qt多弹窗实现包括QDialog、QWidget、QMainWindow

1.相关说明

独立Widget窗口、嵌入式Widget、嵌入式MainWindow窗口、独立MainWindow窗口等弹窗的实现

相关界面包含关系

2.相关界面

3.相关代码

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "tformdoc.h"
#include "tformtable.h"#include <QPainter>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// setCentralWidget(ui->tabWidget);ui->tabWidget->setVisible(false);ui->tabWidget->clear();ui->tabWidget->setTabsClosable(true);}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::do_changeTabTitle(QString title)
{int index = ui->tabWidget->currentIndex();ui->tabWidget->setTabText(index, title);
}void MainWindow::on_actionWidgetInsite_triggered()
{TFormDoc *formDoc = new TFormDoc(this);formDoc->setAttribute(Qt::WA_DeleteOnClose);int cur = ui->tabWidget->addTab(formDoc, QString::asprintf("Doc %d", ui->tabWidget->count()));ui->tabWidget->setCurrentIndex(cur);ui->tabWidget->setVisible(true);connect(formDoc, &TFormDoc::titleChanged, this, &MainWindow::do_changeTabTitle);
}
// tab的关闭
void MainWindow::on_tabWidget_tabCloseRequested(int index)
{ui->tabWidget->widget(index)->close();
}void MainWindow::paintEvent(QPaintEvent *event)
{QPainter painter(this);painter.drawPixmap(0, ui->toolBar->height(),width(), height()-ui->toolBar->height()-ui->statusbar->height(),QPixmap(":/jpg/jpg/shenli.jpg"));
}// 独立Widget窗口
void MainWindow::on_actionWidgetAlone_triggered()
{TFormDoc *formDoc = new TFormDoc();formDoc->setAttribute(Qt::WA_DeleteOnClose);formDoc->setWindowTitle("基于QWidget的窗口");formDoc->setWindowOpacity(0.8);formDoc->show();
}// 嵌入式MainWindow窗口
void MainWindow::on_actionMainWindowInsite_triggered()
{TFormTable *formTable = new TFormTable(this);formTable->setAttribute(Qt::WA_DeleteOnClose);int cur = ui->tabWidget->addTab(formTable, QString::asprintf("Table %d", ui->tabWidget->count()));ui->tabWidget->setCurrentIndex(cur);ui->tabWidget->setVisible(true);
}
// 独立式MainWindow窗口
void MainWindow::on_actionMainWindowAlone_triggered()
{TFormTable *formTable = new TFormTable();formTable->setAttribute(Qt::WA_DeleteOnClose);formTable->setWindowTitle("独立的window");formTable->show();
}

tformdoc.cpp

#include "tformdoc.h"
#include "ui_tformdoc.h"
#include <QToolBar>
#include <QFileDialog>
#include <QFontDialog>
#include <QVBoxLayout>
TFormDoc::TFormDoc(QWidget *parent): QWidget(parent), ui(new Ui::TFormDoc)
{ui->setupUi(this);QToolBar *locToolBar = new QToolBar("文档", this);locToolBar->addAction(ui->actionOpenFile);locToolBar->addAction(ui->actionFont);locToolBar->addSeparator();locToolBar->addAction(ui->actionCut);locToolBar->addAction(ui->actionCopy);locToolBar->addAction(ui->actionPatse);locToolBar->addAction(ui->actionRedo);locToolBar->addAction(ui->actionUndo);locToolBar->addSeparator();locToolBar->addAction(ui->actionClose);locToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);QVBoxLayout *layout = new QVBoxLayout(this);layout->addWidget(locToolBar);layout->addWidget(ui->plainTextEdit);this->setLayout(layout);
}TFormDoc::~TFormDoc()
{delete ui;
}void TFormDoc::on_actionOpenFile_triggered()
{QString curPath = QCoreApplication::applicationDirPath();QString aFileName = QFileDialog::getOpenFileName(this, "打开一个文件", curPath, "(*.h *.cpp);;(*.txt);;(*.*)");if(aFileName.isEmpty()){return;}QFile aFile(aFileName);if(aFile.open(QIODevice::ReadOnly | QIODevice::Text)){ui->plainTextEdit->clear();QTextStream aStream(&aFile);while(!aStream.atEnd()){QString str = aStream.readLine();ui->plainTextEdit->appendPlainText(str);}aFile.close();QFileInfo fileInfo(aFileName);QString shortName = fileInfo.fileName();setWindowTitle(shortName);emit titleChanged(shortName);}}void TFormDoc::on_actionFont_triggered()
{QFont font = ui->plainTextEdit->font();bool ok;font = QFontDialog::getFont(&ok, font);if(ok){ui->plainTextEdit->setFont(font);}
}void TFormDoc::on_actionClose_triggered()
{this->close();
}

tformtable.cpp

#include "tformtable.h"
#include "ui_TFormTable.h"
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include <QMessageBox>
#include <QLabel>
#include "tdialogsize.h"
#include "tdialogheaders.h"
#include "tdialoglocate.h"TFormTable::TFormTable(QWidget *parent): QMainWindow(parent), ui(new Ui::TFormTable)
{ui->setupUi(this);dlgHeaders = NULL; // 初始化m_model = new QStandardItemModel(4, 4, this);QStringList header;header << "姓名" << "性别" << "学位" << "部门";m_model->setHorizontalHeaderLabels(header);m_selection = new QItemSelectionModel(m_model);ui->tableView->setModel(m_model);ui->tableView->setSelectionModel(m_selection);setCentralWidget(ui->tableView);labCellPos = new QLabel("当前单元:", this);labCellPos->setMinimumWidth(200);labCellText = new QLabel("单元格内容:", this);labCellText->setMinimumWidth(200);ui->statusbar->addWidget(labCellPos);ui->statusbar->addWidget(labCellText);connect(m_selection, &QItemSelectionModel::currentChanged, this, &TFormTable::do_model_currentChanged);
}TFormTable::~TFormTable()
{delete ui;}// void TFormTable::on_actionSetRowCol_triggered()
// {
//     TDialogSize *dlgTableSize = new TDialogSize();
//     dlgTableSize->setWindowFlag(Qt::MSWindowsFixedSizeDialogHint);
//     dlgTableSize->setRowColumn(m_model->rowCount(), m_model->columnCount());
//     int ret = dlgTableSize->exec();
//     if(ret == QDialog::Accepted){
//         m_model->setColumnCount(dlgTableSize->columnCount());
//         m_model->setRowCount(dlgTableSize->rowCount());
//     }
//     delete dlgTableSize;
// }void TFormTable::on_actionSetRowCol_triggered()
{TDialogSize dlgTableSize;dlgTableSize.setWindowFlag(Qt::MSWindowsFixedSizeDialogHint);dlgTableSize.setRowColumn(m_model->rowCount(), m_model->columnCount());int ret = dlgTableSize.exec();if(ret == QDialog::Accepted){m_model->setColumnCount(dlgTableSize.columnCount());m_model->setRowCount(dlgTableSize.rowCount());}// delete dlgTableSize;
}void TFormTable::on_actionSetHeader_triggered()
{if(dlgHeaders == NULL){dlgHeaders = new TDialogHeaders(this);}QStringList strList;for(int i = 0; i < m_model->columnCount(); i++){strList.append(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());}dlgHeaders->setHeaderList(strList);int ret = dlgHeaders->exec();if(ret == QDialog::Accepted){m_model->setHorizontalHeaderLabels(dlgHeaders->headerList());}
}void TFormTable::do_setCellText(int row, int column, QString text)
{QModelIndex index = m_model->index(row, column);m_selection->clearSelection();m_selection->setCurrentIndex(index, QItemSelectionModel::Select);m_model->setData(index, text, Qt::DisplayRole);
}void TFormTable::on_actionLocateUnit_triggered()
{TDialogLocate *dlgLocate = new TDialogLocate(this);dlgLocate->setAttribute(Qt::WA_DeleteOnClose);dlgLocate->setWindowFlag(Qt::WindowStaysOnTopHint);// 初始化对话框dlgLocate->setSpinRange(m_model->rowCount(), m_model->columnCount());QModelIndex curIndex = m_selection->currentIndex();if(curIndex.isValid()){dlgLocate->setSpinValue(curIndex.row(), curIndex.column());}dlgLocate->show();dlgLocate->setModal(false);connect(dlgLocate, &TDialogLocate::changeCellText, this, &TFormTable::do_setCellText);connect(dlgLocate, &TDialogLocate::changeActionEnable, ui->actionLocateUnit, &QAction::setEnabled);connect(this, &TFormTable::cellIndexChanged, dlgLocate, &TDialogLocate::setSpinValue);}void TFormTable::on_tableView_clicked(const QModelIndex &index)
{emit cellIndexChanged(index.row(), index.column());
}void TFormTable::do_model_currentChanged(const QModelIndex &current, const QModelIndex &previous)
{Q_UNUSED(previous);if(current.isValid()){labCellPos->setText(QString::asprintf("当前单元格:%d行,%d列",current.row(), current.column()));QStandardItem *aItem = m_model->itemFromIndex(current);labCellText->setText("单元格内容:" + aItem->text());}
}

tdialogheaders.cpp

#include "tdialogheaders.h"
#include "ui_tdialogheaders.h"
#include <QStringListModel>TDialogHeaders::TDialogHeaders(QWidget *parent): QDialog(parent), ui(new Ui::TDialogHeaders)
{ui->setupUi(this);m_model = new QStringListModel(this);ui->listView->setModel(m_model);
}TDialogHeaders::~TDialogHeaders()
{delete ui;
}void TDialogHeaders::setHeaderList(QStringList &headers)
{m_model->setStringList(headers);
}QStringList TDialogHeaders::headerList()
{return m_model->stringList();
}

tdialoglocate.cpp

#include "tdialoglocate.h"
#include "ui_tdialoglocate.h"
#include <QCloseEvent>
#include <QShowEvent>TDialogLocate::TDialogLocate(QWidget *parent): QDialog(parent), ui(new Ui::TDialogLocate)
{ui->setupUi(this);
}TDialogLocate::~TDialogLocate()
{delete ui;
}void TDialogLocate::setSpinRange(int rowCount, int colCount)
{ui->spinBoxRow->setMaximum(rowCount-1);ui->spinBoxCol->setMaximum(colCount-1);
}void TDialogLocate::setSpinValue(int rowNo, int colNo)
{ui->spinBoxRow->setValue(rowNo);ui->spinBoxCol->setValue(colNo);
}// 设定文字
void TDialogLocate::on_btnSetText_clicked()
{QString text = ui->lineEdit->text();int row = ui->spinBoxRow->value();int col = ui->spinBoxCol->value();if(ui->chkIncRow->isChecked()){ui->spinBoxRow->setValue(ui->spinBoxRow->value()+1);}if(ui->chkIncCol->isChecked()){ui->spinBoxCol->setValue(ui->spinBoxCol->value()+1);}emit changeCellText(row, col, text);
}void TDialogLocate::closeEvent(QCloseEvent *event)
{event->accept();emit changeActionEnable(true);
}void TDialogLocate::showEvent(QShowEvent *event)
{event->accept();emit changeActionEnable(false);
}

tdialogsize.cpp

#include "tdialogsize.h"
#include "ui_tdialogsize.h"
#include <QMessageBox>TDialogSize::TDialogSize(QWidget *parent): QDialog(parent), ui(new Ui::TDialogSize)
{ui->setupUi(this);
}TDialogSize::~TDialogSize()
{delete ui;QMessageBox::information(this, "提示", "TDialogSize释放资源");
}void TDialogSize::setRowColumn(int row, int column)
{ui->spinBoxRow->setValue(row);ui->spinBoxCol->setValue(column);
}int TDialogSize::rowCount()
{return ui->spinBoxRow->value();
}int TDialogSize::columnCount()
{return ui->spinBoxCol->value();
}

相关文章:

Qt多弹窗实现包括QDialog、QWidget、QMainWindow

1.相关说明 独立Widget窗口、嵌入式Widget、嵌入式MainWindow窗口、独立MainWindow窗口等弹窗的实现 相关界面包含关系 2.相关界面 3.相关代码 mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include "tformdoc.h" #incl…...

Django高级之-forms组件

Django高级之-forms组件 1 校验字段功能 针对一个实例&#xff1a;注册用户讲解。 模型&#xff1a;models.py class UserInfo(models.Model):namemodels.CharField(max_length32)pwdmodels.CharField(max_length32)emailmodels.EmailField()模版文件 <!DOCTYPE html&g…...

GPT实战系列-LangChain实现简单链

GPT实战系列-LangChain实现简单链 LangChain GPT实战系列-LangChain如何构建基通义千问的多工具链 GPT实战系列-构建多参数的自定义LangChain工具 GPT实战系列-通过Basetool构建自定义LangChain工具方法 GPT实战系列-一种构建LangChain自定义Tool工具的简单方法 GPT实战系…...

关于tomcat服务器配置及性能优化的20道高级面试题

1. 请描述Tomcat服务器的基本架构和组件。 Tomcat服务器的基本架构主要包括Server、Service、Connector和Container等组件。具体来看&#xff1a; Server&#xff1a;是Tomcat中最顶层的容器&#xff0c;代表着整个服务器。它负责运行Tomcat服务器&#xff0c;例如打开和关闭…...

LeetCode 1315.祖父节点值为偶数的节点和

给你一棵二叉树&#xff0c;请你返回满足以下条件的所有节点的值之和&#xff1a; 该节点的祖父节点的值为偶数。&#xff08;一个节点的祖父节点是指该节点的父节点的父节点。&#xff09; 如果不存在祖父节点值为偶数的节点&#xff0c;那么返回 0 。 示例&#xff1a; 输入…...

C语言分支和循环总结

文章目录 概要结构介绍不同结构的语句简单运用小结 概要 C语言中分为三种结构&#xff1a;顺序结构&#xff0c;选择结构&#xff0c;循环结构 结构介绍 顺序结构就是从上到下&#xff0c;从左到右等等&#xff1b;选择结构可以想象是Y字路口就是到了一个地方会有不同的道路…...

【Echarts】曲线图上方显示数字以及自定义值,标题和副标题居中,鼠标上显示信息以及自定义信息

欢迎来到《小5讲堂》 大家好&#xff0c;我是全栈小5。 这是《前端》系列文章&#xff0c;每篇文章将以博主理解的角度展开讲解&#xff0c; 特别是针对知识点的概念进行叙说&#xff0c;大部分文章将会对这些概念进行实际例子验证&#xff0c;以此达到加深对知识点的理解和掌握…...

双环PID控制详细讲解

参考博客&#xff1a; &#xff08;1&#xff09;PID双环控制&#xff08;速度环和位置环&#xff09; &#xff08;2&#xff09;PID控制&#xff08;四&#xff09;&#xff08;单环与双环PID&#xff09; &#xff08;3&#xff09;内外双环pid算法 0 单环PID 目标位置→系…...

深入解析Java内存模型

一、背景 并发编程本质问题是&#xff1a;CPU、内存以及IO三者之间的速度差异。CPU速度快于内存、内存访问速度又远远快于IO&#xff0c;根据木桶理论&#xff0c;程序性能取决于最慢的操作&#xff0c;即IO操作。这样会出现CPU和内存交互时&#xff0c;CPU性能无法被充分利用…...

python使用国内镜像源

使用格式 格式为&#xff1a;pip install 库名 -i 镜像地址&#xff08;注意空格的存在&#xff09; pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple 推荐的镜像源&#xff1a; 清华大学&#xff08;推荐&#xff09;&#xff1a;https://pypi.tuna.tsing…...

【动态规划】代码随想录算法训练营第四十六天 |139.单词拆分,关于多重背包,你该了解这些! ,背包问题总结篇!(待补充)

139.单词拆分 1、题目链接&#xff1a;. - 力扣&#xff08;LeetCode&#xff09; 2、文章讲解&#xff1a;代码随想录 3、题目&#xff1a; 给定一个非空字符串 s 和一个包含非空单词的列表 wordDict&#xff0c;判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词…...

WordPress建站入门教程:如何选择和设置固定链接结构?

我们成功搭建好WordPress网站后&#xff0c;发布的文章对应的URL地址默认是使用“日期和名称型”&#xff0c;即是网站域名跟着的是年月日&#xff0c;最后是文章标题&#xff0c;如http://www.yigujin.com/2024/03/06/免费响应式WordPress博客主题JianYue/ 为了让我们的文章U…...

一款好用的AI工具——边界AICHAT(三)

目录 3.23、文档生成PPT演示3.24、AI文档翻译3.25、AI翻译3.26、论文模式3.27、文章批改3.28、文章纠正3.29、写作助手3.30、文言文翻译3.31、日报周报月报生成器3.32、OCR-DOC办公文档识别3.33、AI真人语音合成3.34、录音音频总结3.35、域方模型市场3.36、模型创建3.37、社区交…...

编程示例: 矩阵的多项式计算以javascript语言为例

编程示例: 矩阵的多项式计算以javascript语言为例 国防工业出版社的《矩阵理论》一书中第一章第8个习题 试计算2*A^8-3*A^5A^4A^2-4I A[[1,0,2],[0,-1,1],[0,1,0]] 代码如下 <html> <head> <title> 矩阵乘法 </title> <script srcset.js ><…...

project generator 简单使用

文章目录 1 progen 资源2 使用简介2.1 安装2.2 添加 target&#xff08;可选&#xff09;2.3 替换 CMake 模板&#xff08;可选&#xff09;2.4 创建 progen 项目 3 总结 1 progen 资源 0&#xff09;简介&#xff1a;progen&#xff08;project-generator&#xff0c;项目生成…...

C语言 —— 图形打印

题目1&#xff1a; 思路&#xff1a; 如果我们要打印一个实心正方形&#xff0c;其实就是一个二维数组&#xff0c;i控制行&#xff0c;j控制列&#xff0c;行列不需要控制&#xff0c;arr[i][j]直接打印星号即可。 对于空心正方形&#xff0c;我们只需要控制行和列的条件&…...

Python基础学习(11)常用模块

文章目录 一、time二、random三、os四、sys五、json补充1&#xff1a;JSON字符串补充2&#xff1a;JSON字符串和字典的区别 六、hashlib Python基础学习(1)基本知识 Python基础学习(2)序列类型方法与数据类型转换 Python基础学习(3)进阶字符串(格式化输出) Python基础学习(4)散…...

嵌入式学习37-TCP并发模型

TCP并发模型: 1.TCP多线程模型: 缺点: 1.创建线程会带来 资源开销 2.能够实现的 并发量 比较有限 2.IO模型: 1.阻塞IO: 没有…...

C语言字符函数和字符串函数

前言 今天这篇博客咱们一起来认识一些特殊的函数&#xff0c;在编程的过程中&#xff0c;我们经常要处理字符和字符串&#xff0c;为了方便字符和字符串&#xff0c;C语言提供了一些库函数&#xff0c;让我们一起看看这些函数都有什么功能吧&#xff01;&#xff01;&#xff0…...

Go语言必知必会100问题-22 空切片与nil切片有区别吗?

空切片与nil切片有区别吗&#xff1f; 很多开发人员经常混淆nil切片和空切片,不清楚什么时候使用空切片什么时候使用nil&#xff0c;而有些库函数又对这两者使用进行了区分。下面先来看看它们的定义。 空切片是length为0的切片当切片等于nil时为nil切片 下面是几种不同空切片…...

利用最小二乘法找圆心和半径

#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …...

【Linux】shell脚本忽略错误继续执行

在 shell 脚本中&#xff0c;可以使用 set -e 命令来设置脚本在遇到错误时退出执行。如果你希望脚本忽略错误并继续执行&#xff0c;可以在脚本开头添加 set e 命令来取消该设置。 举例1 #!/bin/bash# 取消 set -e 的设置 set e# 执行命令&#xff0c;并忽略错误 rm somefile…...

Golang 面试经典题:map 的 key 可以是什么类型?哪些不可以?

Golang 面试经典题&#xff1a;map 的 key 可以是什么类型&#xff1f;哪些不可以&#xff1f; 在 Golang 的面试中&#xff0c;map 类型的使用是一个常见的考点&#xff0c;其中对 key 类型的合法性 是一道常被提及的基础却很容易被忽视的问题。本文将带你深入理解 Golang 中…...

【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例

文章目录 ★ position 的五种类型及基本用法 ★ 一、position 属性概述 二、position 的五种类型详解(初学者版) 1. static(默认值) 2. relative(相对定位) 3. absolute(绝对定位) 4. fixed(固定定位) 5. sticky(粘性定位) 三、定位元素的层级关系(z-i…...

Mac软件卸载指南,简单易懂!

刚和Adobe分手&#xff0c;它却总在Library里给你写"回忆录"&#xff1f;卸载的Final Cut Pro像电子幽灵般阴魂不散&#xff1f;总是会有残留文件&#xff0c;别慌&#xff01;这份Mac软件卸载指南&#xff0c;将用最硬核的方式教你"数字分手术"&#xff0…...

【单片机期末】单片机系统设计

主要内容&#xff1a;系统状态机&#xff0c;系统时基&#xff0c;系统需求分析&#xff0c;系统构建&#xff0c;系统状态流图 一、题目要求 二、绘制系统状态流图 题目&#xff1a;根据上述描述绘制系统状态流图&#xff0c;注明状态转移条件及方向。 三、利用定时器产生时…...

【Web 进阶篇】优雅的接口设计:统一响应、全局异常处理与参数校验

系列回顾&#xff1a; 在上一篇中&#xff0c;我们成功地为应用集成了数据库&#xff0c;并使用 Spring Data JPA 实现了基本的 CRUD API。我们的应用现在能“记忆”数据了&#xff01;但是&#xff0c;如果你仔细审视那些 API&#xff0c;会发现它们还很“粗糙”&#xff1a;有…...

【python异步多线程】异步多线程爬虫代码示例

claude生成的python多线程、异步代码示例&#xff0c;模拟20个网页的爬取&#xff0c;每个网页假设要0.5-2秒完成。 代码 Python多线程爬虫教程 核心概念 多线程&#xff1a;允许程序同时执行多个任务&#xff0c;提高IO密集型任务&#xff08;如网络请求&#xff09;的效率…...

Unit 1 深度强化学习简介

Deep RL Course ——Unit 1 Introduction 从理论和实践层面深入学习深度强化学习。学会使用知名的深度强化学习库&#xff0c;例如 Stable Baselines3、RL Baselines3 Zoo、Sample Factory 和 CleanRL。在独特的环境中训练智能体&#xff0c;比如 SnowballFight、Huggy the Do…...

Aspose.PDF 限制绕过方案:Java 字节码技术实战分享(仅供学习)

Aspose.PDF 限制绕过方案&#xff1a;Java 字节码技术实战分享&#xff08;仅供学习&#xff09; 一、Aspose.PDF 简介二、说明&#xff08;⚠️仅供学习与研究使用&#xff09;三、技术流程总览四、准备工作1. 下载 Jar 包2. Maven 项目依赖配置 五、字节码修改实现代码&#…...