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

使用docker在3台服务器上搭建基于redis 6.x的一主两从三台均是哨兵模式

一、环境及版本说明 如果服务器已经安装了docker,则忽略此步骤,如果没有安装,则可以按照一下方式安装: 1. 在线安装(有互联网环境): 请看我这篇文章 传送阵>> 点我查看 2. 离线安装(内网环境):请看我这篇文章 传送阵>> 点我查看 说明&#xff1a;假设每台服务器已…...

DAY 47

三、通道注意力 3.1 通道注意力的定义 # 新增&#xff1a;通道注意力模块&#xff08;SE模块&#xff09; class ChannelAttention(nn.Module):"""通道注意力模块(Squeeze-and-Excitation)"""def __init__(self, in_channels, reduction_rat…...

Leetcode 3577. Count the Number of Computer Unlocking Permutations

Leetcode 3577. Count the Number of Computer Unlocking Permutations 1. 解题思路2. 代码实现 题目链接&#xff1a;3577. Count the Number of Computer Unlocking Permutations 1. 解题思路 这一题其实就是一个脑筋急转弯&#xff0c;要想要能够将所有的电脑解锁&#x…...

转转集团旗下首家二手多品类循环仓店“超级转转”开业

6月9日&#xff0c;国内领先的循环经济企业转转集团旗下首家二手多品类循环仓店“超级转转”正式开业。 转转集团创始人兼CEO黄炜、转转循环时尚发起人朱珠、转转集团COO兼红布林CEO胡伟琨、王府井集团副总裁祝捷等出席了开业剪彩仪式。 据「TMT星球」了解&#xff0c;“超级…...

python爬虫:Newspaper3k 的详细使用(好用的新闻网站文章抓取和解析的Python库)

更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 一、Newspaper3k 概述1.1 Newspaper3k 介绍1.2 主要功能1.3 典型应用场景1.4 安装二、基本用法2.2 提取单篇文章的内容2.2 处理多篇文档三、高级选项3.1 自定义配置3.2 分析文章情感四、实战案例4.1 构建新闻摘要聚合器…...

HBuilderX安装(uni-app和小程序开发)

下载HBuilderX 访问官方网站&#xff1a;https://www.dcloud.io/hbuilderx.html 根据您的操作系统选择合适版本&#xff1a; Windows版&#xff08;推荐下载标准版&#xff09; Windows系统安装步骤 运行安装程序&#xff1a; 双击下载的.exe安装文件 如果出现安全提示&…...

多模态大语言模型arxiv论文略读(108)

CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文标题&#xff1a;CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文作者&#xff1a;Sayna Ebrahimi, Sercan O. Arik, Tejas Nama, Tomas Pfister ➡️ 研究机构: Google Cloud AI Re…...

LINUX 69 FTP 客服管理系统 man 5 /etc/vsftpd/vsftpd.conf

FTP 客服管理系统 实现kefu123登录&#xff0c;不允许匿名访问&#xff0c;kefu只能访问/data/kefu目录&#xff0c;不能查看其他目录 创建账号密码 useradd kefu echo 123|passwd -stdin kefu [rootcode caozx26420]# echo 123|passwd --stdin kefu 更改用户 kefu 的密码…...

uniapp 字符包含的相关方法

在uniapp中&#xff0c;如果你想检查一个字符串是否包含另一个子字符串&#xff0c;你可以使用JavaScript中的includes()方法或者indexOf()方法。这两种方法都可以达到目的&#xff0c;但它们在处理方式和返回值上有所不同。 使用includes()方法 includes()方法用于判断一个字…...

Vue ③-生命周期 || 脚手架

生命周期 思考&#xff1a;什么时候可以发送初始化渲染请求&#xff1f;&#xff08;越早越好&#xff09; 什么时候可以开始操作dom&#xff1f;&#xff08;至少dom得渲染出来&#xff09; Vue生命周期&#xff1a; 一个Vue实例从 创建 到 销毁 的整个过程。 生命周期四个…...