QT系列第8节 自定义对话框

news2024/10/6 11:19:40

       在实际业务开发中经常要有各种各样的对话框来处理用户信息,本节就结合例子来说明如何自定义对话框。

目录

1.创建对话框

2.创建非模态对话框

3.创建模态对话框

4.综合案例


1.创建对话框

(1)项目鼠标右键菜单 - 添加新文件

(2) 左侧选择 Qt,右侧选择Qt设计器界面类

(3)一般选择 Dialog without Buttons

 (4) 输入我们要定义的对话框类名,其他名字根据输入类名调整,可以不修改

(5)选择完成

 (6)进入了ui设计器面板

 (7)进入编辑tab页中就能看到新增加的三个文件

2.创建非模态对话框

如下方式可以创建和现实一个对话框

 QDialogLocate *dialogLocate = new  QDialogLocate(this);
 //设置属性:关闭时候删除
 dialogLocate->setAttribute(Qt::WA_DeleteOnClose); 
 Qt::WindowFlags flags = dialogLocate->windowFlags();
 dialogLocate->setWindowFlags(flags|Qt::WindowStaysOnTopHint);

 //非模态对话框显示
 dialogLocate->show();

3.创建模态对话框

       如下方式可以创建和销毁一个模态对话框。

DialogSetSie *dlg = new DialogSetSie(this);
dlg->SetRowsColumns(theModel->rowCount(), theModel->columnCount());
int ret = dlg->exec();
if (QDialog::Accepted == ret) {
    ...   
}
delete dlg;

4.综合案例

       以下综合案例创建一个主对话框,主对话框有一个tableview,上面工具栏可以弹出对话框用来设置主对话框中tableview的属性,并且支持通过Model来修改tableview显示内容。

(1)主对话框中tablevview设置Model

 theModel = new QStandardItemModel(this);
    theSelection = new QItemSelectionModel(theModel);
    ui->tableView->setModel(theModel);
    ui->tableView->setSelectionModel(theSelection); theModel = new QStandardItemModel(this);
    theSelection = new QItemSelectionModel(theModel);
    ui->tableView->setModel(theModel);
    ui->tableView->setSelectionModel(theSelection);

(2)设置tablevview行和列的对话两

dialogsetsie.h

#ifndef DIALOGSETSIE_H
#define DIALOGSETSIE_H

#include <QDialog>

namespace Ui {
class DialogSetSie;
}

class DialogSetSie : public QDialog
{
    Q_OBJECT

public:
    explicit DialogSetSie(QWidget *parent = nullptr);
    ~DialogSetSie();
    void SetRowsColumns(int row, int column);
    int columnCount();
    int rowCount();

private slots:
    void on_pushButtonCancel_clicked();

    void on_pushButtonOk_clicked();

private:
    Ui::DialogSetSie *ui;
};

#endif // DIALOGSETSIE_H

dialogsetsie.cpp

#include "dialogsetsie.h"
#include "ui_dialogsetsie.h"

#include <QMessageBox>

DialogSetSie::DialogSetSie(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSetSie)
{
    ui->setupUi(this);
}

DialogSetSie::~DialogSetSie()
{
    QMessageBox::information(this, "", "设置大小对话框已退出");
    delete ui;
}

void DialogSetSie::SetRowsColumns(int row, int column) {
    ui->spinBoxColumn->setValue(column);
    ui->spinBoxLine->setValue(row);
}

int DialogSetSie::columnCount() {
    return ui->spinBoxColumn->value();
}

int DialogSetSie::rowCount() {
    return ui->spinBoxLine->value();
}

void DialogSetSie::on_pushButtonCancel_clicked()
{

}


void DialogSetSie::on_pushButtonOk_clicked()
{

}

(3)设置tablevview表头对话框

dialogsetheaders.h

#ifndef DIALOGSETHEADERS_H
#define DIALOGSETHEADERS_H

#include <QDialog>
#include <QStringListModel>

namespace Ui {
class DialogSetHeaders;
}

class DialogSetHeaders : public QDialog
{
    Q_OBJECT
private:
    QStringListModel *theModel;

public:
    explicit DialogSetHeaders(QWidget *parent = nullptr);
    ~DialogSetHeaders();

    void setStringList(QStringList strList);
    QStringList stringList();

private slots:
    void on_pushButtonOk_clicked();

    void on_pushButtonCancel_clicked();

private:
    Ui::DialogSetHeaders *ui;
};

#endif // DIALOGSETHEADERS_H
#ifndef DIALOGSETHEADERS_H
#define DIALOGSETHEADERS_H

#include <QDialog>
#include <QStringListModel>

namespace Ui {
class DialogSetHeaders;
}

class DialogSetHeaders : public QDialog
{
    Q_OBJECT
private:
    QStringListModel *theModel;

public:
    explicit DialogSetHeaders(QWidget *parent = nullptr);
    ~DialogSetHeaders();

    void setStringList(QStringList strList);
    QStringList stringList();

private slots:
    void on_pushButtonOk_clicked();

    void on_pushButtonCancel_clicked();

private:
    Ui::DialogSetHeaders *ui;
};

#endif // DIALOGSETHEADERS_H

dialogsetheaders.cpp

#include "dialogsetheaders.h"
#include "ui_dialogsetheaders.h"
#include <QMessageBox>

DialogSetHeaders::DialogSetHeaders(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSetHeaders)
{
    ui->setupUi(this);
    theModel = new QStringListModel(this);
    ui->listView->setModel(theModel);
}

DialogSetHeaders::~DialogSetHeaders()
{
    QMessageBox::information(this, "", "设置表头对话框退出");
    delete ui;
}

void DialogSetHeaders::setStringList(QStringList strList)
{
    theModel->setStringList(strList);
}

QStringList DialogSetHeaders::stringList()
{
    return theModel->stringList();
}

void DialogSetHeaders::on_pushButtonOk_clicked()
{
    emit accept();
}


void DialogSetHeaders::on_pushButtonCancel_clicked()
{
    emit reject();
}

#include "dialogsetheaders.h"
#include "ui_dialogsetheaders.h"
#include <QMessageBox>

DialogSetHeaders::DialogSetHeaders(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSetHeaders)
{
    ui->setupUi(this);
    theModel = new QStringListModel(this);
    ui->listView->setModel(theModel);
}

DialogSetHeaders::~DialogSetHeaders()
{
    QMessageBox::information(this, "", "设置表头对话框退出");
    delete ui;
}

void DialogSetHeaders::setStringList(QStringList strList)
{
    theModel->setStringList(strList);
}

QStringList DialogSetHeaders::stringList()
{
    return theModel->stringList();
}

void DialogSetHeaders::on_pushButtonOk_clicked()
{
    emit accept();
}


void DialogSetHeaders::on_pushButtonCancel_clicked()
{
    emit reject();
}

(4)修改tablevview单元格对话框

qdialoglocate.h

#ifndef QDIALOGLOCATE_H
#define QDIALOGLOCATE_H

#include <QDialog>

namespace Ui {
class QDialogLocate;
}

class QDialogLocate : public QDialog
{
    Q_OBJECT

public:
    explicit QDialogLocate(QWidget *parent = nullptr);
    ~QDialogLocate();

    void setRange(int rows, int cols);
    void setValue(int rows, int cols);
    void closeEvent(QCloseEvent *event);

private slots:
    void on_pushButtonText_clicked();

    void on_pushButtonClose_clicked();

signals:
    void changeCellText(int, int, QString);

private:
    Ui::QDialogLocate *ui;
};

#endif // QDIALOGLOCATE_H

qdialoglocate.cpp

#include "qdialoglocate.h"
#include "ui_qdialoglocate.h"
#include "mainwindow.h"
#include <QMessageBox>

QDialogLocate::QDialogLocate(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QDialogLocate)
{
    ui->setupUi(this);
}

QDialogLocate::~QDialogLocate()
{
    QMessageBox::information(this, "提示", "设置单元格被关闭");
    delete ui;
}

void QDialogLocate::setRange(int rows, int cols) {
    ui->spinBoxRow->setRange(0, rows-1);
    ui->spinBoxColumn->setRange(0, cols -1);

}

void QDialogLocate::setValue(int rows, int cols) {
    ui->spinBoxRow->setValue(rows);
    ui->spinBoxColumn->setValue(cols);
}

void QDialogLocate::closeEvent(QCloseEvent *event)
{
    MainWindow *mainWnd = (MainWindow *)parentWidget();
    mainWnd->setActLocateEnable(true);
}

void QDialogLocate::on_pushButtonText_clicked()
{
    int row = ui->spinBoxRow->value();
    int col = ui->spinBoxColumn->value();
    /*
    MainWindow *mainWnd = (MainWindow *)parentWidget();
    mainWnd->setCellText(row, col, ui->lineEditText->text());
    */
    emit changeCellText(row, col, ui->lineEditText->text());
    if (ui->checkBoxAddRow->isChecked()) {
        ui->spinBoxRow->setValue(ui->spinBoxRow->value() + 1);
    }
    if (ui->checkBoxAddCol->isChecked()) {
        ui->spinBoxColumn->setValue(ui->spinBoxColumn->value() + 1);
    }
}


void QDialogLocate::on_pushButtonClose_clicked()
{
    close();
}

(5) 主窗口

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include "dialogsetheaders.h"
#include "qdialoglocate.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT
private:
    QStandardItemModel  *theModel;
    QItemSelectionModel *theSelection;
    DialogSetHeaders *dialogHeder;
    QDialogLocate   *dialogLocate = NULL;

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    void setActLocateEnable(bool enable);
    void setActLocateNull();

private slots:
    void on_actionCut_triggered();

    void on_actionSetLineColumn_triggered();

    void on_actionHeadTitle_triggered();

    void on_actionQuit_triggered();

    void on_actionSetRect_triggered();

    void on_tableView_clicked(const QModelIndex &index);

    void setCellText(int row, int col, QString text);

private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialogsetsie.h"
#include <QStringList>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    theModel = new QStandardItemModel(this);
    theSelection = new QItemSelectionModel(theModel);
    ui->tableView->setModel(theModel);
    ui->tableView->setSelectionModel(theSelection);
    dialogHeder = nullptr;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setActLocateEnable(bool enable)
{
    ui->actionSetRect->setEnabled(enable);
}

void MainWindow::setActLocateNull() {
    dialogLocate = nullptr;
}

void MainWindow::setCellText(int row, int col, QString text)
{
    QModelIndex index = theModel->index(row, col);
    theModel->setData(index, text, Qt::DisplayRole);
    theSelection->clearSelection();
    theSelection->setCurrentIndex(index, QItemSelectionModel::Select);
}

void MainWindow::on_actionCut_triggered()
{
    this->close();
}


void MainWindow::on_actionSetLineColumn_triggered()
{
    DialogSetSie *dlg = new DialogSetSie(this);
    dlg->SetRowsColumns(theModel->rowCount(), theModel->columnCount());
    int ret = dlg->exec();
    if (QDialog::Accepted == ret) {
        int row = dlg->rowCount();
        int col = dlg->columnCount();
        theModel->setColumnCount(col);
        theModel->setRowCount(row);
    }
    delete dlg;
}


void MainWindow::on_actionHeadTitle_triggered()
{
    if (!dialogHeder) {
        dialogHeder = new DialogSetHeaders(this);
    }
    if (dialogHeder->stringList().count() != theModel->columnCount()) {
        QStringList strList;
        for (int i=0; i<theModel->columnCount(); i++) {
            //strList.append(theModel->headerData(i, Qt::Horizontal));
            strList << theModel->headerData(i, Qt::Horizontal).toString();
        }
        dialogHeder->setStringList(strList);
    }
    //模态对话框显示
    int ret = dialogHeder->exec();
    if (QDialog::Accepted == ret) {
        QStringList strList = dialogHeder->stringList();
        theModel->setHorizontalHeaderLabels(strList);
    }
}


void MainWindow::on_actionQuit_triggered()
{
    this->close();
}


void MainWindow::on_actionSetRect_triggered()
{
    ui->actionSetRect->setDisabled(true);
    dialogLocate = new  QDialogLocate(this);
    dialogLocate->setAttribute(Qt::WA_DeleteOnClose);
    Qt::WindowFlags flags = dialogLocate->windowFlags();
    dialogLocate->setWindowFlags(flags|Qt::WindowStaysOnTopHint);
    dialogLocate->setRange(theModel->rowCount(), theModel->columnCount());
    QModelIndex curIndex = theSelection->currentIndex();
    if (curIndex.isValid()) {
        dialogLocate->setValue(curIndex.row(), curIndex.column());
    }
    connect(dialogLocate, SIGNAL(changeCellText(int, int, QString)), this, SLOT(setCellText(int, int, QString)));
    //connect(dialogLocate, &QDialogLocate::changeCellText, this, &MainWindow::setCellText);
    //非模态对话框显示
    dialogLocate->show();
}


void MainWindow::on_tableView_clicked(const QModelIndex &index)
{
    if (NULL != dialogLocate) {
        dialogLocate->setValue(index.row(), index.column());
    }
}

(6) 效果展示

设置五行三列

 

修改表头:

 

 

 修改单元格:

完整代码下载地址:

https://download.csdn.net/download/hsy12342611/87346158

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Hexo + Butterfly 自定义页脚

原文链接 &#xff1a;Hexo Butterfly 自定义页脚 推荐阅读 基于 Hexo 从零开始搭建个人博客&#xff08;一&#xff09;: 环境准备基于 Hexo 从零开始搭建个人博客&#xff08;二&#xff09;: 项目初识基于 Hexo 从零开始搭建个人博客&#xff08;三&#xff09;: 主题安装…

CSDN每日一练最长递增的区间长度 C语言

题目名称&#xff1a;最长递增的区间长度 时间限制&#xff1a;1000ms 内存限制&#xff1a;256M 题目描述 给一个无序数组&#xff0c;求最长递增的区间长度。如&#xff1a;[5,2,3,8,1,9] 最长区间 2,3,8 长度为 3 &#xff08;注意&#xff1a;测试用例仅做参考&#xff0c;…

Spring web开发之Request 获取三种方式

在开发 Java Web 项目中&#xff0c;我们经常使用 HttpServletRequest 获取请求参数、请求头等信息。在Spring项目&#xff0c;我们通常会使用 Spring 提供的注解获取参数&#xff0c;如 RequestParam、RequestHeader。 不过在某些场景下&#xff0c;我们可能需要从 HttpServl…

初识Docker:(4)Docker基本操作

初识Docker&#xff1a;&#xff08;4&#xff09;Docker基本操作1 镜像操作1.1 镜像名称1.2 镜像操作命令1.3 案例&#xff1a;docker拉取nginx镜像利用docker save将nginx镜像导出磁盘&#xff0c;然后再通过load加载回来1.4 镜像操作总结2 容器操作2.1 案例创建运行一个ngin…

【阅读笔记】《持续交付2.0》中理解分支、发布策略

文章目录1. 前言1.1 分支、发布 管理上解耦2. 主干 (Trunk) 和分支 (Branch)2.1 Trunk 开发 Trunk 发布2.1.1 Trunk 开发 Trunk 发布需要解决&#xff1a;重构的需求2.1.2 Trunk 开发 Trunk 发布需要解决&#xff1a;未开发完成的功能被带入发布版本2.2 Trunk 开发 Branch 发布…

leetcode:6272. 好分区的数目【思维转换(正难则反) + dp定义 + 背包问题 + 选or不选】

目录题目截图题目分析ac code总结题目截图 题目分析 先特判&#xff0c;如果sum(nums) < 2 * k显然不可能成功&#xff01;返回0出现Mod大概率就是dp1000的话提示我们用平方复杂度的dp这种取子序列的问题&#xff0c;本质就是选or不选的问题如果我们只考虑一维dp,dp[i]肯定…

Linux--信号

目录1. 信号概念2. 信号产生前2.1 信号产生的各种方式3. 信号产生中信号保存的方式3.1 阻塞信号3.2 信号屏蔽字4. 信号产生后信号处理的方式4.1 信号集操作函数4.2 sigprocmask函数4.3 sigpending函数4.4 sigaction函数5. 信号是什么时候被处理的1. 信号概念 信号是进程之间事…

golang访问KingbaseES V8R6

概述 本文介绍go语言连接KingbaseES V8R6数据库的步骤 测试环境 操作系统&#xff1a;CentOS 7.2.1511 数据库版本&#xff1a;KINGBASE (KingbaseES) V008R006C007B0012 go版本&#xff1a;go version go1.19.4 linux/amd64 KingbaseES go驱动获取 go连接kingbase数据库需…

MySQL为什么使用B+树为索引结构

目录 1、什么是索引 2、索引的类型 3、为什么要用索引 4、索引的使用场景 5、索引为什么要用B树&#xff0c;为什么不能用二叉树、红黑树、B树&#xff1f; 介绍一款可以帮助理解数据结构的网站&#xff08;很好用&#xff09;&#xff1a;Data Structure Visualization …

hadoop生产调优之Hadoop-Yarn 生产经验(参数调优)

一、常用的调优参数 1&#xff09;调优参数列表 &#xff08;1&#xff09;Resourcemanager 相关 yarn.resourcemanager.scheduler.client.thread-count ResourceManager 处理调度器请求的线程数量 yarn.resourcemanager.scheduler.class 配置调度器&#xff08;2&#xff0…

js中ArrayBuffer和node中Buffer的关系和区别

ArrayBuffer 对象用来表示通用的、固定长度的原始二进制数据缓冲区。 它是一个字节数组&#xff0c;通常在其他语言中称为“byte array”。你不能直接操作 ArrayBuffer 中的内容&#xff1b;而是要通过类型化数组对象或 DataView 对象来操作&#xff0c;它们会将缓冲区中的数据…

C++、python、VS code插件安装与SSH使用

下载按照VS coda 官网&#xff1a;https://code.visualstudio.com 1.安装相关插件 1.中文插件&#xff08;可选&#xff09; MS-CEINTL.vscode-language-pack-zh-hans 2.C插件&#xff08;必选&#xff09; ms-vscode.cpptools 3.ssh 远程&#xff08;必选&#xff09; ms-vs…

数据结构——快排的三种实现方式

坚持看完&#xff0c;结尾有思维导图总结 这里写目录标题什么是快排&#xff1f;如何实现递归单次的排序要如何实现hore 的办法![在这里插入图片描述](https://img-blog.csdnimg.cn/40b2ac63f2424bd1828a45f8509ff116.gif#pic_center)坑位法双指针法总结什么是快排&#xff1f;…

线程池(一)

个人博客地址&#xff1a; http://xiaohe-blog.top/index.php/archives/14/ 文章目录1. 为什么要使用线程池2. Executor3. ThreadPoolExecutor3.1 七个参数3.2 任务队列3.3 拒绝策略4. 创建线程池5. Executors5.1 CachedThreadPool5.2 FixedThreadPool5.3 SingleThreadExecutor…

SAP UI5 应用里一些容器控件的介绍

sap.m.Shell 控件可用作应用程序的根元素。 它可以包含 App 或 SplitApp 控件。 Shell 为整个应用程序提供了一些总体功能&#xff0c;并负责在桌面浏览器平台上进行视觉适配&#xff0c;例如应用程序周围的框架。 sap.m.App: 该 App 继承自 sap.m.NavContainer 并因此提供其导…

VUEElement 学习笔记

1 VUE 1.1 示例 新建test_vue.jsp <% page contentType"text/html;charsetUTF-8" language"java" %> <html> <head><title>Title</title> </head> <body><div id"app"><input name"…

三、SpringBoot启动流程及自动化配置

一、Springboot启动流程 图一:Springboot项目的启动流程 首先,针对上图中自己不太明确的两个知识点,这里做如下总结: 1.Banner:参考这篇文章:SpringBoot之Banner介绍 - MarkLogZhu - 博客园 (cnblogs.com) ; 2.钩子方…

【Javassist】快速入门系列07 当检测到字段被访问时使用语句块替换访问

系列文章目录 01 在方法体的开头或结尾插入代码 02 使用Javassist实现方法执行时间统计 03 使用Javassist实现方法异常处理 04 使用Javassist更改整个方法体 05 当有指定方法调用时替换方法调用的内容 06 当有构造方法调用时替换方法调用的内容 07 当检测到字段被访问时使用语…

一个最基本的lLinux驱动开发框架和编译驱动方式

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录前言一、编写驱动文件1.相关头文件2.驱动入口 &出口3.申明完整代码二、编译驱动的方式三、编译驱动1. 和内核一起编译&#xff1a;2. 编译成驱动模块&#xff1a…

Kafka消息写入流程

Kafka消息写入流程 0,写入消息简要流程图 1,从示例开始 在Kafka中,Producer实例是线程安全的,通常一个Producer的进程只需要生成一个Producer实例. 这样比一个进程中生成多个Producer实例的效率反而会更高. 在Producer的配置中,可以配置Producer的每个batch的内存缓冲区的大小…