QScrollArea显示指定的子控件


当我们使用QScrollArea向里面添加控件时,希望显示到最近添加的那个控件或者显示指定的控件

ui.scrollArea->ensureWidgetVisible(childWidget);

但是当动态添加控件后

  1. buttons.append(new QPushButton("New button",scrollAreaWidgetContents_2));
  2. buttons[buttons.count()-1]->setGeometry( QRect(80, 330, 112, 26));
  3. horizontalLayout_2->addWidget(buttons[buttons.count()-1]); //Add the new element and it is show in the screen
  4. //How to move to this new element?
  5. scrollArea->ensureWidgetVisible(buttons[buttons.count()-1],0,0); //This does not work

发现并不会显示到新加的控件,是因为添加完新控件scrollArea会调整期大小导致后面语句不生效

qApp->processEvents()

scrollArea->ensureWidgetVisible(buttons[buttons.count()-1],0,0);

再设置ensureWidgetVisible前面加上这个qApp->processEvents(),基本能解决问题

如果还是有问题

qApp->processEvents()

qApp->processEvents()

scrollArea->ensureWidgetVisible(buttons[buttons.count()-1],0,0)

我这边就加两边才精确定位到到显示的子控件

这里面有好多知识点

参考网址:https://www.qtcentre.org/threads/30632-QScrollArea-Scroll-to-widget-problem

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM