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

2024-03-06 11:04

本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/779867

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

opencv图像处理之指纹验证的实现

《opencv图像处理之指纹验证的实现》本文主要介绍了opencv图像处理之指纹验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、简介二、具体案例实现1. 图像显示函数2. 指纹验证函数3. 主函数4、运行结果三、总结一、