Russian Qt Forum
Октябрь 01, 2024, 10:17 *
Добро пожаловать, Гость. Пожалуйста, войдите или зарегистрируйтесь.
Вам не пришло письмо с кодом активации?

Войти
 
  Начало   Форум  WIKI (Вики)FAQ Помощь Поиск Войти Регистрация  

Страниц: [1]   Вниз
  Печать  
Автор Тема: [РЕШЕНО] QScrollArea не изменяет размеры при добавлении контента  (Прочитано 3043 раз)
Alexander_57
Гость
« : Июль 25, 2012, 16:34 »

Добрый день, форумчане. Помогите пожалуйста с возникшей проблемой. Код внизу и во вложении.
Имеется 2 столбца элементов. В правом (QFrame) есть кнопка "Add", при нажатии на которую внизу в QGridLayout (который находится в QScrollArea) добавляются элементы. Нужно чтобы при добавлении QFrame увеличивался вниз до пределов левого столбца. А потом появлялась прокрутка в QScrollArea.
Все элементы взяты для примера.

-------------main.cpp--------------------------------------------
#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    
    return a.exec();
}
---------------------------------------------------------
-------------widget.h--------------------------------------------
#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui/QWidget>

class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget(QWidget *parent = 0);
};

#endif // WIDGET_H
---------------------------------------------------------
-------------widget.cpp--------------------------------------------
#include "widget.h"
#include <QtGui/QHBoxLayout>
#include <QtGui/QCalendarWidget>
#include "frame.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    QHBoxLayout *pBoxLayoutBase = new QHBoxLayout();

    QVBoxLayout *pVBoxLayoutLeft = new QVBoxLayout();
    for (int i = 0; i < 4; ++i)
    {
        QCalendarWidget *pCalendar = new QCalendarWidget();
        pCalendar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
        pVBoxLayoutLeft->addWidget(pCalendar);
    }
    pBoxLayoutBase->addLayout(pVBoxLayoutLeft);

    QVBoxLayout *pVBoxLayoutRight = new QVBoxLayout();
    Frame *pFrame = new Frame();
    pVBoxLayoutRight->addWidget(pFrame);
    pVBoxLayoutRight->addStretch(1);
    pBoxLayoutBase->addLayout(pVBoxLayoutRight);

    setLayout(pBoxLayoutBase);
}
---------------------------------------------------------
-------------frame.h--------------------------------------------
#ifndef FRAME_H
#define FRAME_H

#include <QFrame>
class QGridLayout;
class QScrollArea;

class Frame : public QFrame
{
    Q_OBJECT
public:
    explicit Frame(QWidget *parent = 0);

private slots:

    void SlotAdd();

private:

    QGridLayout *m_pGridLayout;
    QScrollArea *m_pScrollArea;
};

#endif // FRAME_H
---------------------------------------------------------
-------------frame.cpp--------------------------------------------
#include "frame.h"
#include <QtGui/QVBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QLabel>
#include <QtGui/QDateEdit>
#include <QtGui/QLineEdit>
#include <QtGui/QDateEdit>
#include <QtGui/QScrollArea>
#include <QtGui/QApplication>
#include <QtGui/QScrollBar>


Frame::Frame(QWidget *parent) : QFrame(parent)
{
//    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    QVBoxLayout *const pVBoxLayoutBase = new QVBoxLayout();

    QPushButton *const pAddButton = new QPushButton("Add");
    pAddButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    connect(pAddButton, SIGNAL(clicked()), SLOT(SlotAdd()));
    pVBoxLayoutBase->addWidget(pAddButton);

    QWidget *const pWidget = new QWidget(this);
    QVBoxLayout *const pVBoxLayout = new QVBoxLayout();
    m_pGridLayout = new QGridLayout();
    m_pGridLayout->addWidget(new QLabel(tr("Type"), this), 0, 0, Qt::AlignCenter);
    m_pGridLayout->addWidget(new QLabel(tr("Start"), this), 0, 1, Qt::AlignCenter);
    m_pGridLayout->addWidget(new QLabel(tr("End"), this), 0, 2, Qt::AlignCenter);
    QFrame *const pHLine = new QFrame(this);
    pHLine->setFrameShape(QFrame::HLine);
    pHLine->setFrameShadow(QFrame::Sunken);
    m_pGridLayout->addWidget(pHLine, 1, 0, 1, m_pGridLayout->columnCount());
    pVBoxLayout->addLayout(m_pGridLayout);
    pVBoxLayout->addStretch(1);
    pWidget->setLayout(pVBoxLayout);

    m_pScrollArea = new QScrollArea(this);
//    m_pScrollArea->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
    m_pScrollArea->setWidgetResizable(true);
    m_pScrollArea->setWidget(pWidget);
    pVBoxLayoutBase->addWidget(m_pScrollArea);

    setLayout(pVBoxLayoutBase);
}

void Frame::SlotAdd()
{
    int RowCount(m_pGridLayout->rowCount());
    m_pGridLayout->addWidget(new QLineEdit(), RowCount, 0);
    m_pGridLayout->addWidget(new QDateEdit(), RowCount, 1);
    m_pGridLayout->addWidget(new QDateEdit(), RowCount, 2);
}
---------------------------------------------------------
« Последнее редактирование: Июль 26, 2012, 10:55 от Alexander_57 » Записан
Alexander_57
Гость
« Ответ #1 : Июль 26, 2012, 09:43 »

Решил задачу граблями. Кому интересно код внизу.
В классе Frame переопределил sizeHint().

QSize Frame::sizeHint() const
{
    if (!parentWidget() || !m_pScrollArea)
        return QFrame::sizeHint();
    QWidget *const pWidget(m_pScrollArea->widget());
    if (!pWidget)
        return QFrame::sizeHint();
    int Height(height() - m_pScrollArea->viewport()->height() + pWidget->sizeHint().height());
    if (Height < parentWidget()->height())
        return QSize(width(), Height);
    else
        return QSize(width(), parentWidget()->height());
}
Записан
_OLEGator_
Гость
« Ответ #2 : Июль 26, 2012, 10:26 »

Закинь проект во вложении. Скорее всего у тебя косяк где то.
Записан
Страниц: [1]   Вверх
  Печать  
 
Перейти в:  


Страница сгенерирована за 0.104 секунд. Запросов: 20.