[Qt-interest] Strange behavior of QTextEdit

Tiago da Silva besbatti at gmail.com
Sat Mar 28 16:43:15 CET 2009


Hi there,

I have a QMainWindow with a QTextEdit and a QLineEdit. Once I receive
a signal, I want to read a UNICODE text file to put it into the
QTextEdit area.

Here is the slot which receives the signal

<code>
#define QStringToTCHAR(x) (wchar_t*) x.utf16()
#define TCHARToQString(x) QString::fromUtf16((x))

	void deal_with_song_change(QString new_song)	
	{
		ui.le_title->setText(new_song);   // the QLineEdit

		TextFile *tf_in = Hnew TextFile;    // class for reading a UNICODE text file

		QString fpath = lyrics_dir + new_song + ".txt";

		int result = tf_in->Open2 (QStringToTCHAR(fpath), TF_READ);

		if (result >= 0)	// could open the file
		{
			TCHAR *line_buf = NULL;

			QString qqq;

			do
			{
				result = tf_in->ReadLine2 (NULL, &line_buf);

				if (result >= TF_EOF)
				{
					qqq = TCHARToQString(line_buf);

                                        ui.le_title->setText( qqq  );
 // only to test

					ui.te_lyrics->setPlainText ( qqq );     // QTextEdit,  just to
test, put the first line of the file into QTextEdit
				}
			}
			while (result != TF_EOF);
		}

		tf_in->Close();

		delete tf_in;
	}

</code>

The thing is, both QTextEdit (te_lyrics) and QLineEdit (le_title) are
created by the same thread, in the object ui. So, the line

<line1>

ui.le_title->setText( qqq  );

</line1>

works perfectly. HOWEVER, when the line

<line2>

ui.te_lyrics->setPlainText ( qqq );

</line2>

is executed, I got the following error:

<error>

Debug Error!
Program: ...nts\VisualStudio 2008\ etc etc
Module: 4.4.3
File: global\qglobal.cpp
Line: 2090

Assert failure in QCoreApplication::sendEvent: "Cannot send events to
objects owned by a different thread. Current thread 22f964. Receiver
"(of type 'QTextDocument') was created in thread be6a58", file
kernel\qcoreapplication.cpp, line 296.

</error>

The signal is indeed emitted by another thread, since the latest
version of Qt supports it, and it works for the QLineEdit field.

So, what is it special about QTextEdit?

Thx in advance for your help.

Tiago



More information about the Qt-interest-old mailing list