feat: QFileDialog QMessageBox QInputDialog QColorDialog and QFontDialog

QFileDialog    文件对话框
QColorDialog   颜色对话框
QFontDialog    字体对话框
QInputDialog   输入对话框
QMessageBox    消息对话框
This commit is contained in:
touwoyimuli 2019-10-08 00:18:34 +08:00
parent 5234f29d58
commit b265921182
8 changed files with 130 additions and 22 deletions

View File

@ -8,13 +8,30 @@ ExCustomMainWin::ExCustomMainWin(QWidget *parent) :
ui(new Ui::ExCustomMainWin) ui(new Ui::ExCustomMainWin)
{ {
ui->setupUi(this); ui->setupUi(this);
setWindowTitle(QObject::tr("自定义对话框的用法")); setWindowTitle(QObject::tr("自定义和标准对话框的用法"));
m_model = nullptr; m_model = nullptr;
m_dlglocate = nullptr; m_dlglocate = nullptr;
m_dlgSetHeaders = 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() ExCustomMainWin::~ExCustomMainWin()
@ -22,12 +39,25 @@ ExCustomMainWin::~ExCustomMainWin()
delete ui; delete ui;
} }
//定位到单元格,并设置字符串
void ExCustomMainWin::setACellText(int row, int col, QString text) void ExCustomMainWin::setACellText(int row, int col, QString text)
{ {
QModelIndex index = m_model->index(row, col); QModelIndex index = m_model->index(row, col);
m_seleModel->clearSelection(); m_seleModel->clearSelection();
m_seleModel->setCurrentIndex(index, QItemSelectionModel::Select); 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() void ExCustomMainWin::on_actSetHeader_triggered()
@ -89,3 +119,15 @@ void ExCustomMainWin::on_actLocate_triggered()
m_dlglocate->show(); m_dlglocate->show();
} }
void ExCustomMainWin::on_currentChanged(const QModelIndex &current, 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的文字内容
}
}

View File

@ -8,6 +8,7 @@
#include "ExDlgSize.h" #include "ExDlgSize.h"
#include "ExDlgSetHeaders.h" #include "ExDlgSetHeaders.h"
#include "ExDlgLocate.h" #include "ExDlgLocate.h"
#include <QLabel>
namespace Ui { namespace Ui {
class ExCustomMainWin; class ExCustomMainWin;
@ -21,12 +22,15 @@ public:
explicit ExCustomMainWin(QWidget *parent = nullptr); explicit ExCustomMainWin(QWidget *parent = nullptr);
~ExCustomMainWin(); ~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: private slots:
void on_actSetHeader_triggered(); void on_actSetHeader_triggered();
void on_actSetSize_triggered(); void on_actSetSize_triggered();
void on_actLocate_triggered(); void on_actLocate_triggered();
void on_currentChanged(const QModelIndex &current, const QModelIndex &previous);
private: private:
Ui::ExCustomMainWin *ui; Ui::ExCustomMainWin *ui;
@ -35,6 +39,8 @@ private:
QItemSelectionModel *m_seleModel; //item选择模型 QItemSelectionModel *m_seleModel; //item选择模型
ExDlgSetHeaders *m_dlgSetHeaders; ExDlgSetHeaders *m_dlgSetHeaders;
ExDlgLocate *m_dlglocate; ExDlgLocate *m_dlglocate;
QLabel *m_labCellPos; //当前单元格行列号
QLabel *m_labCellText; //当前单元格内容
}; };
#endif // EXCUSTOMMAINWIN_H #endif // EXCUSTOMMAINWIN_H

View File

@ -100,5 +100,22 @@
<resources> <resources>
<include location="resources.qrc"/> <include location="resources.qrc"/>
</resources> </resources>
<connections/> <connections>
<connection>
<sender>actQuit</sender>
<signal>triggered()</signal>
<receiver>ExCustomMainWin</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>407</x>
<y>235</y>
</hint>
</hints>
</connection>
</connections>
</ui> </ui>

View File

@ -2,6 +2,7 @@
#include "ui_ExDlgLocate.h" #include "ui_ExDlgLocate.h"
#include "ExCustomMainWin.h" #include "ExCustomMainWin.h"
#include <QMessageBox>
ExDlgLocate::ExDlgLocate(QWidget *parent) : ExDlgLocate::ExDlgLocate(QWidget *parent) :
QDialog(parent), QDialog(parent),
@ -12,30 +13,31 @@ ExDlgLocate::ExDlgLocate(QWidget *parent) :
ExDlgLocate::~ExDlgLocate() ExDlgLocate::~ExDlgLocate()
{ {
QMessageBox::information(this,"提示","单元格定位对话框被删除");
delete ui; delete ui;
} }
//设置spin的设定(最大)值
void ExDlgLocate::setSpinRange(int rowCount, int colCount) 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::setSpinValue(int rowNo, int colNo)
{ {
ui->spinBoxRow->setValue(rowNo);
} ui->spinBoxCol->setValue(colNo);
void ExDlgLocate::closeEvent(QCloseEvent *e)
{
} }
//设置文字
void ExDlgLocate::on_btnSetText_clicked() void ExDlgLocate::on_btnSetText_clicked()
{ {
int row = ui->spinBoxRow->value(); //定位到单元格,并且设置字符串 int row = ui->spinBoxRow->value(); //定位到单元格,并且设置字符串
int col = ui->spinBoxCol->value(); int col = ui->spinBoxCol->value();
ExCustomMainWin *parWind = new ExCustomMainWin(nullptr); //向具体的item中填写字符串 ExCustomMainWin* parWind = (ExCustomMainWin*)parentWidget(); //向具体的item中填写字符串
parWind->setACellText(row, col, ui->lineEdit->text()); parWind->setACellText(row, col, ui->lineEdit->text());
if (ui->checkBoxRow->isChecked()) if (ui->checkBoxRow->isChecked())
@ -43,3 +45,23 @@ void ExDlgLocate::on_btnSetText_clicked()
if (ui->checkBoxCol->isChecked()) if (ui->checkBoxCol->isChecked())
ui->spinBoxCol->setValue(1 + ui->spinBoxCol->value()); 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()
{
}

View File

@ -18,17 +18,20 @@ public:
explicit ExDlgLocate(QWidget *parent = nullptr); explicit ExDlgLocate(QWidget *parent = nullptr);
~ExDlgLocate(); ~ExDlgLocate();
void setSpinRange(int rowCount, int colCount); //设置spin的设定 void setSpinRange(int rowCount, int colCount); //设置spin的设定(最大)
void setSpinValue(int rowNo, int colNo); //设置spin的显示 void setSpinValue(int rowNo, int colNo); //设置spin的初始
private slots: private slots:
void on_btnSetText_clicked(); void on_btnSetText_clicked();
void on_btnClose_clicked();
private:
void closeEvent(QCloseEvent* e);
private: private:
Ui::ExDlgLocate *ui; Ui::ExDlgLocate *ui;
// QWidget interface
protected:
virtual void closeEvent(QCloseEvent *event) override; //窗口关闭事件,关闭时释放本窗口
virtual void showEvent(QShowEvent *event) override; //窗口显示事件
}; };
#endif // EXDLGLOCATE_H #endif // EXDLGLOCATE_H

View File

@ -98,5 +98,22 @@
<resources> <resources>
<include location="resources.qrc"/> <include location="resources.qrc"/>
</resources> </resources>
<connections/> <connections>
<connection>
<sender>btnClose</sender>
<signal>clicked()</signal>
<receiver>ExDlgLocate</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>366</x>
<y>136</y>
</hint>
<hint type="destinationlabel">
<x>206</x>
<y>79</y>
</hint>
</hints>
</connection>
</connections>
</ui> </ui>

View File

@ -8,6 +8,7 @@ ExDlgSetHeaders::ExDlgSetHeaders(QWidget *parent) :
{ {
ui->setupUi(this); ui->setupUi(this);
m_model = new QStringListModel(this); m_model = new QStringListModel(this);
ui->listView->setModel(m_model);
} }
ExDlgSetHeaders::~ExDlgSetHeaders() ExDlgSetHeaders::~ExDlgSetHeaders()

View File

@ -61,7 +61,7 @@
- [时间日期(`QTime`/`QDate`/`QDateTime`)和定时器(`QTimer`)的介绍和使用](https://blog.csdn.net/qq_33154343/article/details/101040841)【QtDateTimeEx】 - [时间日期(`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】 - [`QComboBox`(下拉列表框)和`QPlainTextEdit`(多行富文本编辑器)的用法](https://blog.csdn.net/qq_33154343/article/details/101127870) 【QtQcomboBoxEx】
- [列表控件`QListWidget`和工具按钮`QToolButton`的和用法](https://blog.csdn.net/qq_33154343/article/details/101314908)【QtQListWidgetEx】 - [列表控件`QListWidget`和工具按钮`QToolButton`的和用法](https://blog.csdn.net/qq_33154343/article/details/101314908)【QtQListWidgetEx】
- 【QtQTreeWidgetEx】 - 目录树组件`QTreeWidget`和停靠区域组件`QDockWidget`的用法【QtQTreeWidgetEx】
- 【QtQTableWidgetEx】 - 【QtQTableWidgetEx】
- 【QtQFileSystemModelEx】 - 【QtQFileSystemModelEx】