QT学习笔记(三)

一)connect();
connect中的SLOT里的自定义过程的申明一定要写在private slots:(或public slots:)下

二)继续按QT学习笔记(二)顺序:
1)如何在编辑器中插入表格、列表与图片
1.1.1)添加私有槽private slots声明:
      void insertTable();//插入表格
      void insertList();//插入列表
      void insertImage();//插入图片
1.1.2)在工具栏上添加Action;
//插入表格
    QAction *action_textTable = new QAction(tr("表格"), this);
    connect(action_textTable, SIGNAL(triggered()), this, SLOT(insertTable()));
    ui->mainToolBar->addAction(action_textTable);

    //插入列表
    QAction *action_textList = new QAction(tr("列表"), this);
    connect(action_textList, SIGNAL(triggered()), this, SLOT(insertList()));
    ui->mainToolBar->addAction(action_textList);

    //插入图片
    QAction *action_textImage = new QAction(tr("图片"), this);
    connect(action_textImage, SIGNAL(triggered()), this, SLOT(insertImage()));
    ui->mainToolBar->addAction(action_textImage);

1.1.3)添加槽函数的实现代码
void MainWindow::insertTable()//插入表格
{
    QTextCursor cursor = ui->textEdit->textCursor();
    QTextTableFormat format;//表格格式
    format.setCellSpacing(2);//表格外边白
    format.setCellPadding(10);//表格内边白
    cursor.insertTable(2, 2, format);//插入2行2列表格
}

void MainWindow::insertList() //插入列表
{
    QTextListFormat format;//列表格式
    format.setStyle(QTextListFormat::ListDecimal);//列表编号
    ui->textEdit->textCursor().insertList(format);

}

void MainWindow::insertImage() //插入图片
{
    QTextImageFormat format;
    format.setName("D:\For Code\5-3\Image");//图片路径
    ui->textEdit->textCursor().insertImage(format);

}

2 添加查找功能
2.1.1 在.h文件中添加类的前置声明
class QLineEdit;

2.1.2 添加私有对象的指针
QLineEdit *lineEdit;

2.1.3 添加private slots
void textFind();//查找文件
void findNext();//查找下一个
2.1.4 添加头文件以及添加Action:
#include <QDebug>
#include <QlineEdit>
#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>

//增加查找功能
    QAction *action_textFind = new QAction(tr("查找"), this);
    connect(action_textFind, SIGNAL(triggerd()), this, SLOT(textFind()));
    ui->mainToolBar->addAction(action_textFind);


2.1.5添加槽函数的实现代码
void MainWindow::textFind()
{

    QDialog *dlg = new QDialog(this);
    lineEdit = new QLineEdit(dlg);
    QPushButton *btn = new QPushButton(dlg);
    btn->setText(tr("查找下一个"));
    connect(btn, SIGNAL(clicked()), this, SLOT(findNext()));
    QVBoxLayout *layout = new QVBoxLayout;
    layout ->addWidget(lineEdit);
    layout ->addWidget(btn);
    dlg ->setLayout(layout);
    dlg->show();
}

void MainWindow::findNext()
{
    QString string = lineEdit->text();
    bool isfind = ui->textEdit->find(string, QTextDocument::FindBackward);
    if(isfind)
    {
        qDebug()<<tr("行号:%1 列号:&2").arg(ui->textEdit->textCursor().blockNumber()).arg(ui->textEdit->textCursor().columnNumber());

    }
}

3 语法高亮与html
3.1.1添加新的文件,选择C++ 类,类名:mySyntaxHighlighter,基类:QSyntaxHighlighter,继承自:QObject
3.1.2 修改mysyntaxhighlighter.h头文件
将QTextDocument类对象指针作为其父部件指针,这样可以自动调用highlightBlock()函数,可以时的检测文本
    //将QTextDocument作为父窗口这样就可以自动调用highlightBlock()函数
    explicit mySyntaxHighlighter(QTextDocument *parent = 0);
重新实现highlightBlock()函数以便将,字符串的格式应用到特定的字符串上面
protected :
    void highlightBlock(const QString &text);   //必须重新实现该函数
3.1.3  修改mysyntaxhighlighter.cpp文件
mySyntaxHighlighter::mySyntaxHighlighter(QTextDocument *parent) :
    QSyntaxHighlighter(parent)
{
}
void mySyntaxHighlighter::highlightBlock(const QString &text)
{
    QTextCharFormat charFormat;         //设置匹配的字符格式
    charFormat.setFontWeight(QFont::Bold);
    charFormat.setForeground(Qt::green);
    QString pattern = "\\bchar\\b";     //要匹配的字符,这里是char单词
    QRegExp expression(pattern);        //创建正则表达式
    int index = text.indexOf(expression);   //默认从0开始匹配字符串
    //如果匹配成功,返回值为字符串的起始位置,它大于或等于0
    while(index >= 0 ) {
        int length = expression.matchedLength();    //要匹配字符串的长度
        setFormat(index,length,charFormat);         //对要匹配的字符串设置格式
        index = text.indexOf(expression,index + length); //继续匹配
    }
}
3.1.4 修该主界面头文件
    class mySyntaxHighlighter;
添加私有变量
    mySyntaxHighlighter *highlighter;
3.1.5 修该主界面的cpp文件
在构造函数中添加
    highlighter = new mySyntaxHighlighter(ui->textEdit->document());

4 QTextEdit的使用
4.1.1建立一个QTextEdit
    QTextDocument *m_doc = new QTextDocument("文本框中的文本");//创建一个装文本的容器
QTextEdit te;//定义文本框
te.setDocument(m_doc);//为文本框绑定内容
4.1.2 向QTextEdit中写入数据
    QString gyyq = rec.value("gy").toString();
    te.document()->setPlainText(gyyq);

4.1.3 从QTextEdit中读取数据
    QString gyyq = te.document()->toPlainText();



猜你喜欢

转载自daisy-xu.iteye.com/blog/2347840