Nominate our 2022 Qt Champions!

<SOLVED> Text editor found text is not visible.

  • Hi,

    I have a text editor and used the same find()method of QTextEdit. But when a word is found the first occurrence of it is selected and not visible. Please copy and try the code from below and search for hello word in texteditor.

    //main.cpp file

    #include "myApplication.h"

    int main(int argc, char **argv)
    MyApplication app(argc, argv);

    return app.exec();

    //myApplication.h File


    #include <QtGui>

    //Derived Class from QApplication
    class MyApplication: public QApplication
    MyApplication(int & argc, char ** argv ):QApplication(argc,argv)

    QMainWindow *window = new QMainWindow();

    window->setWindowTitle(QString::fromUtf8("QTextEdit Highlight Comment Text"));
    window->resize(300, 250);

    QWidget *centralWidget = new QWidget(window);

    QVBoxLayout* layout = new QVBoxLayout(centralWidget);

    edit = new QTextEdit();


    button = new QPushButton("Find");
    lineEdit = new QLineEdit();



    ~ MyApplication(){};

    public slots:
    void findText()
    //Set Cursor position to the start of document
    //to let find() method make search in whole text.
    //Otherwise it will make search from cursor position to the end.

    QTextEdit* edit;
    QPushButton* button;
    QLineEdit* lineEdit;

    #endif @

    Plz help me regarding this issue. I am working on open SuSe 12.1 is that the problem, suggest ur views.


  • Is the color of the selected text set to white like the background?

  • No.. I did not set any background color.. Plz try the example given in link and type some text in editor and then click find button.. I want to know if this behavior is only with SuSe or other OS also..


  • On windows i can see the text slightly.

    in your main function add :
    @app.setStyleSheet("QTextEdit {selection-background-color: darkblue; selection-color: yellow; }");@

    and you should see the text highlighted.

    This proves my initial guess that the selected text color is the same as the background. It's a common error not easy to detect. I think we all have to deal with this sooner or later ;)

  • Tried both versions on Mac,

    Haney, in your code the first occurrence is selected in light gray, its slightly visible on MAC.

    The stylesheet adaption of Eddy makes it much more clear! :)

    My guess is that Eddy's guess was a good guess! ;)

  • Hi Steven,

    Welcome to devnet!

  • Thanks very much Eddy...

    setting stylesheet fixed my problem..

  • You're welcome.

    Have fun.

Log in to reply