Qt学习(三)

本次实验以电脑中文本文档为模板,编译出其菜单栏及实现部分功能。因初学,有些功能实现的并不是很好。

窗口图如下:


菜单栏如下(部分):

    

功能部分实现了新建文档和退出文档的操作,操作时,未考虑保存文档情况。另外还有查找操作,但也只是做了选择该功能时会跳出查找对话框,并不是真正意义上的查找功能。

效果图如下:

    

源代码如下:

头文件 textdocument.h

#ifndef TEXTDOCUMENT_H
#define TEXTDOCUMENT_H

#include <QMainWindow>
#include <QWidget>

#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>

#include <QTextEdit>
#include <QGridLayout>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QScrollBar>
#include <QMessageBox>


class TextDocument : public QMainWindow
{
    Q_OBJECT

public:
    TextDocument(QWidget *parent = 0);
    //TextDocument(QDialog *parent = 0);
    ~TextDocument();

private:
    TextDocument *_newTextDocument;

    //窗口
    QWidget *_widget;
    QTextEdit *_textEdit;
    QGridLayout *_mainLayout1;

    QDialog *_dialog;
    QLabel *_label;
    QLineEdit *_lineEdit;
    QPushButton *_button1;
    QPushButton *_button2;
    QGridLayout *_mainLayout2;

    //F菜单
    QMenu *_fMenu;
    QAction *_xjAchion;
    QAction *_dkAchion;
    QAction *_bcAchion;
    QAction *_lcAchion;
    QAction *_ymAchion;
    QAction *_dyAchion;
    QAction *_tcAchion;

    //E菜单
    QMenu *_eMenu;
    QAction *_cxAchion;
    QAction *_jqAchion;
    QAction *_fzAchion;
    QAction *_ztAchion;
    QAction *_scAchion;
    QAction *_czAchion;
    QAction *_czxAchion;
    QAction *_thAchion;
    QAction *_zdAchion;
    QAction *_qxAchion;
    QAction *_sjAchion;

    //O菜单
    QMenu *_oMenu;
    QAction *_zdhAchion;
    QAction *_zTAchion;

    //V菜单
    QMenu *_vMenu;
    QAction *_ztlAchion;

    //H菜单
    QMenu *_hMenu;
    QAction *_ckAchion;
    QAction *_gyAchion;

protected slots:
    void NewFile();//新建文件
    void CloseFile();//退出文件
    void Find();//查找操作
    void find();
    void dialog_close();

};

#endif // TEXTDOCUMENT_H

源文件 main.cpp

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

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

    return a.exec();
}

源文件 textdocument.cpp

#include "textdocument.h"

TextDocument::TextDocument(QWidget *parent)
    : QMainWindow(parent)
{
    //窗口
    _mainLayout1 = new QGridLayout();
    _widget = new QWidget(this);
    this->setCentralWidget(_widget);
    _textEdit = new QTextEdit(this);
    _textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    _textEdit->verticalScrollBar()->setValue(_textEdit->verticalScrollBar()->maximumHeight());
    _widget->setLayout(_mainLayout1);
    _mainLayout1->addWidget(_textEdit);

    //F菜单
    _fMenu = menuBar()->addMenu(tr("文件(F)"));
    _xjAchion = new QAction(tr("新建(N)"), this);
    connect(_xjAchion, SIGNAL(triggered(bool)), this, SLOT(NewFile()));//新建文件
    _dkAchion = new QAction(tr("打开(O)…"), this);
    _bcAchion = new QAction(tr("保存(S)"), this);
    _lcAchion = new QAction(tr("另存为(A)…"), this);
    _ymAchion = new QAction(tr("页面设置(U)…"), this);
    _dyAchion = new QAction(tr("打印(P)…"), this);
    _tcAchion = new QAction(tr("退出(X)"), this);
    connect(_tcAchion, SIGNAL(triggered(bool)), this, SLOT(CloseFile()));//退出文件
    //_fMenu = _fMenu->addMenu(tr("文件(F)"));
    _xjAchion->setShortcut(Qt::CTRL + Qt::Key_N);
    _dkAchion->setShortcut(Qt::CTRL + Qt::Key_O);
    _bcAchion->setShortcut(Qt::CTRL + Qt::Key_S);
    _dyAchion->setShortcut(Qt::CTRL + Qt::Key_P);
    _fMenu->addAction(_xjAchion);
    _fMenu->addAction(_dkAchion);
    _fMenu->addAction(_bcAchion);
    _fMenu->addAction(_lcAchion);
    _fMenu->addSeparator();
    _fMenu->addAction(_ymAchion);
    _fMenu->addAction(_dyAchion);
    _fMenu->addSeparator();
    _fMenu->addAction(_tcAchion);

    //E菜单
    _eMenu = menuBar()->addMenu(tr("编辑(E)"));
    _cxAchion = new QAction(tr("撤消(U)"), this);
    _jqAchion = new QAction(tr("剪切(T)"), this);
    _fzAchion = new QAction(tr("复制(C)"), this);

    _ztAchion = new QAction(tr("粘贴(P)"), this);
    _scAchion = new QAction(tr("删除(L)"), this);
    _czAchion = new QAction(tr("查找(F)…"), this);
    connect(_czAchion, SIGNAL(triggered(bool)), this, SLOT(Find()));//查找操作

    _czxAchion = new QAction(tr("查找下一个(N)"), this);
    _thAchion = new QAction(tr("替换(R)…"), this);
    _zdAchion = new QAction(tr("转到(G)…"), this);
    _qxAchion = new QAction(tr("全选(A)"), this);
    _sjAchion = new QAction(tr("时间/日期(D)"), this);
    _cxAchion->setShortcut(Qt::CTRL + Qt::Key_Z);
    _jqAchion->setShortcut(Qt::CTRL + Qt::Key_X);
    _fzAchion->setShortcut(Qt::CTRL + Qt::Key_C);
    _ztAchion->setShortcut(Qt::CTRL + Qt::Key_V);
    _scAchion->setShortcut(Qt::Key_Delete);
    _czAchion->setShortcut(Qt::CTRL + Qt::Key_F);
    _czxAchion->setShortcut(Qt::Key_F3);
    _thAchion->setShortcut(Qt::CTRL + Qt::Key_H);
    _zdAchion->setShortcut(Qt::CTRL + Qt::Key_G);
    _qxAchion->setShortcut(Qt::CTRL + Qt::Key_A);
    _sjAchion->setShortcut(Qt::Key_F5);

    _eMenu->addAction(_cxAchion);
    _eMenu->addSeparator();
    _eMenu->addAction(_jqAchion);
    _eMenu->addAction(_fzAchion);
    _eMenu->addAction(_ztAchion);
    _eMenu->addAction(_scAchion);
    _eMenu->addSeparator();
    _eMenu->addAction(_czAchion);
    _eMenu->addAction(_czxAchion);
    _eMenu->addAction(_thAchion);
    _eMenu->addAction(_zdAchion);
    _eMenu->addSeparator();
    _eMenu->addAction(_qxAchion);
    _eMenu->addAction(_sjAchion);

    //O菜单
    _oMenu = menuBar()->addMenu(tr("格式(O)"));
    _zdhAchion = new QAction(tr("自动换行(W)"), this);
    _zTAchion = new QAction(tr("字体(F)…"), this);
    _oMenu->addAction(_zdhAchion);
    _oMenu->addAction(_zTAchion);

    //V菜单
    _vMenu = menuBar()->addMenu(tr("查看(V)"));
    _ztlAchion = new QAction(tr("状态栏(S)"), this);
    _vMenu->addAction(_ztlAchion);

    //H菜单
    _hMenu = menuBar()->addMenu(tr("帮助(H)"));
    _ckAchion = new QAction(tr("查看帮助(H)"), this);
    _gyAchion = new QAction(tr("关于记事本(A)"), this);
    _hMenu->addAction(_ckAchion);
    _hMenu->addSeparator();
    _hMenu->addAction(_gyAchion);
}

TextDocument::~TextDocument()
{

}

void TextDocument::NewFile()
{
    _newTextDocument = new TextDocument;
    _newTextDocument->show();
    this->close();
}

void TextDocument::CloseFile()
{
    if (!(QMessageBox::question(this, tr("记事本"), tr("是否退出文本文档?"), tr("退出"), tr("取消"))))
    {
        this->close();
    }
}

void TextDocument::Find()
{
    _dialog = new QDialog();
    _dialog->resize(300,100);
    _dialog->setWindowTitle(tr("查找"));
    _label = new QLabel(_dialog);
    _label->setText(tr("查找内容(N):"));
    _lineEdit=new QLineEdit(_dialog);
    _button1=new QPushButton(_dialog);
    _button1->setText(tr("查找下一个(F)"));
    connect(_button1, SIGNAL(clicked()), this, SLOT(find()));
    _button2=new QPushButton(_dialog);
    _button2->setText(tr("取消"));
    connect(_button2, SIGNAL(clicked()), this, SLOT(dialog_close()));
    _mainLayout2 = new QGridLayout(_dialog);
    _mainLayout2->addWidget(_label,0,0);
    _mainLayout2->addWidget(_lineEdit,0,1);
    _mainLayout2->addWidget(_button1,1,1);
    _mainLayout2->addWidget(_button2,2,1);
    _dialog->show();
}

void TextDocument::find()
{
    QString tempStr;
    QString valueStr = _lineEdit->text();
    _lineEdit->setText("查找失败");
}

void TextDocument::dialog_close()
{
    _dialog->close();
}

参考网站:https://www.devbean.net/2012/09/qt-study-road-2-dialogs-intro/点击打开链接

阅读更多

没有更多推荐了,返回首页