2

hi i'm having some issues to move the scrollbar to the top. I put a image into a QTextEdit and when i open the scrollbar is in the bottom. I need that the scrollbar is on the top

i tried all these but it didn't work for me. still the same.

ui.textEdit->verticalScrollBar()->setValue(0);
myTextEdit -> moveCursor (QTextCursor::Start) ;
myTextEdit -> ensureCursorVisible() ;
QScrollBar *vScrollBar = yourTextEdit->verticalScrollBar();
vScrollBar->triggerAction(QScrollBar::SliderToMinimum);

here is my code.

RulesDialog::RulesDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::RulesDialog)
{
    ui->setupUi(this);    
    setWindowIcon(QIcon(":/images/icon3.png"));
    ui->textEdit->insertHtml("<img src=':/images/reglas.png'>");
}
Mattii
  • 109
  • 6
  • Possible duplicate of [How to scroll QPlainTextEdit to top?](https://stackoverflow.com/questions/7280965/how-to-scroll-qplaintextedit-to-top) – Rick Pat Aug 04 '19 at 17:17
  • i read that post, but it didn't fix the problem. – Mattii Aug 04 '19 at 18:25
  • Ok, you are right, you have a different problem, i retract the duplicate flag. – Rick Pat Aug 04 '19 at 20:00
  • @Mattii For future reference, please edit your question and explain why your question is different. –  Aug 04 '19 at 23:19

1 Answers1

2
  • myTextEdit->moveCursor(QTextCursor::Start);
    This will not work because the cursor covers the vertical length of the image and so will not move and neither will the scrollbar. If you had text above the image it would work.

  • vScrollBar->triggerAction(QScrollBar::SliderToMinimum);
    This I think does not work because the QTextEdit will scroll down on the next qt event loop update after an insert. The following is a workaround for that, it will defer the scrolling up to the next event loop update:

    RulesDialog::RulesDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::RulesDialog)
    {
        ui->setupUi(this);    
        setWindowIcon(QIcon(":/images/icon3.png"));
        ui->textEdit->insertHtml("<img src=':/images/reglas.png'>");
    
        scrollUpLater();
    }
    
    void RulesDialog::scrollUpLater()
    {
        QTimer::singleShot(0, [this](){
            ui->textEdit->verticalScrollBar()->triggerAction(QScrollBar::SliderToMinimum);
            // or // ui->textEdit->verticalScrollBar()->setValue(0);
        });
    }
    
Rick Pat
  • 789
  • 5
  • 14