Question

The thing I am doing is a QTreeWidget, with each Item's text editable. So I group checkbox,QTextEdit/QPlainTextEdit togeter within a widget, and setItemWidget for each item of the QTreeWidget. I have set the QTextEdit's scrollBar always off,I dont even want it scrollable by middle mouse button.Because I paint the dot lines on its parent widget, with a 25 pixel spacing. I want each text line be fixed on top of each dotline,can't be scrolled.

enter image description here enter image description here

So I need:

1> Make sure each text line has 25pixel height. each text line sits above the dot line.

2> Resize the QTextEdit to 25*LineCount height, disable the scroll feature of QTextEdit.

When the text change,I will reset the Qt::sizeHintRole data to resize the QTreeWidgetItem in height. To do so,I have to correctly calculate the height needed.

All texts are just plain texts, but with wordwrap enabled.

Here is what I've got:

My setting line height codes:(ui->textLbl is my QTextEdit widget,LineHeight is 25)

QTextDocument* doc = ui->textLbl->document();
QTextCursor textCursor = ui->textLbl->textCursor();

for(QTextBlock it = doc->begin(); it != doc->end(); it = it.next())
{
        QTextBlockFormat textBlockFormat = it.blockFormat();
        textBlockFormat.setLineHeight(LineHeight,QTextBlockFormat::FixedHeight);  //set line height
        textCursor.setBlockFormat(textBlockFormat);
        ui->textLbl->setTextCursor(textCursor);
}

Here is the string I setPlainText() with for the item #2: "Small Refine: Advanced mode has no effect.\nHaha,bingo!\nI like multi-lines." It turns out like so:

enter image description here

1> Issue #1: I can't get the correct linecount that iniclude the lines caused by the wordwrap feature.

2> Issue #2: It seems like setLineHeight only works for text lines with one paragraph. I have no control over spacing over different paragraphs.

So if I delete all newline symbol in my QTextEdit control by hand,and press enter to remake the newLine, It become so:

enter image description here

That is what I want.But I need the text could been set by setText method, too.

3> Issue #3: Still scrollable by Mid mouse button.Once I use midMouse button scroll on the QTextEdit,I find these are extra pixels below the line.the QTextEdit is scrollable.

enter image description here

Can somebody could help in these three issues? Thanks so much :)

Was it helpful?

Solution

1) try using

textEdit->document()->size().height()

2) textCursor is still on the beginning of the document (the first block), use

QTextCursor cursor(it); 

instead in that for-cycle to get QTextCursor of the given QTextBlock.

3) Scrollbar:

textEdit->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

and

textEdit->verticalScrollBar()->setMaximum(0)

(or connect to signal valueChanged/sliderMove and setValue again to zero)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top