多窗体应用程序设计

界面

image

代码

qwmainwindow.h

#ifndef QWMAINWINDOW_H
#define QWMAINWINDOW_H

#include <QMainWindow>

#include    "qformdoc.h"
#include    "qformtable.h"

namespace Ui {
class QWMainWindow;
}

class QWMainWindow : public QMainWindow
{
    Q_OBJECT
private:
    //重载函数,可在里面加入图片
    void    paintEvent(QPaintEvent *event);

public:
    explicit QWMainWindow(QWidget *parent = 0);
    ~QWMainWindow();

private slots:
    void on_tabWidget_tabCloseRequested(int index);

    void on_actWidgetInsite_triggered();

    void on_actWindowInsite_triggered();

    void on_actWindow_triggered();

    void on_actWidget_triggered();

    void on_tabWidget_currentChanged(int index);

private:
    Ui::QWMainWindow *ui;
};

#endif // QWMAINWINDOW_H

qwmainwindow.cpp

#include "qwmainwindow.h"
#include "ui_qwmainwindow.h"

#include    <QPainter>
#include    <QFileDialog>
#include    "qwmisc.h"

void QWMainWindow::paintEvent(QPaintEvent *event)
{ //绘制窗口背景图片
    Q_UNUSED(event);
    QPainter painter(this);
    // x, y, w.h
    //0,工具栏下面第一行,this(mainwindow)的宽度,户界面减去上下工具栏和状态栏的高度
    painter.drawPixmap(0,ui->mainToolBar->height(),this->width(),
                       this->height()-ui->mainToolBar->height()-ui->statusBar->height(),
                       QPixmap(":/images/images/back2.jpg"));
}

QWMainWindow::QWMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::QWMainWindow)
{
    ui->setupUi(this);
    ui->tabWidget->setVisible(false); //希望刚启动的时候图片显示出来,所以可视化设为false
    ui->tabWidget->clear();//清除所有页面
    ui->tabWidget->tabsClosable(); //Page有关闭按钮,可被关闭

    this->setCentralWidget(ui->tabWidget);
//    this->setWindowState(Qt::WindowFullScreen); //窗口最大化显示
    this->setWindowState(Qt::WindowMaximized); //窗口最大化显示
//    this->setWindowState(Qt::WindowMinimized); //窗口最大化显示
    this->setAutoFillBackground(true);

//    this->setAttribute(Qt::WA_DeleteOnClose); //关闭时自动删除

//    Qt::WindowFlags    flags=this->windowFlags();
//    this->setWindowFlags(flags | Qt::MSWindowsFixedSizeDialogHint); //设置对话框固定大小
}

QWMainWindow::~QWMainWindow()
{
    delete ui;
}

void QWMainWindow::on_tabWidget_tabCloseRequested(int index)
{//关闭Tab
    if (index<0)
        return;

    QWidget* aForm=ui->tabWidget->widget(index);
    aForm->close();
}


void QWMainWindow::on_actWidgetInsite_triggered()
{ //创建QFormDoc窗体,并在tabWidget中显示
    QFormDoc *formDoc = new QFormDoc(this); //不指定父窗口,单独用show()方法显示
    formDoc->setAttribute(Qt::WA_DeleteOnClose); //关闭时自动删除

//    formDoc->setWindowFlag(Qt::Widget,true);
//    formDoc->setWindowFlag(Qt::CustomizeWindowHint,true);
//    formDoc->setWindowFlag(Qt::WindowMinMaxButtonsHint,false);

//    formDoc->setWindowState(Qt::WindowMaximized);
//    formDoc->setWindowOpacity(0.5);
//    formDoc->setWindowModality(Qt::WindowModal);

//    formDoc->show(); //在单独的窗口中显示
//    formDoc->setWindowTitle("基于QWidget的窗口,无父窗口,关闭时删除");
    //返回的是当前第几个
    int cur=ui->tabWidget->addTab(formDoc,
            QString::asprintf("Doc %d",ui->tabWidget->count()));//参数2是标题
    ui->tabWidget->setCurrentIndex(cur);
    ui->tabWidget->setVisible(true);
}

void QWMainWindow::on_actWindowInsite_triggered()
{
    QFormTable *formTable = new QFormTable(this);
    formTable->setAttribute(Qt::WA_DeleteOnClose); //关闭时自动删除
    //    aTable->setWindowTitle("基于QWidget的窗口,无父窗口,关闭时删除");
    int cur=ui->tabWidget->addTab(formTable,
              QString::asprintf("Table %d",ui->tabWidget->count()));
    ui->tabWidget->setCurrentIndex(cur);
    ui->tabWidget->setVisible(true);
}

void QWMainWindow::on_actWindow_triggered()
{
    QFormTable* formTable = new QFormTable(this);
//    formChart = new QFormTable(); //无父窗体,在windows任务栏上有显示
    formTable->setAttribute(Qt::WA_DeleteOnClose); //对话框关闭时自动删除对话框对象,用于不需要读取返回值的对话框
    formTable->setWindowTitle("基于QMainWindow的窗口,指定父窗口,关闭时删除");
//    formChart->setWindowTitle("多实例窗口,指定父窗口");
    formTable->show();
}


void QWMainWindow::on_actWidget_triggered()
{
    QFormDoc *formDoc = new QFormDoc(); //不指定父窗口,用show()显示
    formDoc->setAttribute(Qt::WA_DeleteOnClose); //关闭时自动删除
    formDoc->setWindowTitle("基于QWidget的窗体,无父窗口,关闭时删除");

    formDoc->setWindowFlag(Qt::Window,true);

//    formDoc->setWindowFlag(Qt::CustomizeWindowHint,true);
//    formDoc->setWindowFlag(Qt::WindowMinMaxButtonsHint,true);
//    formDoc->setWindowFlag(Qt::WindowCloseButtonHint,true);
//    formDoc->setWindowFlag(Qt::WindowStaysOnTopHint,true);

//    formDoc->setWindowState(Qt::WindowMaximized);
    formDoc->setWindowOpacity(0.9);
//    formDoc->setWindowModality(Qt::WindowModal);

    formDoc->show(); //在单独的窗口中显示
}

void QWMainWindow::on_tabWidget_currentChanged(int index)
{
    Q_UNUSED(index);
    bool  en=ui->tabWidget->count()>0; //再无页面时,actions禁用
    ui->tabWidget->setVisible(en);
}

qformdoc.h

#ifndef QFORMDOC_H
#define QFORMDOC_H

#include <QWidget>

namespace Ui {
class QFormDoc;
}

class QFormDoc : public QWidget
{
    Q_OBJECT

private:
    QString mCurrentFile; //当前文件

public:
    explicit QFormDoc(QWidget *parent = 0);
    ~QFormDoc();

    void    loadFromFile(QString& aFileName);

private slots:
    void on_actOpen_triggered();

    void on_actFont_triggered();

private:
    Ui::QFormDoc *ui;
};

#endif // QFORMDOC_H

qformdoc.cpp

#include "qformdoc.h"
#include "ui_qformdoc.h"

#include    <QToolBar>
#include    <QVBoxLayout>
#include    <QFileDialog>
#include    <QFile>
#include    <QTextStream>
#include    <QFontDialog>
#include    <QFileInfo>
#include    <QMessageBox>

#include    <qwmainwindow.h>

QFormDoc::QFormDoc(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::QFormDoc)
{
    ui->setupUi(this);

//使用UI设计的Actions设计工具栏
    QToolBar* locToolBar = new QToolBar(tr("文档"),this); //创建工具栏
    locToolBar->addAction(ui->actOpen);
    locToolBar->addAction(ui->actFont);
    locToolBar->addSeparator();
    locToolBar->addAction(ui->actCut);
    locToolBar->addAction(ui->actCopy);
    locToolBar->addAction(ui->actPaste);
    locToolBar->addAction(ui->actUndo);
    locToolBar->addAction(ui->actRedo);
    locToolBar->addSeparator();
    locToolBar->addAction(ui->actClose);

    locToolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
//    locToolBar->setAutoFillBackground(true);

    QVBoxLayout *Layout = new QVBoxLayout();
    Layout->addWidget(locToolBar); //设置工具栏和编辑器上下布局
    Layout->addWidget(ui->plainTextEdit);
    Layout->setContentsMargins(2,2,2,2); //减小边框的宽度
    Layout->setSpacing(2);
    this->setLayout(Layout); //设置布局
//    this->setAutoFillBackground(true); //避免工具栏显示背景图片
}

QFormDoc::~QFormDoc()
{
//    QMessageBox::information(this, "消息", "QFormDoc对象被删除和释放");
    delete ui;
}

void QFormDoc::loadFromFile(QString &aFileName)
{
    QString str;
    QFile aFile(aFileName);  //以文件方式读出
    if (aFile.open(QIODevice::ReadOnly | QIODevice::Text)) //以只读文本方式打开文件
    {
        QTextStream aStream(&aFile); //用文本流读取文件
        ui->plainTextEdit->clear();//清空
        while (!aStream.atEnd())
        {
            str=aStream.readLine();//读取文件的一行
            ui->plainTextEdit->appendPlainText(str); //添加到文本框显示
        }
        aFile.close();//关闭文件

        mCurrentFile=aFileName;
        QFileInfo   fileInfo(aFileName);
        QString shorName=fileInfo.fileName();
        this->setWindowTitle(shorName);
    }
}

void QFormDoc::on_actOpen_triggered()
{
//    curPath=QCoreApplication::applicationDirPath(); //获取应用程序的路径
    QString curPath=QDir::currentPath();
//调用打开文件对话框打开一个文件
    QString aFileName=QFileDialog::getOpenFileName(this,tr("打开一个文件"),curPath,
                 "C程序文件文件(*.h *cpp);;文本文件(*.txt);;所有文件(*.*)");

    if (aFileName.isEmpty())
        return; //如果未选择文件,退出

    loadFromFile(aFileName);
}

void QFormDoc::on_actFont_triggered()
{
    QFont   font;
    font=ui->plainTextEdit->font();

    bool    ok;
    font=QFontDialog::getFont(&ok,font);
    ui->plainTextEdit->setFont(font);

}

qformtable.h

#ifndef QFORMTABLE_H
#define QFORMTABLE_H

#include <QMainWindow>

#include    <QStandardItemModel>

#include    <QItemSelectionModel>

#include    "qwdialogheaders.h"
#include    "qwdialogsize.h"

namespace Ui {
class QFormTable;
}

class QFormTable : public QMainWindow
{
    Q_OBJECT

private:
    QWDialogHeaders *dlgSetHeaders=NULL;//设置表头文字对话框, 一次创建,exec()重复调用

    QStandardItemModel  *theModel;//数据模型
    QItemSelectionModel *theSelection;//Item选择模型

public:
    explicit QFormTable(QWidget *parent = 0);
    ~QFormTable();

private slots:
    void on_actSize_triggered();

    void on_actSetHeader_triggered();

private:
    Ui::QFormTable *ui;
};

#endif // QFORMTABLE_H

qformtable.cpp

#include "qformtable.h"
#include "ui_qformtable.h"

#include    <QMessageBox>

QFormTable::QFormTable(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::QFormTable)
{
    ui->setupUi(this);

    theModel = new QStandardItemModel(12,6,this); //数据模型
    theSelection = new QItemSelectionModel(theModel);//Item选择模型
    //为tableView设置数据模型
    ui->tableView->setModel(theModel); //设置数据模型
    ui->tableView->setSelectionModel(theSelection);//设置选择模型

//    QMessageBox::information(this, "消息", "表格窗口被创建");
}

QFormTable::~QFormTable()
{
//    QMessageBox::information(this, "消息", "FormTable窗口被删除和释放");
    delete ui;
}

void QFormTable::on_actSize_triggered()
{
    QWDialogSize    *dlgTableSize=new QWDialogSize(this); //创建对话框

    Qt::WindowFlags    flags=dlgTableSize->windowFlags();
    dlgTableSize->setWindowFlags(flags | Qt::MSWindowsFixedSizeDialogHint); //设置对话框固定大小

    dlgTableSize->setRowColumn(theModel->rowCount(),theModel->columnCount()); //对话框数据初始化

    int ret=dlgTableSize->exec();// 以模态方式显示对话框,用户关闭对话框时返回 DialogCode值
    if (ret==QDialog::Accepted) //OK键被按下,对话框关闭,若设置了setAttribute(Qt::WA_DeleteOnClose),对话框被释放,无法获得返回值
    { //OK键被按下,获取对话框上的输入,设置行数和列数
        int cols=dlgTableSize->columnCount();
        theModel->setColumnCount(cols);

        int rows=dlgTableSize->rowCount();
        theModel->setRowCount(rows);
    }
    delete dlgTableSize; //删除对话框
}

void QFormTable::on_actSetHeader_triggered()
{
    if (dlgSetHeaders==NULL) //如果对象没有被创建过,就创建对象
        dlgSetHeaders = new QWDialogHeaders(this);

    if (dlgSetHeaders->headerList().count()!=theModel->columnCount())
    {
        QStringList strList;
        for (int i=0;i<theModel->columnCount();i++)//获取现有的表头标题
            strList.append(theModel->headerData(i,Qt::Horizontal,Qt::DisplayRole).toString());
        dlgSetHeaders->setHeaderList(strList);//用于对话框初始化显示
    }

    int ret=dlgSetHeaders->exec();// 以模态方式显示对话框
    if (ret==QDialog::Accepted) //OK键被按下
    {
        QStringList strList=dlgSetHeaders->headerList();//获取对话框上修改后的StringList
        theModel->setHorizontalHeaderLabels(strList);// 设置模型的表头标题
    }
}

qwdialogheaders.h

#ifndef QWDIALOGHEADERS_H
#define QWDIALOGHEADERS_H

#include <QDialog>

#include    <QStringListModel>

namespace Ui {
class QWDialogHeaders;
}

class QWDialogHeaders : public QDialog
{
    Q_OBJECT

private:
    QStringListModel  *model;

public:
    explicit QWDialogHeaders(QWidget *parent = 0);
    ~QWDialogHeaders();

    void    setHeaderList(QStringList& headers);
    QStringList headerList();
private:
    Ui::QWDialogHeaders *ui;
};

#endif // QWDIALOGHEADERS_H

qwdialogheaders.cpp

#include "qwdialogheaders.h"
#include "ui_qwdialogheaders.h"

#include    <QMessageBox>

QWDialogHeaders::QWDialogHeaders(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QWDialogHeaders)
{
    ui->setupUi(this);
    model= new QStringListModel;
    ui->listView->setModel(model);
}

QWDialogHeaders::~QWDialogHeaders()
{
//    QMessageBox::information(this, "消息", "DialogHeaders对话框被删除和释放");
    delete ui;
}

void QWDialogHeaders::setHeaderList(QStringList &headers)
{
    model->setStringList(headers);
}

QStringList QWDialogHeaders::headerList()
{
    return  model->stringList();
}

qwdialogsize.h

#ifndef QWDIALOGSIZE_H
#define QWDIALOGSIZE_H

#include <QDialog>

namespace Ui {
class QWDialogSize;
}

class QWDialogSize : public QDialog
{
    Q_OBJECT

public:
    explicit QWDialogSize(QWidget *parent = 0);
    ~QWDialogSize();

    int rowCount();//获取对话框输入的行数
    int columnCount();//获取对话框输入的列数
    void    setRowColumn(int row, int column); //初始对话框上两个SpinBox的值

private slots:

private:
    Ui::QWDialogSize *ui;
};

#endif // QWDIALOGSIZE_H

qwqialogsize.cpp

#include "qwdialogsize.h"
#include "ui_qwdialogsize.h"

#include    <QMessageBox>

QWDialogSize::QWDialogSize(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QWDialogSize)
{
    ui->setupUi(this);
}

QWDialogSize::~QWDialogSize()
{
//    QMessageBox::information(this,"提示","DialogSize对话框被删除");
    delete ui;
}

int QWDialogSize::rowCount()
{ //用于主窗口调用获得行数的输入值
    return  ui->spinBoxRow->value();
}

int QWDialogSize::columnCount()
{//用于主窗口调用获得列数的输入值
    return  ui->spinBoxColumn->value();
}

void QWDialogSize::setRowColumn(int row, int column)
{ //初始化数据显示
    ui->spinBoxRow->setValue(row);
    ui->spinBoxColumn->setValue(column);
}


posted @ 2022-06-01 20:34  蘑菇王国大聪明  阅读(72)  评论(0编辑  收藏  举报