<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.
code:
@
//main.cpp file#include "myApplication.h"
int main(int argc, char **argv)
{
MyApplication app(argc, argv);return app.exec();
}
@@
//myApplication.h File#ifndef MYAPPLICATION_H
#define MYAPPLICATION_H#include <QtGui>
//Derived Class from QApplication
class MyApplication: public QApplication
{
Q_OBJECT
public:
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();
layout->addWidget(edit);
button = new QPushButton("Find");
lineEdit = new QLineEdit();
layout->addWidget(lineEdit);
layout->addWidget(button);window->setCentralWidget(centralWidget);
connect(button,SIGNAL(clicked()),this,SLOT(findText()));
window->show();};
~ 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.
edit->moveCursor(QTextCursor::Start);
edit->find(lineEdit->text());
};public:
QTextEdit* edit;
QPushButton* button;
QLineEdit* lineEdit;};
#endif @http://www.codeprogress.com/cpp/libraries/qt/QTextEditFindHighlightExample.php
Plz help me regarding this issue. I am working on open SuSe 12.1 is that the problem, suggest ur views.
Thanks
-
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..
Thanks,,,
-
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.