PDA

View Full Version : Suggestions for particular text format in QTextEdit



Suppaman
25th March 2013, 21:21
Hi

Since I'm not very expert in use QT I would to ask for some suggestion regarding a particular text format I would to show using QTextEdit control. My need is to show lines of text in a format like the following:


AAAA BBBB CCCCC

___aaa_____bbbb______cccc


Please, note that in this example characters '_' are used instead of spaces since I can not obtain such text format using the standard control of this forum editor.

As you can see the big text "words" have below some small text that must be "centered" with the connected big word over. I don't have a clear idea regarding how to obtain this fomat into QTextEdit. I tried to use html code (<span> and css) but the same html code working in a standar browser don't have the same result in the qt control, I suppose for some unsupported css tag. I could try using tables but how to make tables aligned in the same line? The problem is this format must act like standard text, than if I'll resize the editor window the text must to redistribute correctly.

Someone can point me in the right direction for make this result?

Thank you

giblit
25th March 2013, 22:36
I'm a bit confused are you trying to make "AAAA BBBB CCCC" lowercase and centered or are you trying to replace spaces with "_"? or are you trying to center the small stuff under the big letters?

Eidt if you are trying to center the second string you could do something like ->>> void QTextEdit::setAlignment ( Qt::Alignment a ) [slot]
Sets the alignment of the current paragraph to a. Valid alignments are Qt::AlignLeft, Qt::AlignRight, Qt::AlignJustify and Qt::AlignCenter (which centers horizontally).
See also alignment(). <<<-

so someting like QTextEdit::setAlignment(Qt::AlignCenter);

Suppaman
25th March 2013, 22:55
Hi

Sorry, I understand my request is a bit difficult to explain. Basically my need is the third option you said. Regarding the character "_" I used instead of spaces because this forum editor clean each additional speces between words than I can not "format" the words position as I would. In the original version you have to imagine blank spaces instead of "_". Basically each big word must have below a small word centered "inside" the width of the big word itself. This because the small word will be like an "explanation" of the meaning of the "connected" big word above. Big and small words should be a unique block indipendend from any others. Hope to have clarify a bit...

giblit
25th March 2013, 23:17
basically replacing all the underscores with spaces is a simple task.


QTextEdit textEdit;
QString qstr = "____aaaa____bbbb____cccc";
qstr.replace("_", " ");
textEdit->setText(qstr);

and as far as centering under the other words you could do something like this


QTextEdit textEdit;
QString large = "AAAA BBBB CCCC";
QString small = "aaaa bbbb cccc";
int index;
index = large.indexOf("AAAA");
QTextCursor c(textEdit->textCursor());
c.movePosition(QTextCursor::setPosition(index,QTex tCursor::KeepAnchor));
textEdit->setPlainText(small);

maybe something like that will work

wysota
25th March 2013, 23:42
You can always put the words in a table.

Suppaman
26th March 2013, 08:58
You can always put the words in a table.

This is another way I'm trying to follow. However there are two problems. Currently I'm testing use of QTextCursor and insertTable but I have a problem that, after created a new table, the cursor remain "inside" the table and every new insertion is always inside the first cell, I need a way to move the cursor out of the table for create a new table outside. Since we are on topic can you help me about this problem? :) However the doubt I have regarding this solution is that, if QTextEdit control use tables as same way as standard html browser do, tables can not be positioned side by side but only one per line. I still was not able to verify...

By the way I made a picture for clarify better my needed format:

8857

As you cann see "block of words" have to be disposed in multiple lines as normal text but every big word must have the connected small word centered under them...

wysota
26th March 2013, 09:17
Maybe you should just state what is the final effect you are trying to achieve? Why are you using QTextEdit, is that some requirement?

Suppaman
26th March 2013, 09:24
No, QTextEdit is the first control I thought to use but is not mandatory. My need is only that these "text blocks" must be act like standard edit control. This mean if I resize the "control" showing these text the block must to redispose automatically as a normal text do (ex. if I decrease the width of the control the blocks on the right must to moved in the line below and so on). The second need is that the text can be selected and copied into clipboard and the control is read only.

wysota
26th March 2013, 09:31
This would suggest you are dealing with a list of blocks which would suggest QListView or QListWidget to be an ideal solution to your problem. Of course you have to provide a custom delegate for selecting text (unless you're not interested in selecting part of a single block but rather whole block at a time).

Suppaman
26th March 2013, 09:50
Unfortunately I don't think QListView is a good solution mainly because not all the big words will have the same width size. Using the ListView control I have a grid with all the columns sized as the largest width element in the list that is not what I would. Just for a moment forget the presence of the "small words below the big words". In this case this will be a normal editor with standard text written using big font (like this text I'm writing). In add of this standard text every big words will have a small word below. An example could be a text written in a foreign language and below every words the meaning of the word in your language written smaller...

wysota
26th March 2013, 11:00
Unfortunately I don't think QListView is a good solution mainly because not all the big words will have the same width size.
That is not important. Enable wrapping property and have the list flow from left to right.

Suppaman
26th March 2013, 11:37
I'll try to follow this way too, but before I would to verify if really tables can not be aligned into the same line in QTextEdit. Someone can confirm? In add I still have the problem above using QTextTable. After inserted new table using insertTable() the cursor remain inside the first cell, how can I move the cursor outside the table just created?

Thank you for the help

Suppaman
26th March 2013, 18:36
Hi

If someone is interested I found the solution. Basically I created tables with css style "float:left" that is supported by QTextEdit control. This will result to show tables in the same line and move on the next line if the edit control is resized, exactly what I need. ;) Thank you to all for the support.