Scroll area not working...
-
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_OBJECTpublic:
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...
-
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...!!!
-
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 rowspublic:
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.