简体   繁体   English

将文本编辑的滚动条移至顶部

[英]Move Scrollbar of a textedit to the top

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.我将图像放入 QTextEdit 中,当我打开滚动条时,滚动条位于底部。 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'>");
}
  • 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.我认为这不起作用,因为QTextEdit将在插入后的下一个 qt 事件循环更新中向下滚动。 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); }); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM