自定义对话框及其调用

时间:2021-10-01 18:06:41

最近在阅读Qt 5.9 C 开发指南,为了加深对书本上内容的理解,参照书上的讲解尝试写了一些demo,用于以后工作中查阅,如果涉及侵权请告知实例程序samp6_2

mymainwindow.h

#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H

#include "mydialog.h"
#include <QMainWindow>
#include <QWidget>
#include <QAction>
#include <QStandardItemModel>
#include <QTableView>
#include <QItemSelectionModel>
#include <QLabel>

class MyMainWindow : public QMainWindow
{
    Q_OBJECT

private:
    QAction *_actSetRowColumn;
    QAction *_actSetHeader;
    QAction *_actLocateTable;
    QAction *_actExit;
    QTableView *_tableView;
    QLabel *_labStatus;
    QStandardItemModel *_standardItemModel;
    QItemSelectionModel *_itemSelectionModel;
    DialogHeader *_dialogHeader;
    DialogLocate *_dialogLocate;

public:
    MyMainWindow(QWidget *parent = nullptr);
    ~MyMainWindow();

    void iniUI();
    void iniSignalSlots();
    void setCellText(int row, int column, QString text);
    void setActLoacteTableEnabled(bool enable);
    void setDialogLocateNull();
    void setAlignment(Qt::AlignmentFlag flag);

public slots:
    void actSetRowColumnTriggered();
    void actSetHeaderTriggered();
    void actLocateTableTriggered();
    void actExitTriggered();
    void tableViewClicked(const QModelIndex &index);

};

#endif // MYMAINWINDOW_H

mymainwindow.cpp

#include "mymainwindow.h"
#include <QIcon>
#include <QToolBar>
#include <QStatusBar>
#include <QModelIndex>

MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent)
{
    iniUI();
    iniSignalSlots();

    _standardItemModel = new QStandardItemModel(this);
    _tableView->setModel(_standardItemModel);
    _itemSelectionModel = new QItemSelectionModel(_standardItemModel);
    _tableView->setSelectionModel(_itemSelectionModel);

    _dialogHeader = nullptr;
    _dialogLocate = nullptr;
}

MyMainWindow::~MyMainWindow()
{

}

void MyMainWindow::iniUI()
{
    _actSetRowColumn = new QAction(QIcon(":/images/rowcolumn.bmp"), "设置行数列数");
    _actSetHeader = new QAction(QIcon(":/images/header.bmp"), "设置表头标题");
    _actLocateTable = new QAction(QIcon(":/images/locate.bmp"), "定位单元格");
    _actExit = new QAction(QIcon(":/images/exit.bmp"), "退出");

    QToolBar *toolBar = new QToolBar(this);
    toolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    toolBar->addAction(_actSetRowColumn);
    toolBar->addAction(_actSetHeader);
    toolBar->addAction(_actLocateTable);
    toolBar->addAction(_actExit);
    addToolBar(toolBar);

    _tableView = new QTableView();
    setCentralWidget(_tableView);

    _labStatus = new QLabel();
    QStatusBar *statusBar = new QStatusBar();
    statusBar->addWidget(_labStatus);
    setStatusBar(statusBar);

    resize(1100, 600);
}

void MyMainWindow::iniSignalSlots()
{
    connect(_actSetRowColumn, SIGNAL(triggered()), this, SLOT(actSetRowColumnTriggered()));
    connect(_actSetHeader, SIGNAL(triggered()), this, SLOT(actSetHeaderTriggered()));
    connect(_actLocateTable, SIGNAL(triggered()), this, SLOT(actLocateTableTriggered()));
    connect(_actExit, SIGNAL(triggered()), this, SLOT(actExitTriggered()));
    connect(_tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(tableViewClicked(QModelIndex)));
}

void MyMainWindow::actSetRowColumnTriggered()
{
    DialogSize *dialogSize = new DialogSize(this);
    Qt::WindowFlags flags = dialogSize->windowFlags();
    dialogSize->setWindowFlags(flags | Qt::MSWindowsFixedSizeDialogHint);
    dialogSize->setRow(_standardItemModel->rowCount());
    dialogSize->setColumn(_standardItemModel->columnCount());

    int result = dialogSize->exec();
    if(result == QDialog::Accepted)
    {
        int row = dialogSize->row();
        _standardItemModel->setRowCount(row);
        int column = dialogSize->column();
        _standardItemModel->setColumnCount(column);
    }

    delete dialogSize;
}

void MyMainWindow::actSetHeaderTriggered()
{
    if(_dialogHeader == nullptr)
    {
        _dialogHeader = new DialogHeader(this);
    }

    QStringList headerList;
    for(int i = 0; i < _standardItemModel->columnCount(); i  )
    {
        QString header = _standardItemModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString();
        headerList.append(header);
    }
    _dialogHeader->setHeaderList(headerList);

    int result = _dialogHeader->exec();
    if(result == QDialog::Accepted)
    {
        headerList.clear();
        headerList = _dialogHeader->headerList();
        _standardItemModel->setHorizontalHeaderLabels(headerList);
    }
}

void MyMainWindow::actLocateTableTriggered()
{
    _actLocateTable->setEnabled(false);

    _dialogLocate = new DialogLocate(this);
    _dialogLocate->setAttribute(Qt::WA_DeleteOnClose);
    Qt::WindowFlags flags = _dialogLocate->windowFlags();
    _dialogLocate->setWindowFlags(flags | Qt::WindowStaysOnTopHint);
    _dialogLocate->setSpinRange(_standardItemModel->rowCount(), _standardItemModel->columnCount());

    QModelIndex index = _itemSelectionModel->currentIndex();
    if(index.isValid())
    {
        _dialogLocate->setSpinValue(index.row(), index.column());
    }
    _dialogLocate->show();
}

void MyMainWindow::actExitTriggered()
{
    close();
}

void MyMainWindow::setCellText(int row, int column, QString text)
{
    QModelIndex index = _standardItemModel->index(row, column);
    _standardItemModel->setData(index, text);
    _itemSelectionModel->clearSelection();
    _itemSelectionModel->setCurrentIndex(index, QItemSelectionModel::Select);
}

void MyMainWindow::tableViewClicked(const QModelIndex &index)
{
    if(_dialogLocate != nullptr)
    {
        _dialogLocate->setSpinValue(index.row(), index.column());
    }
}

void MyMainWindow::setActLoacteTableEnabled(bool enable)
{
    _actLocateTable->setEnabled(enable);
}

void MyMainWindow::setDialogLocateNull()
{
    _dialogLocate = nullptr;
}

void MyMainWindow::setAlignment(Qt::AlignmentFlag flag)
{
    for(int i = 0; i < _standardItemModel->columnCount(); i  )
    {
        QList<QStandardItem *> standardItems = _standardItemModel->takeColumn(i);
        for(int j = 0; j < standardItems.size(); j  )
        {
            QStandardItem *standardItem = standardItems.at(i);
            standardItem->setTextAlignment(flag);
        }
    }
}

mydialog.h

#ifndef MYDIALOG_H
#define MYDIALOG_H

#include <QDialog>
#include <QSpinBox>
#include <QWidget>
#include <QStringListModel>
#include <QListView>
#include <QPushButton>
#include <QLineEdit>

class DialogSize : public QDialog
{
    Q_OBJECT

private:
    QSpinBox *_sbRow;
    QSpinBox *_sbColumn;
    QPushButton *_btnOK;
    QPushButton *_btnCancel;

public:
    DialogSize(QWidget *parent = nullptr);
    ~DialogSize();
    void iniUI();
    void iniSignalSlots();
    int row();
    int column();
    void setRow(int row);
    void setColumn(int column);

};

class DialogHeader : public QDialog
{
    Q_OBJECT

private:
    QPushButton *_btnOK;
    QPushButton *_btnCancel;
    QStringListModel *_stringListModel;
    QListView *_listView;


public:
    DialogHeader(QWidget *parent = nullptr);
    ~DialogHeader();
    void iniUI();
    void iniSignalSlots();
    void setHeaderList(QStringList &stringList);
    QStringList headerList();
};

class DialogLocate : public QDialog
{
    Q_OBJECT

private:
    QSpinBox *_sbRow;
    QSpinBox *_sbColumn;
    QLineEdit *_text;
    QPushButton *_btnOK;
    QPushButton *_btnClose;

public:
    DialogLocate(QWidget *parent);
    ~DialogLocate();

    void iniUI();
    void iniSignalSlots();
    void setSpinRange(int maxRow, int maxColumn);
    void setSpinValue(int row, int column);
    void closeEvent(QCloseEvent *e);

public slots:
    void btnOKClicked();

};

#endif // MYDIALOG_H

mydialog.cpp

#include "mydialog.h"
#include "mymainwindow.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGroupBox>
#include <QIcon>

DialogSize::DialogSize(QWidget *parent) : QDialog(parent)
{
    iniUI();
    iniSignalSlots();
}

DialogSize::~DialogSize()
{

}

void DialogSize::iniUI()
{
    _sbRow = new QSpinBox();
    _sbRow->setMinimum(0);
    _sbRow->setMaximum(20);
    _sbRow->setValue(10);
    _sbColumn = new QSpinBox();
    _sbColumn->setMinimum(0);
    _sbColumn->setMaximum(20);
    _sbColumn->setValue(10);
    _btnOK = new QPushButton("确定");
    _btnCancel = new QPushButton("取消");

    QLabel *lab1 = new QLabel("行数");
    QLabel *lab2 = new QLabel("列数");
    QHBoxLayout *layout1 = new QHBoxLayout();
    layout1->addWidget(lab1);
    layout1->addWidget(_sbRow);
    QHBoxLayout *layout2 = new QHBoxLayout();
    layout2->addWidget(lab2);
    layout2->addWidget(_sbColumn);
    QVBoxLayout *layout3 = new QVBoxLayout();
    layout3->addLayout(layout1);
    layout3->addLayout(layout2);

    QGroupBox *box = new QGroupBox("设置表格行数和列数");
    box->setLayout(layout3);

    QVBoxLayout *layout4 = new QVBoxLayout();
    layout4->addWidget(_btnOK);
    layout4->addWidget(_btnCancel);

    QHBoxLayout *layout5 = new QHBoxLayout();
    layout5->addWidget(box);
    layout5->addLayout(layout4);

    setLayout(layout5);
    setWindowTitle("设置表格行数和列数");
    resize(200,100);
}

void DialogSize::iniSignalSlots()
{
    connect(_btnOK, SIGNAL(clicked()), this, SLOT(accept()));
    connect(_btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
}

int DialogSize::row()
{
    return _sbRow->value();
}

int DialogSize::column()
{
    return _sbColumn->value();
}

void DialogSize::setRow(int row)
{
    _sbRow->setValue(row);
}

void DialogSize::setColumn(int column)
{
    _sbColumn->setValue(column);
}

DialogHeader::DialogHeader(QWidget *parent) : QDialog (parent)
{
    iniUI();
    iniSignalSlots();

    _stringListModel = new QStringListModel(this);
    _listView->setModel(_stringListModel);
}

DialogHeader::~DialogHeader()
{

}

void DialogHeader::iniUI()
{
    _btnOK = new QPushButton(QIcon(":/images/ok.bmp"),"确定");
    _btnCancel = new QPushButton(QIcon(":/images/cancel.bmp"),"取消");

    _listView = new QListView();
    QHBoxLayout *layout1 = new QHBoxLayout();
    layout1->addWidget(_listView);
    QGroupBox *box = new QGroupBox("表头标题");
    box->setLayout(layout1);

    QVBoxLayout *layout2 = new QVBoxLayout();
    layout2->addWidget(_btnOK);
    layout2->addWidget(_btnCancel);

    QHBoxLayout *layout3 = new QHBoxLayout();
    layout3->addWidget(box);
    layout3->addLayout(layout2);

    setLayout(layout3);
    setWindowTitle("设置表头标题");
    resize(200,100);
}

void DialogHeader::iniSignalSlots()
{
    connect(_btnOK, SIGNAL(clicked()), this, SLOT(accept()));
    connect(_btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
}

QStringList DialogHeader::headerList()
{
    return _stringListModel->stringList();
}

void DialogHeader::setHeaderList(QStringList &stringList)
{
    _stringListModel->setStringList(stringList);
}

DialogLocate::DialogLocate(QWidget *parent) : QDialog (parent)
{
    iniUI();
    iniSignalSlots();


}

DialogLocate::~DialogLocate()
{

}

void DialogLocate::iniUI()
{
    _sbRow = new QSpinBox();
    _sbColumn = new QSpinBox();
    _btnOK = new QPushButton("设定文字");
    _btnClose = new QPushButton("关闭");
    _text = new QLineEdit();

    QLabel *lab1 = new QLabel("行号");
    QLabel *lab2 = new QLabel("列号");
    QLabel *lab3 = new QLabel("设定文字");

    QHBoxLayout *layout1 = new QHBoxLayout();
    layout1->addWidget(lab1);
    layout1->addWidget(_sbRow);

    QHBoxLayout *layout2 = new QHBoxLayout();
    layout2->addWidget(lab2);
    layout2->addWidget(_sbColumn);

    QHBoxLayout *layout3 = new QHBoxLayout();
    layout3->addWidget(lab3);
    layout3->addWidget(_text);

    QVBoxLayout *layout4 = new QVBoxLayout();
    layout4->addLayout(layout1);
    layout4->addLayout(layout2);
    layout4->addLayout(layout3);

    QGroupBox *box = new QGroupBox();
    box->setLayout(layout4);

    QVBoxLayout *layout5= new QVBoxLayout();
    layout5->addWidget(_btnOK);
    layout5->addWidget(_btnClose);

    QHBoxLayout *layout6 = new QHBoxLayout();
    layout6->addWidget(box);
    layout6->addLayout(layout5);

    setLayout(layout6);
    resize(300,150);
}

void DialogLocate::iniSignalSlots()
{
    connect(_btnOK, SIGNAL(clicked()), this, SLOT(btnOKClicked()));
    connect(_btnClose, SIGNAL(clicked()), this, SLOT(close()));
}

void DialogLocate::btnOKClicked()
{
    int row = _sbRow->value();
    int column = _sbColumn->value();
    QString text = _text->text();
    MyMainWindow *mainWindow = static_cast<MyMainWindow *>(parentWidget());
    mainWindow->setCellText(row, column, text);
}

void DialogLocate::setSpinRange(int maxRow, int maxColumn)
{
    _sbRow->setRange(0, maxRow);
    _sbColumn->setRange(0, maxColumn);
}

void DialogLocate::setSpinValue(int row, int column)
{
    _sbRow->setValue(row);
    _sbColumn->setValue(column);
}

void DialogLocate::closeEvent(QCloseEvent *e)
{
    MyMainWindow *mainWindow = static_cast<MyMainWindow *>(parentWidget());
    mainWindow->setActLoacteTableEnabled(true);
    mainWindow->setDialogLocateNull();
}

main.cpp

#include "mymainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyMainWindow w;
    w.show();

    return a.exec();