第五步,添加通过“单元”对话框,设置单元格文字的功能。
C:\Users\Minwei\Projects\Qt\Sheet\celldialog.h:
xxxxxxxxxx
181...
2class CellDialog : public QDialog
3{
4 ...
5protected:
6 void closeEvent(QCloseEvent*);
7 void showEvent(QShowEvent*);
8
9signals:
10 void updateCell(int row, int column, QString const& content);
11 void enableCell(bool enabled);
12
13public slots:
14 void setMaximum(int rowCount, int columnCount);
15 void setCurrent(int row, int column, QString const& content);
16 ...
17};
18...
C:\Users\Minwei\Projects\Qt\Sheet\celldialog.cpp:
xxxxxxxxxx
461...
2CellDialog::CellDialog(QWidget *parent) :
3 ...
4{
5 ...
6 connect(parent, SIGNAL(setMaximum(int, int)),
7 this, SLOT(setMaximum(int, int)));
8 connect(parent, SIGNAL(setCurrent(int, int, QString const&)),
9 this, SLOT(setCurrent(int, int, QString const&)));
10 connect( this, SIGNAL(updateCell(int, int, QString const&)),
11 parent, SLOT(updateCell(int, int, QString const&)));
12 connect( this, SIGNAL(enableCell(bool)),
13 parent, SLOT(enableCell(bool)));
14 ...
15}
16...
17void CellDialog::closeEvent(QCloseEvent*)
18{
19 emit enableCell(true);
20}
21
22void CellDialog::showEvent(QShowEvent*)
23{
24 emit enableCell(false);
25}
26
27void CellDialog::setMaximum(int rowCount, int columnCount)
28{
29 ui->m_spinRow->setMaximum(rowCount);
30 ui->m_spinCol->setMaximum(columnCount);
31}
32
33void CellDialog::setCurrent(int row, int column, QString const& content)
34{
35 ui->m_spinRow->setValue(row);
36 ui->m_spinCol->setValue(column);
37 ui->m_editContent->setText(content);
38}
39
40void CellDialog::on_m_btnUpdate_clicked()
41{
42 emit updateCell(
43 ui->m_spinRow->value() - 1,
44 ui->m_spinCol->value() - 1,
45 ui->m_editContent->text());
46}
C:\Users\Minwei\Projects\Qt\Sheet\sheetwindow.h:
xxxxxxxxxx
201...
2
3...
4class SheetWindow : public QMainWindow
5{
6 ...
7signals:
8 void setMaximum(int rowCount, int columnCount);
9 void setCurrent(int row, int column, QString const& content);
10
11public slots:
12 void updateCell(int row, int column, QString const& content);
13 void enableCell(bool enabled);
14 ...
15private:
16 ...
17 CellDialog m_dlgCell;
18 ...
19};
20...
C:\Users\Minwei\Projects\Qt\Sheet\sheetwindow.cpp:
xxxxxxxxxx
541...
2SheetWindow::SheetWindow(QWidget *parent)
3 ...
4 , m_dlgCell(this)
5 ...
6{
7 ...
8}
9...
10void SheetWindow::updateCell(int row, int column, QString const& content)
11{
12 QModelIndex index = m_model->index(row, column);
13 m_model->setData(index, content, Qt::DisplayRole);
14
15 m_selection->clearSelection();
16 m_selection->setCurrentIndex(index, QItemSelectionModel::Select);
17}
18
19void SheetWindow::enableCell(bool enabled)
20{
21 ui->m_actCell->setEnabled(enabled);
22}
23
24void SheetWindow::on_m_actRowCol_triggered()
25{
26 ...
27 if (dlg.exec() == QDialog::Accepted)
28 {
29 ...
30 emit setMaximum(dlg.getRowCount(), dlg.getColumnCount());
31 ...
32 }
33 ...
34}
35...
36void SheetWindow::on_m_actCell_triggered()
37{
38 m_dlgCell.show();
39}
40
41void SheetWindow::on_m_table_clicked(const QModelIndex &index)
42{
43 emit setCurrent(index.row() + 1, index.column() + 1,
44 index.data().toString());
45}
46
47void SheetWindow::on_m_selection_currentChanged(
48 QModelIndex const& current, QModelIndex const& previous)
49{
50 ...
51 emit setCurrent(current.row() + 1, current.column() + 1, item->text());
52 ...
53}
54...
运行效果如图所示: