PDA

View Full Version : Definitively QScrollArea doesn't want work. I need help.



franco.amato
7th January 2010, 02:33
Hi, I'm becoming crazy with my customwidget.
I would write a widget that can grow orizontally and not vertically. ( fixed height and expanding width ) and I would attach a QScrollArea to it.
Well it don't work nothing.
My app inherits from QMainWidget and I do all in the centralwidget. ( in the central widget I would have 2 custom widget that's are WaveForm visualizer.

To test I add only one and it doesn't work.

Here the code:


CentralWidget::CentralWidget( QWidget* parent /* = 0 */ )
: QWidget(parent),
m_pVbox( 0 ),
m_pWaveU( 0 )
{
/* vertical layout */
m_pVbox = new QVBoxLayout( this );

/* scroll area */
saU = new QScrollArea( this );

saU->setWidgetResizable( true );

saU->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );

/* waveform */
m_pWaveU = new WaveDisplay( saU );

m_pWaveU->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_pWaveU->setMinimumSize( QSize(1000, 200) );

saU->setWidget( m_pWaveU );

m_pVbox->addWidget( saU );
QScrollBar *scrollBar = saU->horizontalScrollBar();
scrollBar->setSliderDown( true );

m_pVbox->addWidget( saU );

setLayout(m_pVbox);
}

I have lots of problems with this code:

If I set
QSizePolicy::Fixed for the vertical size the widget is not shown
The scroll bar is only displayed when I resize the widget at the minimum size
If I draw outsize the visible part of the widget I can not see what I displayed. The scrollbar doesn't allow me to see the graph


And other, if I call resize( w, h ) inside the widget, it's not resized. The width() routine give to me always the same value.
i'm really confused. Is 1 month that I try to do this work without any success.
I'll be very happy to get some help from this mailing list.

Best Regards,
Franco

faldzip
7th January 2010, 07:21
Hi, I'm becoming crazy with my customwidget.
So I think that your custom widget code would be more interesting to see if it is there what you done wrong.
Especially how your sizeHint() method looks like?

EDIT:
Consider this example:



#include <QtCore>
#include <QtGui>

class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
QSize sizeHint() const;
void paintEvent(QPaintEvent *pe);
private slots:
void slotClicked();
private:
QPushButton *m_button;
QSize m_size;
};

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
m_size = QSize(200, 80);
QHBoxLayout *lo = new QHBoxLayout;
m_button = new QPushButton("Button", this);
lo->addWidget(m_button);
connect(m_button, SIGNAL(clicked()), this, SLOT(slotClicked()));
}

void Widget::paintEvent(QPaintEvent *pe)
{
QWidget::paintEvent(pe);
QPainter p(this);
p.drawRect(QRect(0, 0, m_size.width()-1, m_size.height()-1));
}

QSize Widget::sizeHint() const
{
return m_size;
}

void Widget::slotClicked()
{
m_size.setWidth(m_size.width() + 100);
updateGeometry();
update();
}

int main(int argc, char **argv)
{
QApplication a(argc, argv);
QScrollArea sa;
sa.setWidgetResizable(true);
sa.setWidget(new Widget(&sa));
sa.widget()->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
sa.show();
return a.exec();
}

#include "main.moc"


You can experiment with size policy (read the documentation carefully about QSizePolicy::Policy) with reimplementing minimumSizeHint() and so on...