PDA

View Full Version : Insert image in RTL QTextEdit problem!



SudaNix
21st October 2009, 13:51
Hello,

I have a problem in inserting image in RightToLeft QTextEdit.
What i want is to insert some text then insert image.

I try the following code:


QTextEdit* textEdit = new QTextEdit;

QTextOption option=textEdit->document()->defaultTextOption();
option.setTextDirection(Qt::RightToLeft); // RTL.
textEdit->document()->setDefaultTextOption(option);

QTextCursor cursor = textEdit->textCursor();

cursor.insertText("مرحبا بكم في كيوتي العربي"); // insert Arabic text

QTextImageFormat imageFormat;
imageFormat.setName(":/images/jumpy.gif");
cursor.insertImage(imageFormat); // then insert the Image.

textEdit->show();


The problem is the image appear in the wrong location, it should appear after the text not before it!:confused:

How i can solve it ?

I also try to change the current block format direction but it did not solve the problem.

Thanks in advance.

montylee
23rd October 2009, 22:33
For some characters, Qt inserts it in left to right mode even when you have set it to RTL. For e.g. if you enter a space or 1 digit number it's inserted in the left to right manner. So, i guess when you try to insert the image, somehow it doesn't insert it in RTL format. Simplest solution will be to manually set the cursor in the end and try. Although i am sure there should be a better solution to it, maybe somebody else can help you with this.

I am also trying to have a QTextEdit where user can mix english and arabic characters. But in my case if you type arabic, the text is not aligned to the right as per your screenshot although text is properly typed right to left. Is it normal? It aligns correctly when QTextEdit::setAlignment(Qt::AlignRight) is used.

montylee
23rd October 2009, 22:53
Ok, if you add this line before cursor.insertImage(), it should work:


cursor.movePosition(QTextCursor::Start);

Although, there might be a better and more logical solution to this. Please let me know how you managed to get the arabic text right aligned.