QScrollQArea and EnsureWidgetVisible
-
Hello,
The following application starts with a single QLineEdit.
Each time the user will press ENTER, a new LineEdit appears and I want to get the focus on it.All those QLineEdit are in a QVBoxLayout, contained in a QScrollArea.
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.Here is my code:
bug.h
@
#ifndef BUG_H
#define BUG_H
#include <QScrollArea>
#include <QVector>class QLineEdit;
class QVBoxLayout;class Scroll : public QScrollArea{
Q_OBJECT
public:
Scroll(QWidget *parent=0);
private slots:
void addLine();
private:
QWidget mainPanel;
int current;
QVector <QLineEdit > lines;
QVBoxLayout vLayout;
QVBoxLayout mainLayout;
};
#endif
@bug.cpp
@
#include <QtGui>
#include "bug.h"Scroll::Scroll(QWidget* ):QScrollArea(){
current=0;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->addWidget(lines.at(0)); mainLayout->addLayout(vLayout); mainLayout->addStretch(1); mainPanel->setLayout(mainLayout); mainPanel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed); setWidget(mainPanel); setWidgetResizable(true); lines.last()->setFocus(Qt::OtherFocusReason);
}
void Scroll::addLine(){
QLineEdit *line=new QLineEdit(this);
connect(line,SIGNAL(returnPressed()),this,SLOT(addLine()));
lines.append(line);
vLayout->addWidget(line);
line->setFocus(Qt::OtherFocusReason);
line->show();
ensureWidgetVisible(line);
}
@and main.cpp
@#include <QApplication>
#include "bug.h"int main(int argc, char* argv[]){
QApplication app(argc,argv);
Scroll *s=new Scroll;
s->show();
return (app.exec());}
@If anyone has encountered this problem, thanks in advance for your help.
Best, -
It gains the focus for me (copy-pasting your code) for
- Qt 4.5.3 on Windows 7 (mingw 4.3.4)
- Qt 4.7.1 on Linux (gcc 4.1.2)
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?
-
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.