feat: 创建自定义对话框的使用

This commit is contained in:
touwoyimuli 2019-10-04 23:37:27 +08:00
parent cf12d7798a
commit a117497eb2
23 changed files with 831 additions and 0 deletions

View File

@ -0,0 +1,91 @@
#include "ExCustomMainWin.h"
#include "ui_ExCustomMainWin.h"
#include <QDebug>
ExCustomMainWin::ExCustomMainWin(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ExCustomMainWin)
{
ui->setupUi(this);
setWindowTitle(QObject::tr("自定义对话框的用法"));
m_model = nullptr;
m_dlglocate = nullptr;
m_dlgSetHeaders = nullptr;
m_model = new QStandardItemModel(this);
}
ExCustomMainWin::~ExCustomMainWin()
{
delete ui;
}
void ExCustomMainWin::setACellText(int row, int col, QString text)
{
QModelIndex index = m_model->index(row, col);
m_seleModel->clearSelection();
m_seleModel->setCurrentIndex(index, QItemSelectionModel::Select);
m_model->setData(index, text, Qt::DisplayRole);
}
void ExCustomMainWin::on_actSetHeader_triggered()
{
if (m_dlgSetHeaders == nullptr)
m_dlgSetHeaders = new ExDlgSetHeaders(this); //只有主窗口销毁时候该dialog窗口才会销毁
if(m_dlgSetHeaders->headerList().count() != m_model->columnCount()) { //如果表头列数发生变化,就重新初始化
QStringList list;
for (int i = 0; i < m_model->columnCount(); i++) {
list.append(m_model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString());
}
m_dlgSetHeaders->setHeaderList(list); //对话框,初始化显示
}
int ret = m_dlgSetHeaders->exec(); //以模态对话框的方式显示
if (ret == QDialog::Accepted) { //Ok按键被按下
QStringList list = m_dlgSetHeaders->headerList();
m_model->setHorizontalHeaderLabels(list); //设置模型的表头标题
}
}
void ExCustomMainWin::on_actSetSize_triggered()
{
//创建模态对话框, 动态创建, 使用过后删除
ExDlgSize *dlgSize = new ExDlgSize(this);
Qt::WindowFlags flags = dlgSize->windowFlags();
dlgSize->setWindowFlags(flags | Qt::MSWindowsFixedSizeDialogHint); //在窗口上给窗口一个窄的对话框边框。用于固定大小的对话框。
dlgSize->setRowCol(m_model->rowCount(), m_model->columnCount());
int ret = dlgSize->exec(); //以模态对话框显示
if (ret == QDialog::Accepted) { //Ok按钮被按下获取对话框中输入设置行数和列数
int row = dlgSize->getRowCount();
int col = dlgSize->getColCount();
m_model->setRowCount(row);
m_model->setColumnCount(col);
}
delete dlgSize;
}
void ExCustomMainWin::on_actLocate_triggered()
{
ui->actLocate->setEnabled(false);
m_dlglocate = new ExDlgLocate(this);
m_dlglocate->setAttribute(Qt::WA_DeleteOnClose); //关闭对话框时候,自动删除
Qt::WindowFlags flags = m_dlglocate->windowFlags();
m_dlglocate->setWindowFlags(flags | Qt::WindowStaysOnTopHint); //窗口置顶
m_dlglocate->setSpinRange(m_model->rowCount(), m_model->columnCount());
QModelIndex curIndex = m_seleModel->currentIndex();
if (curIndex.isValid())
m_dlglocate->setSpinValue(curIndex.row(), curIndex.column());
m_dlglocate->show();
}

View File

@ -0,0 +1,40 @@
#ifndef EXCUSTOMMAINWIN_H
#define EXCUSTOMMAINWIN_H
#include <QMainWindow>
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include "ExDlgSize.h"
#include "ExDlgSetHeaders.h"
#include "ExDlgLocate.h"
namespace Ui {
class ExCustomMainWin;
}
class ExCustomMainWin : public QMainWindow
{
Q_OBJECT
public:
explicit ExCustomMainWin(QWidget *parent = nullptr);
~ExCustomMainWin();
void setACellText(int row, int col, QString text); //设置一个单元格,有ExDlgLocate调用
private slots:
void on_actSetHeader_triggered();
void on_actSetSize_triggered();
void on_actLocate_triggered();
private:
Ui::ExCustomMainWin *ui;
QStandardItemModel *m_model; //数据模型
QItemSelectionModel *m_seleModel; //item选择模型
ExDlgSetHeaders *m_dlgSetHeaders;
ExDlgLocate *m_dlglocate;
};
#endif // EXCUSTOMMAINWIN_H

View File

@ -0,0 +1,104 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExCustomMainWin</class>
<widget class="QMainWindow" name="ExCustomMainWin">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>816</width>
<height>472</height>
</rect>
</property>
<property name="windowTitle">
<string>ExCustomMainWin</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QTableView" name="tableView"/>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>816</width>
<height>23</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
<addaction name="actSetHeader"/>
<addaction name="actSetSize"/>
<addaction name="actLocate"/>
<addaction name="separator"/>
<addaction name="actQuit"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actSetHeader">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image002.png</normaloff>:/images/Image002.png</iconset>
</property>
<property name="text">
<string>设置表头</string>
</property>
<property name="toolTip">
<string>设置表头列表</string>
</property>
</action>
<action name="actSetSize">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image001.png</normaloff>:/images/Image001.png</iconset>
</property>
<property name="text">
<string>设置表格行列</string>
</property>
<property name="toolTip">
<string>设置表格行列数</string>
</property>
</action>
<action name="actLocate">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image004.jpg</normaloff>:/images/Image004.jpg</iconset>
</property>
<property name="text">
<string>修改单元格</string>
</property>
<property name="toolTip">
<string>修改某一单元格</string>
</property>
</action>
<action name="actQuit">
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image003.png</normaloff>:/images/Image003.png</iconset>
</property>
<property name="text">
<string>退出</string>
</property>
<property name="toolTip">
<string>退出程序</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources>
<include location="resources.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -0,0 +1,45 @@
#include "ExDlgLocate.h"
#include "ui_ExDlgLocate.h"
#include "ExCustomMainWin.h"
ExDlgLocate::ExDlgLocate(QWidget *parent) :
QDialog(parent),
ui(new Ui::ExDlgLocate)
{
ui->setupUi(this);
}
ExDlgLocate::~ExDlgLocate()
{
delete ui;
}
void ExDlgLocate::setSpinRange(int rowCount, int colCount)
{
}
void ExDlgLocate::setSpinValue(int rowNo, int colNo)
{
}
void ExDlgLocate::closeEvent(QCloseEvent *e)
{
}
void ExDlgLocate::on_btnSetText_clicked()
{
int row = ui->spinBoxRow->value(); //定位到单元格,并且设置字符串
int col = ui->spinBoxCol->value();
ExCustomMainWin *parWind = new ExCustomMainWin(nullptr); //向具体的item中填写字符串
parWind->setACellText(row, col, ui->lineEdit->text());
if (ui->checkBoxRow->isChecked())
ui->spinBoxRow->setValue(1 + ui->spinBoxRow->value());
if (ui->checkBoxCol->isChecked())
ui->spinBoxCol->setValue(1 + ui->spinBoxCol->value());
}

View File

@ -0,0 +1,34 @@
#ifndef EXDLGLOCATE_H
#define EXDLGLOCATE_H
#include <QDialog>
class ExCustomMainWin;
namespace Ui {
class ExDlgLocate;
}
class ExDlgLocate : public QDialog
{
Q_OBJECT
public:
explicit ExDlgLocate(QWidget *parent = nullptr);
~ExDlgLocate();
void setSpinRange(int rowCount, int colCount); //设置spin的设定值
void setSpinValue(int rowNo, int colNo); //设置spin的显示值
private slots:
void on_btnSetText_clicked();
private:
void closeEvent(QCloseEvent* e);
private:
Ui::ExDlgLocate *ui;
};
#endif // EXDLGLOCATE_H

View File

@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExDlgLocate</class>
<widget class="QDialog" name="ExDlgLocate">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>414</width>
<height>159</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QGridLayout" name="gridLayout" columnstretch="4,1,1">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>GroupBox</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="1">
<widget class="QSpinBox" name="spinBoxCol"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lab1">
<property name="text">
<string>行号:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lab2">
<property name="text">
<string>列号:</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QCheckBox" name="checkBoxRow">
<property name="text">
<string>行增</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QCheckBox" name="checkBoxCol">
<property name="text">
<string>列增</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="lab3">
<property name="text">
<string>设置文字:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="spinBoxRow"/>
</item>
<item row="2" column="1" colspan="2">
<widget class="QLineEdit" name="lineEdit"/>
</item>
</layout>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="btnSetText">
<property name="text">
<string>设定文字</string>
</property>
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image005.png</normaloff>:/images/Image005.png</iconset>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="btnClose">
<property name="text">
<string>关闭</string>
</property>
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image007.png</normaloff>:/images/Image007.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources>
<include location="resources.qrc"/>
</resources>
<connections/>
</ui>

View File

@ -0,0 +1,27 @@
#include "ExDlgSetHeaders.h"
#include "ui_ExDlgSetHeaders.h"
#include <QMessageBox>
ExDlgSetHeaders::ExDlgSetHeaders(QWidget *parent) :
QDialog(parent),
ui(new Ui::ExDlgSetHeaders)
{
ui->setupUi(this);
m_model = new QStringListModel(this);
}
ExDlgSetHeaders::~ExDlgSetHeaders()
{
QMessageBox::information(this,"提示","设置表头消息框被删除");
delete ui;
}
void ExDlgSetHeaders::setHeaderList(QStringList &headers)
{
m_model->setStringList(headers); //初始化字符串列表
}
QStringList ExDlgSetHeaders::headerList()
{
return m_model->stringList(); //返回字符串列表
}

View File

@ -0,0 +1,28 @@
#ifndef EXDLGSETHEADERS_H
#define EXDLGSETHEADERS_H
#include <QDialog>
#include <QStringListModel>
namespace Ui {
class ExDlgSetHeaders;
}
class ExDlgSetHeaders : public QDialog
{
Q_OBJECT
public:
explicit ExDlgSetHeaders(QWidget *parent = nullptr);
~ExDlgSetHeaders();
void setHeaderList(QStringList& headers);
QStringList headerList();
private:
Ui::ExDlgSetHeaders *ui;
QStringListModel *m_model; //管理字符串列表数据
};
#endif // EXDLGSETHEADERS_H

View File

@ -0,0 +1,107 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExDlgSetHeaders</class>
<widget class="QDialog" name="ExDlgSetHeaders">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>274</width>
<height>370</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>表头标题:</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListView" name="listView"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnOk">
<property name="text">
<string>确定</string>
</property>
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image005.png</normaloff>:/images/Image005.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnClose">
<property name="text">
<string>取消</string>
</property>
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image007.png</normaloff>:/images/Image007.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources>
<include location="resources.qrc"/>
</resources>
<connections>
<connection>
<sender>btnOk</sender>
<signal>clicked()</signal>
<receiver>ExDlgSetHeaders</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>135</x>
<y>337</y>
</hint>
<hint type="destinationlabel">
<x>136</x>
<y>184</y>
</hint>
</hints>
</connection>
<connection>
<sender>btnClose</sender>
<signal>clicked()</signal>
<receiver>ExDlgSetHeaders</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>216</x>
<y>337</y>
</hint>
<hint type="destinationlabel">
<x>136</x>
<y>184</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -0,0 +1,32 @@
#include "ExDlgSize.h"
#include "ui_ExDlgSize.h"
#include <QMessageBox>
ExDlgSize::ExDlgSize(QWidget *parent) :
QDialog(parent),
ui(new Ui::ExDlgSize)
{
ui->setupUi(this);
}
ExDlgSize::~ExDlgSize()
{
QMessageBox::information(this,"提示","设置表格行列数对话框被删除");
delete ui;
}
int ExDlgSize::getRowCount()
{
return ui->spinBoxRow->value();
}
int ExDlgSize::getColCount()
{
return ui->spinBoxCol->value();
}
void ExDlgSize::setRowCol(int row, int col)
{
ui->spinBoxRow->setValue(row);
ui->spinBoxCol->setValue(col);
}

View File

@ -0,0 +1,26 @@
#ifndef EXDLGSIZE_H
#define EXDLGSIZE_H
#include <QDialog>
namespace Ui {
class ExDlgSize;
}
class ExDlgSize : public QDialog
{
Q_OBJECT
public:
explicit ExDlgSize(QWidget *parent = nullptr);
~ExDlgSize();
int getRowCount();
int getColCount();
void setRowCol(int row, int col);
private:
Ui::ExDlgSize *ui;
};
#endif // EXDLGSIZE_H

View File

@ -0,0 +1,119 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ExDlgSize</class>
<widget class="QDialog" name="ExDlgSize">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>236</width>
<height>128</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>设置表格行列数:</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="1">
<widget class="QSpinBox" name="spinBoxCol">
<property name="value">
<number>6</number>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lab1">
<property name="text">
<string>行数:</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="lab2">
<property name="text">
<string>列数:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="spinBoxRow">
<property name="value">
<number>5</number>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QPushButton" name="btnOk">
<property name="text">
<string>确定</string>
</property>
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image005.png</normaloff>:/images/Image005.png</iconset>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnClose">
<property name="text">
<string>关闭</string>
</property>
<property name="icon">
<iconset resource="resources.qrc">
<normaloff>:/images/Image007.png</normaloff>:/images/Image007.png</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<resources>
<include location="resources.qrc"/>
</resources>
<connections>
<connection>
<sender>btnOk</sender>
<signal>clicked()</signal>
<receiver>ExDlgSize</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>62</x>
<y>105</y>
</hint>
<hint type="destinationlabel">
<x>117</x>
<y>63</y>
</hint>
</hints>
</connection>
<connection>
<sender>btnClose</sender>
<signal>clicked()</signal>
<receiver>ExDlgSize</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>173</x>
<y>105</y>
</hint>
<hint type="destinationlabel">
<x>117</x>
<y>63</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@ -0,0 +1,54 @@
#-------------------------------------------------
#
# Project created by QtCreator 2019-10-02T06:43:57
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtCustomDialogEx
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
ExCustomMainWin.cpp \
ExDlgSize.cpp \
ExDlgSetHeaders.cpp \
ExDlgLocate.cpp
HEADERS += \
ExCustomMainWin.h \
ExDlgSize.h \
ExDlgSetHeaders.h \
ExDlgLocate.h
FORMS += \
ExCustomMainWin.ui \
ExDlgSize.ui \
ExDlgSetHeaders.ui \
ExDlgLocate.ui
RC_ICONS += qt.ico
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
resources.qrc

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 877 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 773 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 538 B

11
QtCustomDialogEx/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "ExCustomMainWin.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ExCustomMainWin w;
w.show();
return a.exec();
}

BIN
QtCustomDialogEx/qt.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,11 @@
<RCC>
<qresource prefix="/">
<file>images/Image001.png</file>
<file>images/Image002.png</file>
<file>images/Image003.png</file>
<file>images/Image004.jpg</file>
<file>images/Image005.png</file>
<file>images/Image006.png</file>
<file>images/Image007.png</file>
</qresource>
</RCC>