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