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. Scroll area not working...
Forum Updated to NodeBB v4.3 + New Features

Scroll area not working...

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 7.1k 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.
  • A Offline
    A Offline
    aurora
    wrote on last edited by
    #1

    i created scroll area using designer and adding line edit in run time, when button clicked....
    But scroll area is not expanding(scroll bars are inactive even though i set scrollbarAlways on)
    please help me...
    here is my complete code..
    mainwindow.h
    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private slots:
    void on_pushButton_clicked();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @

    mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::on_pushButton_clicked()
    {
    QLineEdit *le=new QLineEdit;

    ui->gridLayout->addWidget(le);

    ui->scrollArea->setWidgetResizable(true);

    }
    @

    mainwindow.ui
    @
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
    <class>MainWindow</class>
    <widget class="QMainWindow" name="MainWindow">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
    </rect>
    </property>
    <property name="windowTitle">
    <string>MainWindow</string>
    </property>
    <widget class="QWidget" name="centralWidget">
    <layout class="QVBoxLayout" name="verticalLayout">
    <property name="sizeConstraint">
    <enum>QLayout::SetMinAndMaxSize</enum>
    </property>
    <item>
    <widget class="QScrollArea" name="scrollArea">
    <property name="widgetResizable">
    <bool>true</bool>
    </property>
    <widget class="QWidget" name="scrollAreaWidgetContents">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>380</width>
    <height>198</height>
    </rect>
    </property>
    <widget class="QWidget" name="">
    <property name="geometry">
    <rect>
    <x>40</x>
    <y>31</y>
    <width>216</width>
    <height>25</height>
    </rect>
    </property>
    <layout class="QGridLayout" name="gridLayout">
    <property name="sizeConstraint">
    <enum>QLayout::SetMinAndMaxSize</enum>
    </property>
    <item row="0" column="0">
    <widget class="QPushButton" name="pushButton_2">
    <property name="text">
    <string>PushButton</string>
    </property>
    </widget>
    </item>
    <item row="0" column="1">
    <widget class="QLineEdit" name="lineEdit"/>
    </item>
    </layout>
    </widget>
    </widget>
    </widget>
    </item>
    <item>
    <widget class="QPushButton" name="pushButton">
    <property name="text">
    <string>PushButton</string>
    </property>
    </widget>
    </item>
    </layout>
    </widget>
    <widget class="QMenuBar" name="menuBar">
    <property name="geometry">
    <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>21</height>
    </rect>
    </property>
    </widget>
    <widget class="QToolBar" name="mainToolBar">
    <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
    </attribute>
    <attribute name="toolBarBreak">
    <bool>false</bool>
    </attribute>
    </widget>
    <widget class="QStatusBar" name="statusBar"/>
    </widget>
    <layoutdefault spacing="6" margin="11"/>
    <resources/>
    <connections/>
    </ui>
    @

    please tell me what am i missing here...

    1 Reply Last reply
    0
    • J Offline
      J Offline
      joonhwan
      wrote on last edited by
      #2

      QScrollArea comprise of itself and its child viewport widget.

      in your code, try to do "ui->scrollArea->viewPort()->setSize(...)" for resizing the content of your scrollarea widget.

      joonhwan at gmail dot com

      1 Reply Last reply
      0
      • P Offline
        P Offline
        pi88el
        wrote on last edited by
        #3

        You have to set a Widget into the scrolArea and set the lineEdit('s) into this new widget. also the gridLayout have to be the layout of the new widget.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          aurora
          wrote on last edited by
          #4

          Thanks all for ur reply...
          But sorry, i tried but that didnt work,,,.:(

          I took a main window
          Added scrollarea
          Aplied vertical layout to the whole mainwindow
          added push button and a line edit inside the scroll area and applied grid layout to it.
          Kept another push button outside the scroll area, when it clicked adding another line edit into the gridlayout which is inside the scrol area.....
          Now please tell me, what changes i should do?

          Here line edit added correctly but line edit added aftr some limit not displaying, means scroll area not giving any chance to scroll...!!!

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pi88el
            wrote on last edited by
            #5

            So the first change you have to do is: Forget the qtdesigner, there is no benefit to use it. Create the window on your own, especially if you want learn more about Qt.
            2. I think you first have to learn more about c++ before you start working with frameworks.
            But anyway, look at this example:

            mainwindow.h
            @#ifndef MAINWINDOW_H
            #define MAINWINDOW_H

            #include <QMainWindow>
            #include <QPushButton>
            #include <QWidget>
            #include <QGridLayout>
            #include <QScrollArea>
            #include <QLineEdit>
            #include <QVBoxLayout>

            class MainWindow : public QMainWindow {
            Q_OBJECT

            // all your objects
            QWidget *centralWidget; // your centralwidget
            QVBoxLayout *cwLayout; // your layout for the centralwidget
            QScrollArea *scrollArea; // your scrollarea
            QWidget *scrollWidget; // widget for the scrollare
            QGridLayout swLayout; // the grid layout
            QPushButton addButton; // adds a new rows in the gridlayout
            typedef QPair<QPushButton
            , QLineEdit
            > pair_t; // pair for the rows
            QList<pair_t> rows; // the list of rows

            public:
            explicit MainWindow(QWidget *parent = 0);
            ~MainWindow();

            private slots:
            void addLine();
            };

            #endif // MAINWINDOW_H
            @

            mainwindow.cpp
            @#include "mainwindow.h"

            MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent) {
            centralWidget = new QWidget(this);
            scrollWidget = new QWidget(centralWidget);
            swLayout = new QGridLayout(scrollWidget);

            addButton = new QPushButton(tr("Add line"), centralWidget);

            connect(addButton, SIGNAL(clicked()), this, SLOT(addLine()));

            scrollArea = new QScrollArea(centralWidget);
            scrollArea->setBackgroundRole(QPalette::Dark);
            scrollArea->setWidget(scrollWidget);
            scrollArea->setWidgetResizable(true);

            cwLayout = new QVBoxLayout(centralWidget);
            cwLayout->addWidget(scrollArea);
            cwLayout->addWidget(addButton);

            setCentralWidget(centralWidget);
            }

            MainWindow::~MainWindow() {
            destroy(true, true);
            // because of the inheritanceof all widgets they will be deleted automatically on deleting *this
            // but it is better if you destroy it yourself
            }

            void MainWindow::addLine() {
            QPushButton *newButton = new QPushButton(tr("Button ") + QString::number(rows.count()+1), scrollWidget);
            QLineEdit *newLine = new QLineEdit(scrollWidget);
            swLayout->addWidget(newButton, rows.count(), 0);
            swLayout->addWidget(newLine, rows.count(), 1);
            rows.append(qMakePair(newButton, newLine));
            }
            @

            Remove the mainwindow.ui and it should work. also remive the line FORMS = mainwindow.ui in the .pro file.

            1 Reply Last reply
            0
            • A Offline
              A Offline
              aurora
              wrote on last edited by
              #6

              OH THANK A LOT...:)
              I GOT IT.....

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aurora
                wrote on last edited by
                #7

                In designer also i got it done...by adding these two line...:)
                @
                ui->scrollAreaWidgetContents->layout()->addWidget(checkbox);
                ui->scrollAreaWidgetContents->layout()->addWidget(lineEdit);
                @

                Anyway thak u so much for ur help 'pi88el'....:)

                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