QT study notes (3)

1) connect();
The declaration of the custom process in the SLOT in connect must be written under private slots: (or public slots:)

2 ) Continue to follow the order of QT study notes (2):
1) How to write in the editor Insert tables, lists and pictures
1.1.1) Add private slots private slots declaration:
      void insertTable();//insert table
      void insertList();//insert list
      void insertImage();//insert picture
1.1.2) in the tool Add Action on the bar;
//Insert table
    QAction *action_textTable = new QAction(tr("table"), this);
    connect(action_textTable, SIGNAL(triggered()), this, SLOT(insertTable()));
    ui-> mainToolBar->addAction(action_textTable);

    //Insert list
    QAction *action_textList = new QAction(tr("list"), this);
    connect(action_textList, SIGNAL(triggered()), this, SLOT(insertList()));
    ui->mainToolBar->addAction(action_textList);

    //Insert picture
    QAction *action_textImage = new QAction(tr("picture"), this);
    connect(action_textImage, SIGNAL(triggered()), this, SLOT(insertImage() ));
    ui->mainToolBar->addAction(action_textImage);

1.1.3) Implementation code for adding slot function
void MainWindow::insertTable()//Insert table
{
    QTextCursor cursor = ui->textEdit->textCursor();
    QTextTableFormat format;//Table format
    format.setCellSpacing(2);//Outside the table
    format.setCellPadding(10);//Inside the table
    cursor.insertTable(2, 2, format);//Insert 2 rows and 2 columns
}

void MainWindow::insertList() //Insert list
{
    QTextListFormat format;//List format
    format.setStyle(QTextListFormat::ListDecimal);//List number
    ui->textEdit->textCursor().insertList(format);

}

void MainWindow::insertImage() //Insert picture
{
    QTextImageFormat format;
    format.setName(" D:\For Code\5-3\Image");//image path
    ui->textEdit->textCursor().insertImage(format);

}

2 Add the search function
2.1.1 Before adding the class in the .h file Set declaration
class QLineEdit;

2.1.2 Add pointer to private object
QLineEdit *lineEdit;

2.1.3 Add private slots
void textFind();//Find file
void findNext();//Find next
2.1.4 Add header file and add Action:
#include <QDebug>
#include <QlineEdit>
#include <QDialog>
#include <QPushButton>
#include <QVBoxLayout>

//Add search function
    QAction *action_textFind = new QAction(tr("Find"), this);
    connect(action_textFind, SIGNAL(triggerd()), this, SLOT(textFind()));
    ui- >mainToolBar->addAction(action_textFind);


2.1.5 Implementation code of adding slot function
void MainWindow::textFind()
{

    QDialog *dlg = new QDialog(this);
    lineEdit = new QLineEdit(dlg);
    QPushButton *btn = new QPushButton (dlg);
    btn->setText(tr("find next"));
    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("Line number: %1 Column number: &2").arg(ui->textEdit->textCursor().blockNumber()).arg(ui->textEdit->textCursor().columnNumber ());

    }
}

3 Add a new file for syntax highlighting and html
3.1.1, select C++ class, class name: mySyntaxHighlighter, base class: QSyntaxHighlighter, inherited from: QObject
3.1.2 Modify mysyntaxhighlighter. h header file Use the     QTextDocument
class object pointer as its parent component pointer, so that the highlightBlock() function can be automatically called, and the text can be detected when possible.//QTextDocument is
    used as the parent window so that the highlightBlock() function can be automatically called .
= 0);
Reimplement the highlightBlock() function to apply the format of the string to a specific string
protected:
    void highlightBlock(const QString &text); //The function must be reimplemented
3.1.3 Modify the mysyntaxhighlighter.cpp file
mySyntaxHighlighter::mySyntaxHighlighter( QTextDocument *parent) :
    QSyntaxHighlighter(parent)
{
}
void mySyntaxHighlighter::highlightBlock(const QString &text)
{
    QTextCharFormat charFormat; //Set the matching character format
    charFormat.setFontWeight(QFont::Bold);
    charFormat.setForeground(Qt::green );
    QString pattern = "\\bchar\\b"; //Character to match, here is the char word
    QRegExp expression(pattern); //Create a regular expression
    int index = text.indexOf(expression); //Default Match strings starting from 0
    //If the match is successful, the return value is the starting position of the string, which is greater than or equal to 0
    while(index >= 0 ) {
        int length = expression.matchedLength(); //The length of the string to match
        setFormat(index, length,charFormat); //Set the format for the string to be matched
        index = text.indexOf(expression,index + length); //Continue to match
    }
}
3.1.4 Modify the main interface header file
    class mySyntaxHighlighter;
add a private variable
    mySyntaxHighlighter *highlighter;
3.1.5 Repair the cpp file
of Add
    highlighter = new mySyntaxHighlighter(ui->textEdit->document());

4 Use of QTextEdit
4.1.1 to create a QTextEdit
    QTextDocument *m_doc = new QTextDocument ("Text in the text box");//Create a container for text
QTextEdit te;//Define the text box
te.setDocument(m_doc);//Bind the content for the text box
4.1.2 Write data to QTextEdit
    QString gyyq = rec.value("gy").toString();
    te.document()->setPlainText(gyyq );

4.1.3 Read data from QTextEdit
    QString gyyq = te.document()->toPlainText();



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326360383&siteId=291194637