Search code examples
c++qtqtexteditqtwidgets

Qt - change the text of QTextBrowser inside GridLayout


I've got a QGridLayout filled with QTextBrowser's.

I am getting access to them using itemAtPosition(i,j), so I can eg. change to background using widget()->setStyleSheet.

Is there any way to change the text inside those fields?


Solution

  • Use setText() or append() to do this, but your way to get pointer is not very good. You should operate with original pointer. For example

    QTextBrowser txt;
    grid.addWidget(&txt,...);
    //...
    txt.append("example");//we just use object, we don't get this object from layout
    

    QTextBrowser inherits QWidget, so you can use setStyleSheet() too.

    About your current approach: of course you can cast your pointer to needed type using qobject_cast but try to use easier approach.

    (qobject_cast<QTextBrowser*>(grid.itemAtPosition(0,0)->widget()))->append("example");//is it so beautiful?