Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QScrollQArea and EnsureWidgetVisible
Forum Updated to NodeBB v4.3 + New Features

QScrollQArea and EnsureWidgetVisible

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IluvQt
    wrote on last edited by
    #1

    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,

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Thrichard
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Keith Cancel
        wrote on last edited by
        #3

        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.

        ♫♪♪^。^ Always Whistling A Tune!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved