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();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/513615.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Thinkphp5.1中,将数组赋值给js使用

一、例如Thinkphp5.1中的的代码是这样的 $data [status > 1,msg > 加载成功,data > [id > 1,username > 小洪帽,] ];$this->assign(data,$data);二、JS代码接收PHP中的数组 注意 <> 符号是不需要放引号的。 let arr <?json_encode($data)?>…

【C语言】新类型,结构体篇-----深入理解结构体,结构体成员的访问,结构体的大小和新概念偏移量!【图文详解】

欢迎来CILMY23的博客喔&#xff0c;本篇为【C语言】新类型&#xff0c;结构体篇-----深入理解结构体&#xff0c;结构体成员的访问&#xff0c;结构体的大小和新概念偏移量&#xff01;【图文详解】&#xff0c;感谢观看&#xff0c;支持的可以给个一键三连&#xff0c;点赞关注…

【牛客】VL64 时钟切换

描述 题目描述&#xff1a; 存在两个同步的倍频时钟clk0 clk1,已知clk0是clk1的二倍频&#xff0c;现在要设计一个切换电路&#xff0c;sel选择时候进行切换&#xff0c;要求没有毛刺。 信号示意图&#xff1a; 波形示意图&#xff1a; 输入描述&#xff1a; clk0 clk1为时…

超详细的Scrapy框架的基本使用教程

Scrapy的介绍 scrapy的工作流程&#xff08;重点&#xff01;&#xff01;&#xff01;&#xff09; 如下图所示&#xff1a; 爬虫&#xff1a; 负责向引擎提供要爬取网页的URL&#xff0c;引擎会把这个URL封装成request对象并传递给调度器&#xff0c;把引擎传递过来的resp…

冒泡经典题

&#x1f4d1;前言 本文主要是【】——简单使用的文章&#xff0c;如果有什么需要改进的地方还请大佬指出⛺️ &#x1f3ac;作者简介&#xff1a;大家好&#xff0c;我是听风与他&#x1f947; ☁️博客首页&#xff1a;CSDN主页听风与他 &#x1f304;每日一句&#xff1a;狠…

搜维尔科技:捕获、分析、优化,使用 Xsens Ergo 创建更安全的工作空间

简化人体工程学分析&#xff0c;优先考虑员工福祉&#xff0c;并利用客观数据和见解提高生产力。 捕获。分析。优化。使用 Xsens Ergo 创建更安全的工作空间 1.质量数据 使用高质量、客观且经过验证的运动数据进行详细的人体工程学分析 2.随处使用 在最具挑战性的工作环境中…

C++面试干货---带你梳理常考的面试题(二)

顾得泉&#xff1a;个人主页 个人专栏&#xff1a;《Linux操作系统》 《C从入门到精通》 《LeedCode刷题》 键盘敲烂&#xff0c;年薪百万&#xff01; 1.struct 和 class 区别 1.默认访问权限&#xff1a;struct中的成员默认为public&#xff0c;而class中的成员默认为priv…

【知识梳理】关于网络分层

转载说明&#xff1a;如果您喜欢这篇文章并打算转载它&#xff0c;请私信作者取得授权。感谢您喜爱本文&#xff0c;请文明转载&#xff0c;谢谢。 网络协议分层 网络协议通常分不同层次进行开发&#xff0c;每一层分别负责不同的通信功能。一个协议族&#xff0c;比如 TCP/IP…

JDK17在Windows安装以及环境变量配置(超详细的教程)

目录 一、JDK17的安装包下载 二、安装JDK17 第一步&#xff1a;运行JDK的EXE文件 第二步&#xff1a;选择下一步 第三步&#xff1a;选择安装目录 第四步&#xff1a;安装完成 三、配置JDK17的环境变量 第一步&#xff1a;打开系统属性界面 第二步&#xff1a;打开高级…

Jekins 自启动Java应用的Shell笔记

背景 最近在研究jdk 的jvisualvm 对JVM服务远程监控时&#xff0c;意外的与jekins接轨了。公司使用jekins自动从Git上获得源码&#xff0c;打包后传到测试服务器并启动jar包&#xff0c;实现自动部署&#xff0c;而我需要做的是在测试服务器启动jar包时添加几个我设置的命令&am…

LeetCode刷题日志-17.电话号码的字母组合

纯暴力解法&#xff0c;digits有多长&#xff0c;就循环多少次进行字母组合 class Solution {public List<String> letterCombinations(String digits) {List<String> reslut new ArrayList<>();if(digits.equals(""))return reslut;Map<Inte…

【一起学习Arcade】(6):属性规则实例_约束规则和验证规则

一、约束规则 约束规则用于指定要素上允许的属性配置和一般关系。 与计算规则不同&#xff0c;约束规则不用于填充属性&#xff0c;而是用于确保要素满足特定条件。 简单理解&#xff0c;约束规则就是约束你的编辑操作在什么情况下可执行。 如果出现不符合规则的操作&#…