当前位置: 首页 > 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切片 下面是几种不同空切片…...

AtCoder 第409​场初级竞赛 A~E题解

A Conflict 【题目链接】 原题链接&#xff1a;A - Conflict 【考点】 枚举 【题目大意】 找到是否有两人都想要的物品。 【解析】 遍历两端字符串&#xff0c;只有在同时为 o 时输出 Yes 并结束程序&#xff0c;否则输出 No。 【难度】 GESP三级 【代码参考】 #i…...

css的定位(position)详解:相对定位 绝对定位 固定定位

在 CSS 中&#xff0c;元素的定位通过 position 属性控制&#xff0c;共有 5 种定位模式&#xff1a;static&#xff08;静态定位&#xff09;、relative&#xff08;相对定位&#xff09;、absolute&#xff08;绝对定位&#xff09;、fixed&#xff08;固定定位&#xff09;和…...

如何在最短时间内提升打ctf(web)的水平?

刚刚刷完2遍 bugku 的 web 题&#xff0c;前来答题。 每个人对刷题理解是不同&#xff0c;有的人是看了writeup就等于刷了&#xff0c;有的人是收藏了writeup就等于刷了&#xff0c;有的人是跟着writeup做了一遍就等于刷了&#xff0c;还有的人是独立思考做了一遍就等于刷了。…...

C#学习第29天:表达式树(Expression Trees)

目录 什么是表达式树&#xff1f; 核心概念 1.表达式树的构建 2. 表达式树与Lambda表达式 3.解析和访问表达式树 4.动态条件查询 表达式树的优势 1.动态构建查询 2.LINQ 提供程序支持&#xff1a; 3.性能优化 4.元数据处理 5.代码转换和重写 适用场景 代码复杂性…...

C语言中提供的第三方库之哈希表实现

一. 简介 前面一篇文章简单学习了C语言中第三方库&#xff08;uthash库&#xff09;提供对哈希表的操作&#xff0c;文章如下&#xff1a; C语言中提供的第三方库uthash常用接口-CSDN博客 本文简单学习一下第三方库 uthash库对哈希表的操作。 二. uthash库哈希表操作示例 u…...

Golang——7、包与接口详解

包与接口详解 1、Golang包详解1.1、Golang中包的定义和介绍1.2、Golang包管理工具go mod1.3、Golang中自定义包1.4、Golang中使用第三包1.5、init函数 2、接口详解2.1、接口的定义2.2、空接口2.3、类型断言2.4、结构体值接收者和指针接收者实现接口的区别2.5、一个结构体实现多…...

Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement

Cilium动手实验室: 精通之旅---13.Cilium LoadBalancer IPAM and L2 Service Announcement 1. LAB环境2. L2公告策略2.1 部署Death Star2.2 访问服务2.3 部署L2公告策略2.4 服务宣告 3. 可视化 ARP 流量3.1 部署新服务3.2 准备可视化3.3 再次请求 4. 自动IPAM4.1 IPAM Pool4.2 …...

ubuntu系统文件误删(/lib/x86_64-linux-gnu/libc.so.6)修复方案 [成功解决]

报错信息&#xff1a;libc.so.6: cannot open shared object file: No such file or directory&#xff1a; #ls, ln, sudo...命令都不能用 error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory重启后报错信息&…...

Canal环境搭建并实现和ES数据同步

作者&#xff1a;田超凡 日期&#xff1a;2025年6月7日 Canal安装&#xff0c;启动端口11111、8082&#xff1a; 安装canal-deployer服务端&#xff1a; https://github.com/alibaba/canal/releases/1.1.7/canal.deployer-1.1.7.tar.gz cd /opt/homebrew/etc mkdir canal…...

Appium下载安装配置保姆教程(图文详解)

目录 一、Appium软件介绍 1.特点 2.工作原理 3.应用场景 二、环境准备 安装 Node.js 安装 Appium 安装 JDK 安装 Android SDK 安装Python及依赖包 三、安装教程 1.Node.js安装 1.1.下载Node 1.2.安装程序 1.3.配置npm仓储和缓存 1.4. 配置环境 1.5.测试Node.j…...