• <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();

                    //***************結(jié)束***************

                    

                    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 逛奔的蝸牛 閱讀(4303) 評論(3)  編輯 收藏 引用 所屬分類: Qt自定義Widget

            評論

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

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

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

            欧美亚洲另类久久综合婷婷 | 亚洲七七久久精品中文国产| 久久亚洲高清综合| 久久久久久久久久久久久久| 久久久国产精品福利免费| 久久精品aⅴ无码中文字字幕不卡| 久久国产免费直播| 99久久精品国内| 99久久婷婷国产综合亚洲| 久久精品国产亚洲av麻豆蜜芽| 亚洲精品国产字幕久久不卡| 香蕉久久永久视频| 99久久精品国产一区二区蜜芽| 漂亮人妻被黑人久久精品| 97精品依人久久久大香线蕉97| 四虎国产精品成人免费久久| 久久精品国产99久久香蕉| 精品亚洲综合久久中文字幕| 久久久久亚洲AV无码专区体验| 无码伊人66久久大杳蕉网站谷歌| 国产精品成人99久久久久 | 国内精品九九久久久精品| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 欧美伊人久久大香线蕉综合69| 国产V亚洲V天堂无码久久久| 久久久国产视频| 久久笫一福利免费导航| 色综合久久综合网观看| 99精品久久久久中文字幕| 亚洲精品无码久久一线| 国内精品人妻无码久久久影院导航 | 久久综合给合久久狠狠狠97色| 久久影院久久香蕉国产线看观看| 精品久久一区二区| 国产精品乱码久久久久久软件| 国产情侣久久久久aⅴ免费| 中文精品99久久国产| 久久精品视屏| 久久久噜噜噜www成人网| 99久久人妻无码精品系列蜜桃| 久久人人爽人人爽人人片AV不|