diff --git a/QtCustomDialogEx/ExCustomMainWin.cpp b/QtCustomDialogEx/ExCustomMainWin.cpp index 4123fd8..03ea3b1 100644 --- a/QtCustomDialogEx/ExCustomMainWin.cpp +++ b/QtCustomDialogEx/ExCustomMainWin.cpp @@ -8,13 +8,30 @@ ExCustomMainWin::ExCustomMainWin(QWidget *parent) : ui(new Ui::ExCustomMainWin) { ui->setupUi(this); - setWindowTitle(QObject::tr("自定义对话框的用法")); + setWindowTitle(QObject::tr("自定义和标准对话框的用法")); m_model = nullptr; m_dlglocate = nullptr; m_dlgSetHeaders = nullptr; - m_model = new QStandardItemModel(this); + m_model = new QStandardItemModel(10, 5, this); //创建数据模型 + m_seleModel = new QItemSelectionModel(m_model); //Item选择模型 + connect(m_seleModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)), this,SLOT(on_currentChanged(QModelIndex,QModelIndex))); + + ui->tableView->setModel(m_model); + ui->tableView->setSelectionModel(m_seleModel); + + //创建状态栏组件 + m_labCellPos = new QLabel("当前单元格:",this); + m_labCellPos->setMinimumWidth(180); + m_labCellPos->setAlignment(Qt::AlignHCenter); + + m_labCellText = new QLabel("单元格内容:",this); + m_labCellText->setMinimumWidth(200); + + + ui->statusBar->addWidget(m_labCellPos); + ui->statusBar->addWidget(m_labCellText); } ExCustomMainWin::~ExCustomMainWin() @@ -22,12 +39,25 @@ 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); + m_model->setData(index, text, Qt::DisplayRole); //设置单元格字符串 +} + +//设置actLocatee的enabled属性 +void ExCustomMainWin::setActLocateEnable(bool enable) +{ + ui->actLocate->setEnabled(enable); +} + +//将ExDlgLocate指针设置为NULL +void ExCustomMainWin::setDlgLocateNull() +{ + m_dlglocate = nullptr; } void ExCustomMainWin::on_actSetHeader_triggered() @@ -89,3 +119,15 @@ void ExCustomMainWin::on_actLocate_triggered() m_dlglocate->show(); } + +void ExCustomMainWin::on_currentChanged(const QModelIndex ¤t, const QModelIndex &previous) +{ + if (current.isValid()) { + //状态栏的显示 + QStandardItem* item; + item = m_model->itemFromIndex(current); + + m_labCellPos->setText(QString::asprintf("当前单元格:%d行,%d列", current.row(),current.column())); //显示模型索引的行和列号 + m_labCellText->setText("单元格内容:"+ item->text()); //显示item的文字内容 + } +} diff --git a/QtCustomDialogEx/ExCustomMainWin.h b/QtCustomDialogEx/ExCustomMainWin.h index 2e464d0..42ab518 100644 --- a/QtCustomDialogEx/ExCustomMainWin.h +++ b/QtCustomDialogEx/ExCustomMainWin.h @@ -8,6 +8,7 @@ #include "ExDlgSize.h" #include "ExDlgSetHeaders.h" #include "ExDlgLocate.h" +#include namespace Ui { class ExCustomMainWin; @@ -21,20 +22,25 @@ public: explicit ExCustomMainWin(QWidget *parent = nullptr); ~ExCustomMainWin(); - void setACellText(int row, int col, QString text); //设置一个单元格,有ExDlgLocate调用 + void setACellText(int row, int col, QString text); //定位到单元格,并设置字符串 + void setActLocateEnable(bool enable); //设置actLocatee的enabled属性 + void setDlgLocateNull(); //将ExDlgLocate指针设置为NULL private slots: void on_actSetHeader_triggered(); void on_actSetSize_triggered(); void on_actLocate_triggered(); + void on_currentChanged(const QModelIndex ¤t, const QModelIndex &previous); private: Ui::ExCustomMainWin *ui; - QStandardItemModel *m_model; //数据模型 - QItemSelectionModel *m_seleModel; //item选择模型 + QStandardItemModel *m_model; //数据模型 + QItemSelectionModel *m_seleModel; //item选择模型 ExDlgSetHeaders *m_dlgSetHeaders; ExDlgLocate *m_dlglocate; + QLabel *m_labCellPos; //当前单元格行列号 + QLabel *m_labCellText; //当前单元格内容 }; #endif // EXCUSTOMMAINWIN_H diff --git a/QtCustomDialogEx/ExCustomMainWin.ui b/QtCustomDialogEx/ExCustomMainWin.ui index 4aeb527..96ea798 100644 --- a/QtCustomDialogEx/ExCustomMainWin.ui +++ b/QtCustomDialogEx/ExCustomMainWin.ui @@ -100,5 +100,22 @@ - + + + actQuit + triggered() + ExCustomMainWin + close() + + + -1 + -1 + + + 407 + 235 + + + + diff --git a/QtCustomDialogEx/ExDlgLocate.cpp b/QtCustomDialogEx/ExDlgLocate.cpp index 17375b9..6b57fe9 100644 --- a/QtCustomDialogEx/ExDlgLocate.cpp +++ b/QtCustomDialogEx/ExDlgLocate.cpp @@ -2,6 +2,7 @@ #include "ui_ExDlgLocate.h" #include "ExCustomMainWin.h" +#include ExDlgLocate::ExDlgLocate(QWidget *parent) : QDialog(parent), @@ -12,30 +13,31 @@ ExDlgLocate::ExDlgLocate(QWidget *parent) : ExDlgLocate::~ExDlgLocate() { + QMessageBox::information(this,"提示","单元格定位对话框被删除"); delete ui; } +//设置spin的设定(最大)值 void ExDlgLocate::setSpinRange(int rowCount, int colCount) { - + ui->spinBoxRow->setMaximum(rowCount - 1); + ui->spinBoxCol->setMaximum(colCount - 1); } +//设置spin的初始值 void ExDlgLocate::setSpinValue(int rowNo, int colNo) { - -} - -void ExDlgLocate::closeEvent(QCloseEvent *e) -{ - + ui->spinBoxRow->setValue(rowNo); + ui->spinBoxCol->setValue(colNo); } +//设置文字 void ExDlgLocate::on_btnSetText_clicked() { int row = ui->spinBoxRow->value(); //定位到单元格,并且设置字符串 int col = ui->spinBoxCol->value(); - ExCustomMainWin *parWind = new ExCustomMainWin(nullptr); //向具体的item中填写字符串 + ExCustomMainWin* parWind = (ExCustomMainWin*)parentWidget(); //向具体的item中填写字符串 parWind->setACellText(row, col, ui->lineEdit->text()); if (ui->checkBoxRow->isChecked()) @@ -43,3 +45,23 @@ void ExDlgLocate::on_btnSetText_clicked() if (ui->checkBoxCol->isChecked()) ui->spinBoxCol->setValue(1 + ui->spinBoxCol->value()); } + +//窗口关闭事件,关闭时释放本窗口 +void ExDlgLocate::closeEvent(QCloseEvent *event) +{ + ExCustomMainWin* parWind = (ExCustomMainWin*)parentWidget(); //获取父窗口指针 + parWind->setActLocateEnable(true); //设置 actLocatee 为true + parWind->setDlgLocateNull(); //将窗口指针设置为NULL +} + +//窗口显示事件 +void ExDlgLocate::showEvent(QShowEvent *event) +{ + ExCustomMainWin* parWind = (ExCustomMainWin*)parentWidget(); + parWind->setActLocateEnable(false); +} + +void ExDlgLocate::on_btnClose_clicked() +{ + +} diff --git a/QtCustomDialogEx/ExDlgLocate.h b/QtCustomDialogEx/ExDlgLocate.h index 7c60711..6b54c3b 100644 --- a/QtCustomDialogEx/ExDlgLocate.h +++ b/QtCustomDialogEx/ExDlgLocate.h @@ -18,17 +18,20 @@ public: explicit ExDlgLocate(QWidget *parent = nullptr); ~ExDlgLocate(); - void setSpinRange(int rowCount, int colCount); //设置spin的设定值 - void setSpinValue(int rowNo, int colNo); //设置spin的显示值 + 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); + void on_btnClose_clicked(); private: Ui::ExDlgLocate *ui; + + // QWidget interface +protected: + virtual void closeEvent(QCloseEvent *event) override; //窗口关闭事件,关闭时释放本窗口 + virtual void showEvent(QShowEvent *event) override; //窗口显示事件 }; #endif // EXDLGLOCATE_H diff --git a/QtCustomDialogEx/ExDlgLocate.ui b/QtCustomDialogEx/ExDlgLocate.ui index 0c6596e..0383193 100644 --- a/QtCustomDialogEx/ExDlgLocate.ui +++ b/QtCustomDialogEx/ExDlgLocate.ui @@ -98,5 +98,22 @@ - + + + btnClose + clicked() + ExDlgLocate + close() + + + 366 + 136 + + + 206 + 79 + + + + diff --git a/QtCustomDialogEx/ExDlgSetHeaders.cpp b/QtCustomDialogEx/ExDlgSetHeaders.cpp index 7bb52fe..48cb1eb 100644 --- a/QtCustomDialogEx/ExDlgSetHeaders.cpp +++ b/QtCustomDialogEx/ExDlgSetHeaders.cpp @@ -8,6 +8,7 @@ ExDlgSetHeaders::ExDlgSetHeaders(QWidget *parent) : { ui->setupUi(this); m_model = new QStringListModel(this); + ui->listView->setModel(m_model); } ExDlgSetHeaders::~ExDlgSetHeaders() diff --git a/README.md b/README.md index 8bbda4c..e5a53a6 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ - [时间日期(`QTime`/`QDate`/`QDateTime`)和定时器(`QTimer`)的介绍和使用](https://blog.csdn.net/qq_33154343/article/details/101040841)【QtDateTimeEx】 - [`QComboBox`(下拉列表框)和`QPlainTextEdit`(多行富文本编辑器)的用法](https://blog.csdn.net/qq_33154343/article/details/101127870) 【QtQcomboBoxEx】 - [列表控件`QListWidget`和工具按钮`QToolButton`的和用法](https://blog.csdn.net/qq_33154343/article/details/101314908)【QtQListWidgetEx】 -- 【QtQTreeWidgetEx】 +- 目录树组件`QTreeWidget`和停靠区域组件`QDockWidget`的用法【QtQTreeWidgetEx】 - 【QtQTableWidgetEx】 - 【QtQFileSystemModelEx】