diff --git a/QtCustomDialogEx/ExCustomMainWin.cpp b/QtCustomDialogEx/ExCustomMainWin.cpp new file mode 100644 index 0000000..4123fd8 --- /dev/null +++ b/QtCustomDialogEx/ExCustomMainWin.cpp @@ -0,0 +1,91 @@ +#include "ExCustomMainWin.h" +#include "ui_ExCustomMainWin.h" +#include + + +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(); +} diff --git a/QtCustomDialogEx/ExCustomMainWin.h b/QtCustomDialogEx/ExCustomMainWin.h new file mode 100644 index 0000000..2e464d0 --- /dev/null +++ b/QtCustomDialogEx/ExCustomMainWin.h @@ -0,0 +1,40 @@ +#ifndef EXCUSTOMMAINWIN_H +#define EXCUSTOMMAINWIN_H + +#include +#include +#include + +#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 diff --git a/QtCustomDialogEx/ExCustomMainWin.ui b/QtCustomDialogEx/ExCustomMainWin.ui new file mode 100644 index 0000000..4aeb527 --- /dev/null +++ b/QtCustomDialogEx/ExCustomMainWin.ui @@ -0,0 +1,104 @@ + + + ExCustomMainWin + + + + 0 + 0 + 816 + 472 + + + + ExCustomMainWin + + + + + + + + + + + + 0 + 0 + 816 + 23 + + + + + + Qt::ToolButtonTextBesideIcon + + + TopToolBarArea + + + false + + + + + + + + + + + + :/images/Image002.png:/images/Image002.png + + + 设置表头 + + + 设置表头列表 + + + + + + :/images/Image001.png:/images/Image001.png + + + 设置表格行列 + + + 设置表格行列数 + + + + + + :/images/Image004.jpg:/images/Image004.jpg + + + 修改单元格 + + + 修改某一单元格 + + + + + + :/images/Image003.png:/images/Image003.png + + + 退出 + + + 退出程序 + + + + + + + + + diff --git a/QtCustomDialogEx/ExDlgLocate.cpp b/QtCustomDialogEx/ExDlgLocate.cpp new file mode 100644 index 0000000..17375b9 --- /dev/null +++ b/QtCustomDialogEx/ExDlgLocate.cpp @@ -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()); +} diff --git a/QtCustomDialogEx/ExDlgLocate.h b/QtCustomDialogEx/ExDlgLocate.h new file mode 100644 index 0000000..7c60711 --- /dev/null +++ b/QtCustomDialogEx/ExDlgLocate.h @@ -0,0 +1,34 @@ +#ifndef EXDLGLOCATE_H +#define EXDLGLOCATE_H + +#include + +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 diff --git a/QtCustomDialogEx/ExDlgLocate.ui b/QtCustomDialogEx/ExDlgLocate.ui new file mode 100644 index 0000000..0c6596e --- /dev/null +++ b/QtCustomDialogEx/ExDlgLocate.ui @@ -0,0 +1,102 @@ + + + ExDlgLocate + + + + 0 + 0 + 414 + 159 + + + + Dialog + + + + + + + + GroupBox + + + + + + + + + 行号: + + + + + + + 列号: + + + + + + + 行增 + + + + + + + 列增 + + + + + + + 设置文字: + + + + + + + + + + + + + + + + 设定文字 + + + + :/images/Image005.png:/images/Image005.png + + + + + + + 关闭 + + + + :/images/Image007.png:/images/Image007.png + + + + + + + + + + + + diff --git a/QtCustomDialogEx/ExDlgSetHeaders.cpp b/QtCustomDialogEx/ExDlgSetHeaders.cpp new file mode 100644 index 0000000..7bb52fe --- /dev/null +++ b/QtCustomDialogEx/ExDlgSetHeaders.cpp @@ -0,0 +1,27 @@ +#include "ExDlgSetHeaders.h" +#include "ui_ExDlgSetHeaders.h" +#include + +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(); //返回字符串列表 +} diff --git a/QtCustomDialogEx/ExDlgSetHeaders.h b/QtCustomDialogEx/ExDlgSetHeaders.h new file mode 100644 index 0000000..c9c691d --- /dev/null +++ b/QtCustomDialogEx/ExDlgSetHeaders.h @@ -0,0 +1,28 @@ +#ifndef EXDLGSETHEADERS_H +#define EXDLGSETHEADERS_H + +#include +#include + +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 diff --git a/QtCustomDialogEx/ExDlgSetHeaders.ui b/QtCustomDialogEx/ExDlgSetHeaders.ui new file mode 100644 index 0000000..a231f45 --- /dev/null +++ b/QtCustomDialogEx/ExDlgSetHeaders.ui @@ -0,0 +1,107 @@ + + + ExDlgSetHeaders + + + + 0 + 0 + 274 + 370 + + + + Dialog + + + + + + 表头标题: + + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + 确定 + + + + :/images/Image005.png:/images/Image005.png + + + + + + + 取消 + + + + :/images/Image007.png:/images/Image007.png + + + + + + + + + + + + + + + + btnOk + clicked() + ExDlgSetHeaders + accept() + + + 135 + 337 + + + 136 + 184 + + + + + btnClose + clicked() + ExDlgSetHeaders + reject() + + + 216 + 337 + + + 136 + 184 + + + + + diff --git a/QtCustomDialogEx/ExDlgSize.cpp b/QtCustomDialogEx/ExDlgSize.cpp new file mode 100644 index 0000000..9650a8f --- /dev/null +++ b/QtCustomDialogEx/ExDlgSize.cpp @@ -0,0 +1,32 @@ +#include "ExDlgSize.h" +#include "ui_ExDlgSize.h" +#include + +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); +} diff --git a/QtCustomDialogEx/ExDlgSize.h b/QtCustomDialogEx/ExDlgSize.h new file mode 100644 index 0000000..7fab89e --- /dev/null +++ b/QtCustomDialogEx/ExDlgSize.h @@ -0,0 +1,26 @@ +#ifndef EXDLGSIZE_H +#define EXDLGSIZE_H + +#include + +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 diff --git a/QtCustomDialogEx/ExDlgSize.ui b/QtCustomDialogEx/ExDlgSize.ui new file mode 100644 index 0000000..c78165f --- /dev/null +++ b/QtCustomDialogEx/ExDlgSize.ui @@ -0,0 +1,119 @@ + + + ExDlgSize + + + + 0 + 0 + 236 + 128 + + + + Dialog + + + + + + 设置表格行列数: + + + + + + 6 + + + + + + + 行数: + + + + + + + 列数: + + + + + + + 5 + + + + + + + + + + + + 确定 + + + + :/images/Image005.png:/images/Image005.png + + + + + + + 关闭 + + + + :/images/Image007.png:/images/Image007.png + + + + + + + + + + + + + btnOk + clicked() + ExDlgSize + accept() + + + 62 + 105 + + + 117 + 63 + + + + + btnClose + clicked() + ExDlgSize + reject() + + + 173 + 105 + + + 117 + 63 + + + + + diff --git a/QtCustomDialogEx/QtCustomDialogEx.pro b/QtCustomDialogEx/QtCustomDialogEx.pro new file mode 100644 index 0000000..3269873 --- /dev/null +++ b/QtCustomDialogEx/QtCustomDialogEx.pro @@ -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 diff --git a/QtCustomDialogEx/images/Image001.png b/QtCustomDialogEx/images/Image001.png new file mode 100644 index 0000000..97d62a4 Binary files /dev/null and b/QtCustomDialogEx/images/Image001.png differ diff --git a/QtCustomDialogEx/images/Image002.png b/QtCustomDialogEx/images/Image002.png new file mode 100644 index 0000000..8f0257e Binary files /dev/null and b/QtCustomDialogEx/images/Image002.png differ diff --git a/QtCustomDialogEx/images/Image003.png b/QtCustomDialogEx/images/Image003.png new file mode 100644 index 0000000..c939e96 Binary files /dev/null and b/QtCustomDialogEx/images/Image003.png differ diff --git a/QtCustomDialogEx/images/Image004.jpg b/QtCustomDialogEx/images/Image004.jpg new file mode 100644 index 0000000..08aae9d Binary files /dev/null and b/QtCustomDialogEx/images/Image004.jpg differ diff --git a/QtCustomDialogEx/images/Image005.png b/QtCustomDialogEx/images/Image005.png new file mode 100644 index 0000000..686336c Binary files /dev/null and b/QtCustomDialogEx/images/Image005.png differ diff --git a/QtCustomDialogEx/images/Image006.png b/QtCustomDialogEx/images/Image006.png new file mode 100644 index 0000000..328ba50 Binary files /dev/null and b/QtCustomDialogEx/images/Image006.png differ diff --git a/QtCustomDialogEx/images/Image007.png b/QtCustomDialogEx/images/Image007.png new file mode 100644 index 0000000..9d00551 Binary files /dev/null and b/QtCustomDialogEx/images/Image007.png differ diff --git a/QtCustomDialogEx/main.cpp b/QtCustomDialogEx/main.cpp new file mode 100644 index 0000000..e6a3ade --- /dev/null +++ b/QtCustomDialogEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExCustomMainWin.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExCustomMainWin w; + w.show(); + + return a.exec(); +} diff --git a/QtCustomDialogEx/qt.ico b/QtCustomDialogEx/qt.ico new file mode 100644 index 0000000..0ee0568 Binary files /dev/null and b/QtCustomDialogEx/qt.ico differ diff --git a/QtCustomDialogEx/resources.qrc b/QtCustomDialogEx/resources.qrc new file mode 100644 index 0000000..fd9c464 --- /dev/null +++ b/QtCustomDialogEx/resources.qrc @@ -0,0 +1,11 @@ + + + images/Image001.png + images/Image002.png + images/Image003.png + images/Image004.jpg + images/Image005.png + images/Image006.png + images/Image007.png + +