PDA

View Full Version : RightToLeft QTextEdit for Hebrew



hed
14th April 2008, 22:24
I would like to use QT for a Hebrew speaking application.
I need to enter some text into a text area but since t's in hebrew
The text is being written Right to left
Specifically the cursor should blnk on the right side of the area on start.

Is it possible?

tnx,
hed

momesana
14th April 2008, 22:43
You can set the direction of a widget via QWidget::setLayoutDirection() and
QApplication::setLayoutDirection().

try this


#include <QtGui>

int main(int argc, char** argv)
{
QApplication app(argc, argv);
QLineEdit* edit = new QLineEdit;
edit->setLayoutDirection(Qt::RightToLeft);
edit->show();
return app.exec();
}



The result will look like this:
2177

hed
15th April 2008, 05:18
Tnx a lot,
Tried it and it works but still doesnt solve my problem.
because i need multiple lines edit and the same code you posted doesn't work for QTextEdit.
It behaves dffrently then QLineEdit:
Attached an image.
The lower is QLineEdit and the Middle one is QTextEdit which in which you can see the text s on the left.

Hed

momesana
15th April 2008, 12:09
One ugly but simple workaround would be using QTextEdit::setAlignment(Qt::Right); This may suffice if your edit is solely used for hebrew and all the text contained there should be right-aligned.
But You should use something better than this. I guess it is all about setting the right layout for the document, not the gui element that displays it.
Maybe these functions can be of some help to you:


QTextEdit::document();
QTextFormat::setLayoutDirection();
Unfortunately I've never really done anything special with QTextDocuments so I am pretty clueless here.

mchara
16th April 2008, 07:06
Hi there,

first at all, could you precise if your want text aligned to right or just text should be entered from right to left while typing?

If the problem is an alignment, it can be set by QTextEdit::setAlignment(Qt::Right) but it's not dependent on language and can be set different for different paragraphes.

If you want text to be written right to left while typing, try settin system language to hebrew and text edit should deal with inverted insertion automatically[we have no additional methods for right-to-left languages and if application is run in hebrew text edits inserts chars before cursor when typing(except numbers which are typed normally, so text cursor have strange arrow that show text direction].

or am i missing something?

Please let me know if what qt does automatically for hebrew is incorrect(our application should deal with right-to-left languages, but we don't know any so we can't be sure is our software works properly with such languages)

elcuco
16th April 2008, 11:28
I think you will have to sub-class QTextEdit and set the proper options in QTextOptions for the selected paragraph. I have done that for katePart (well, still not finished, but parts are working).

See this thread, and when my development setup will be available (my computer crahsed, and I had to re-install it from scratch) I will try to post some code here. Meanwhile, look at this thread:
http://www.progressive-comp.com/?l=kwrite-devel&m=119954289516836&w=2

momesana
16th April 2008, 20:33
If you want text to be written right to left while typing, try settin system language to hebrew and text edit should deal with inverted insertion automatically[we have no additional methods for right-to-left languages and if application is run in hebrew text edits inserts chars before cursor when typing(except numbers which are typed normally, so text cursor have strange arrow that show text direction].


Persian, like hebrew is written from right to left. Thus, the text typed in should be right-aligned. More so, if I have set the applications layoutDirection to Right to left as in this example:
2182
As you can see the line edit behaves the way it should. The TextEdit however doesnt and the text is still left-aligned. This looks very out of place for persian text (and other rtl languages.)

It should however do that depending on the language typed in so that one paragraph can be persian, the next one english and so forth ... and all be aligned correctly irrespective of the layoutDirection.

In kde only kedit is smart enough to handle that correctly. kwrite and kate do not (which doesn't come as a surprise since they both are built upon katepart). I really hope kate will handle that correctly in the newer version.

For the sake of completeness, here's the code for the above example:


#include <QtGui>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0) : QMainWindow(parent) {
QWidget* cw = new QWidget;
QVBoxLayout* lo = new QVBoxLayout;
lo->addWidget(new QLineEdit);
lo->addWidget(new QTextEdit);
cw->setLayout(lo);
setCentralWidget(cw);
}
};

int main(int argc, char** argv)
{
QApplication app(argc, argv);
app.setLayoutDirection(Qt::RightToLeft);
MainWindow mw;
mw.show();
return app.exec();
}
#include "main.moc"