[SOLVED] Creating editor without scrolling



  • Hello!
    I want to create editor, which will change it's size for content (without scroll). I take QPlainTextEdit, and make next connection:

    connect(ui->plainTextEdit->document()->documentLayout(), 
    SIGNAL(documentSizeChanged(QSizeF)),
    SLOT(editorSizeChanged(QSizeF)));
    

    And in slot I take received height, and multiply it to pixels height of one line:

     QFont font("", 0);
     QFontMetrics fm(font);
     int pixelsHigh = fm.lineSpacing(); 
     ui->plainTextEdit->setFixedHeight((inSize.height()+1)*pixelsHigh);
    

    This construction works perfectly until we have < 5 lines in editor. After that in the bottom of the editor appears extra space, and scrollbar at right.

    What i'm doing wrong ?
    Minimal example


  • Qt Champions 2017

    Hi and welcome
    Have a look at
    QAbstractScrollArea::setVerticalScrollBarPolicy()
    QAbstractScrollArea::setHorizontalScrollBarPolicy( )

    and the Qt::ScrollBarAlwaysOff value



  • @mrjj scrollbars already disabled in Qt Designer. Problem actually in extra space, which causes incorrect size computing.


  • Qt Champions 2017

    @Yakov-Eremin
    hi
    I think your issue is that you use another font for getting line height than the actual editor and
    since you multiply then slowly the space grows.

    try with

    void MainWindow::editorSizeChanged(QSizeF inSize)
    {
    QFont font=ui->plainTextEdit->font();
    .....



  • @mrjj thank you, the problem is solved


  • Qt Champions 2017

    @Yakov-Eremin
    you're welcome.
    Very good with the mini sample !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.