• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::

            要在QTextEdit中高亮搜索到的字符串, 使用它的QTextDocument和QTextCursor: 


            void TextFinder::on_findButton_clicked() {

                QString searchString = ui_lineEdit->text();

                QTextDocument *document = ui_textEdit->document();


                bool found = false;


                if (isFirstTime == false)

                    document->undo();


                if (searchString == "") {

                    QMessageBox::information(this, tr("Empty Search Field"),

                            "The search field is empty. Please enter a word and click Find.");

                } else {


                    QTextCursor highlightCursor(document);

                    QTextCursor cursor(document);


                    //***************開始***************

                    cursor.beginEditBlock();


                    QTextCharFormat plainFormat(highlightCursor.charFormat());

                    QTextCharFormat colorFormat = plainFormat;

                    colorFormat.setForeground(Qt::red);


                    while (!highlightCursor.isNull() && !highlightCursor.atEnd()) {

                        highlightCursor = document->find(searchString, highlightCursor,

                                QTextDocument::FindWholeWords);


                        if (!highlightCursor.isNull()) {

                            found = true;

                            highlightCursor.movePosition(QTextCursor::WordRight,

                                    QTextCursor::KeepAnchor);

                            highlightCursor.mergeCharFormat(colorFormat);

                        }

                    }


                    cursor.endEditBlock();

                    //***************結束***************

                    

                    isFirstTime = false;


                    if (found == false) {

                        QMessageBox::information(this, tr("Word Not Found"),

                                "Sorry, the word cannot be found.");

                    }

                }

            }

            posted on 2009-05-24 19:36 逛奔的蝸牛 閱讀(4290) 評論(3)  編輯 收藏 引用 所屬分類: Qt自定義Widget

            評論

            # re: QtWidget: TextFinder(高亮搜索到的字符串) 2010-08-16 15:32 lijunsong
            那個 movePosition 是多余的……  回復  更多評論
              

            # re: QtWidget: TextFinder(高亮搜索到的字符串) 2012-05-11 22:19 gaoshixian
            介個哥們是我瓜大的吧  回復  更多評論
              

            # re: QtWidget: TextFinder(高亮搜索到的字符串) 2012-10-16 15:34 毛竹
            如果是在ListView上呢,要怎么標記高亮  回復  更多評論
              

            91精品国产91久久久久久| 大香伊人久久精品一区二区| 97久久精品无码一区二区天美| 久久国产亚洲精品无码| 国产精品免费久久久久久久久| 欧美日韩精品久久久免费观看| 亚洲午夜久久久久妓女影院| 2021精品国产综合久久| 国产精品久久久久久久久久影院 | 999久久久免费精品国产| 久久久久国色AV免费看图片| 久久精品国产亚洲av麻豆小说 | 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 超级97碰碰碰碰久久久久最新| 久久久久无码精品国产不卡| 亚洲国产高清精品线久久| 亚洲精品蜜桃久久久久久| 久久久久亚洲AV无码专区网站| 久久久免费精品re6| 久久久久亚洲AV综合波多野结衣 | 国产精品99精品久久免费| 日本亚洲色大成网站WWW久久| 好久久免费视频高清| 少妇内射兰兰久久| 香蕉久久AⅤ一区二区三区| 国产成人久久久精品二区三区| 久久夜色精品国产噜噜亚洲AV | 一本色道久久88精品综合| 亚洲国产成人久久笫一页| 99久久国产综合精品成人影院| 欧美va久久久噜噜噜久久| 99精品久久久久久久婷婷 | 欧美日韩精品久久免费| 色婷婷久久久SWAG精品| 久久婷婷五月综合成人D啪| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 精品久久久久中文字| 久久99精品免费一区二区| 国产精品激情综合久久| 久久男人AV资源网站| 亚洲欧洲中文日韩久久AV乱码|