<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[QScrollQArea and EnsureWidgetVisible]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">The following application starts with a single QLineEdit.<br />
Each time the user will press ENTER, a new LineEdit appears and I want to get the focus on it.</p>
<p dir="auto">All those QLineEdit are in a QVBoxLayout, contained in a QScrollArea.<br />
I want the last QLineEdit to be visible on viewport, that's why I used the method EnsureWidgetVisible from QScrollArea but I didn't succeed it to work.</p>
<p dir="auto">Here is my code:</p>
<p dir="auto">bug.h<br />
@<br />
#ifndef BUG_H<br />
#define BUG_H<br />
#include &lt;QScrollArea&gt;<br />
#include &lt;QVector&gt;</p>
<p dir="auto">class QLineEdit;<br />
class QVBoxLayout;</p>
<p dir="auto">class Scroll : public QScrollArea{<br />
Q_OBJECT<br />
public:<br />
Scroll(QWidget *parent=0);<br />
private slots:<br />
void addLine();<br />
private:<br />
QWidget <em>mainPanel;<br />
int current;<br />
QVector &lt;QLineEdit <em>&gt; lines;<br />
QVBoxLayout</em> vLayout;<br />
QVBoxLayout</em> mainLayout;<br />
};<br />
#endif<br />
@</p>
<p dir="auto">bug.cpp<br />
@<br />
#include &lt;QtGui&gt;<br />
#include "bug.h"</p>
<p dir="auto">Scroll::Scroll(QWidget* ):QScrollArea(){<br />
current=0;</p>
<pre><code>mainPanel=new QWidget;

vLayout=new QVBoxLayout;
mainLayout=new QVBoxLayout;
QLineEdit *line=new QLineEdit(this);
connect(line,SIGNAL(returnPressed()),this,SLOT(addLine()));
lines.append(line);
vLayout-&gt;addWidget(lines.at(0));
mainLayout-&gt;addLayout(vLayout);
mainLayout-&gt;addStretch(1);
mainPanel-&gt;setLayout(mainLayout);
mainPanel-&gt;setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

setWidget(mainPanel);
setWidgetResizable(true);

lines.last()-&gt;setFocus(Qt::OtherFocusReason);
</code></pre>
<p dir="auto">}<br />
void Scroll::addLine(){<br />
QLineEdit *line=new QLineEdit(this);<br />
connect(line,SIGNAL(returnPressed()),this,SLOT(addLine()));<br />
lines.append(line);<br />
vLayout-&gt;addWidget(line);<br />
line-&gt;setFocus(Qt::OtherFocusReason);<br />
line-&gt;show();<br />
ensureWidgetVisible(line);<br />
}<br />
@</p>
<p dir="auto">and main.cpp<br />
@#include &lt;QApplication&gt;<br />
#include "bug.h"</p>
<p dir="auto">int main(int argc, char* argv[]){<br />
QApplication app(argc,argv);<br />
Scroll *s=new Scroll;<br />
s-&gt;show();<br />
return (app.exec());</p>
<p dir="auto">}<br />
@</p>
<p dir="auto">If anyone has encountered this problem, thanks in advance for your help.<br />
Best,</p>
]]></description><link>https://forum.qt.io/topic/5153/qscrollqarea-and-ensurewidgetvisible</link><generator>RSS for Node</generator><lastBuildDate>Sun, 16 Aug 2026 16:00:40 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/5153.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 16 Apr 2011 10:15:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QScrollQArea and EnsureWidgetVisible on Fri, 22 Apr 2011 15:55:59 GMT]]></title><description><![CDATA[<p dir="auto">Add a container widget to the scroll area and set the minimum size to the container widget and place all the other widgets into the container widget you added. Then the scroll area should work.</p>
]]></description><link>https://forum.qt.io/post/85042</link><guid isPermaLink="true">https://forum.qt.io/post/85042</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 22 Apr 2011 15:55:59 GMT</pubDate></item><item><title><![CDATA[Reply to QScrollQArea and EnsureWidgetVisible on Fri, 22 Apr 2011 10:08:39 GMT]]></title><description><![CDATA[<p dir="auto">It gains the focus for me (copy-pasting your code) for</p>
<ul>
<li>Qt 4.5.3 on Windows 7 (mingw 4.3.4)</li>
<li>Qt 4.7.1 on Linux (gcc 4.1.2)</li>
</ul>
<p dir="auto">Or do you want only the last one to be visible on your list? Or you want the scroll area to scroll the view down when more are added?</p>
]]></description><link>https://forum.qt.io/post/85005</link><guid isPermaLink="true">https://forum.qt.io/post/85005</guid><dc:creator><![CDATA[[[global:former-user]]]]></dc:creator><pubDate>Fri, 22 Apr 2011 10:08:39 GMT</pubDate></item></channel></rss>