Add QScrollArea to QDialog
Hi,
Is it possible to create a Qdialog with a QScrollArea?
I've tried the following but neither method works:
Code:
scrollArea.
viewport()->setBackgroundRole
(QPalette::Dark);
scrollArea.viewport()->setAutoFillBackground(true);
scrollArea.
setWindowTitle(QObject::tr("Mixer Strip"));
scrollArea.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea.setParent(this);
scrollArea.show();
Code:
scroll.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll.setWidget(viewport);
scroll.setWidgetResizable(true);
setLayout(l);
scroll.show();
Cheers,
Chris
Re: Add QScrollArea to QDialog
The second example seems to be correct, but what is the parent of QScrollArea?
Re: Add QScrollArea to QDialog
If I change the code to the following I still don't see the QScrollArea:
Code:
scroll.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll.setWidget(viewport);
scroll.setWidgetResizable(true);
setLayout(l);
scroll.setParent(this);
scroll.show();
Re: Add QScrollArea to QDialog
Create QScrollArea on heap
Re: Add QScrollArea to QDialog
Hi,
I tried the following:
Code:
setWindowTitle("Mixer Strip");
setMinimumSize(600, 350);
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setWidget(viewport);
scroll->setWidgetResizable(true);
setLayout(l);
Using this code the scrollbars are visible however they're positioned in the top left corner of the dialog and don't change position when the dialog is resized. I can just makeout the widgets that are added.
Cheers,
Chris
Re: Add QScrollArea to QDialog
If you are adding widgets to QHBoxLayout *l then
Code:
viewport->setLayout(l);
Re: Add QScrollArea to QDialog
I tried the following:
Code:
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setWidget(viewport);
scroll->setWidgetResizable(true);
viewport->setLayout(l);
The scrollbars are visible however they're still not attached to the dialog.
I also tried removing the call to
Code:
scroll->setWidget(viewport)
In this case, the scrollbars are visible (and the size of the area they contain is larger) but they're still not attached to the dialog.
Re: Add QScrollArea to QDialog
I think I got you problem, not sure how you are using QDialog, let me put both the ways, check which is applicable to you
1. If sub-classing QDialog, then in the constructor of the sub-calss
Code:
{
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setWidget(viewport);
scroll->setWidgetResizable(true);
viewport->setLayout(l);
// add needed widgets to layout "l"
for(int i = 0; i < 10; i++)
// Add a layout for QDialog
dialog_layout->addWidget(scroll); // add scroll to the QDialog's layout
setLayout(dialog_layout);
// show dialog
show();
}
1. If using QDialog directly, then
Code:
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setWidget(viewport);
scroll->setWidgetResizable(true);
viewport->setLayout(l);
// add needed widgets to layout "l"
for(int i = 0; i < 10; i++)
// Add a layout for QDialog
dialog->setLayout(dialog_layout);
dialog->layout()->addWidget(scroll); // add scroll to the QDialog's layout
dialog->show();
Edit: your actual problem is you are adding QScrollArea to QDialog, where as you should be adding QScrollArea to QDialog's layout
Re: Add QScrollArea to QDialog
Thanks, that fixed the problem. :)
Re: Add QScrollArea to QDialog
Although I've got the scrollbars showing now, I've run into another problem - the widgets are being spread too widely in the horizontal direction. What I'd like is for the widgets to bunch up on the left hand side of the layout. I tried using a QGridLayout however this didn't fix the problem. The code I'm using is below. One thing, the widget is a custom widget that has a fixed size. I'm implemented the sizeHint method returning the size of the widget.
Code:
MixerDialog
::MixerDialog(QWidget *parent
) :
setWindowTitle("Mixer");
setMinimumSize(600, 350);
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scrollArea->setWidget(viewport);
scrollArea->setWidgetResizable(true);
hlayout->setMargin(0);
hlayout->setSpacing(0);
viewport->setLayout(hlayout);
// add needed widgets to layout "hlayout"
int numberOfChannels = 10;
for(int i = 0; i < numberOfChannels; i++) {
MixerStrip *strip = new MixerStrip;
hlayout->addWidget(strip);
}
hlayout->addStretch();
// Add a layout for QDialog
dialog_layout->setMargin(0);
dialog_layout->setSpacing(0);
dialog_layout->addWidget(scrollArea); // add scrollArea to the QDialog's layout
setLayout(dialog_layout);
// show dialog
show();
}
Re: Add QScrollArea to QDialog
You code should work, hlayout->addStretch(); does the magic...Post a screenshot indicating the of the probelm
Re: Add QScrollArea to QDialog
Here's a screenshot of how the widgets are being laid out:
Grab.tiff
The addStretch() doesn't seem to push them left.
Re: Add QScrollArea to QDialog
Have you tried hlayout->setSizeConstraint ( QLayout::SetFixedSize ); ?
Re: Add QScrollArea to QDialog
Quote:
Originally Posted by
Rachol
Have you tried hlayout->setSizeConstraint ( QLayout::SetFixedSize ); ?
I just tried what you've suggested however it didn't make any difference.
Re: Add QScrollArea to QDialog
Can you try your code with QPushButton instead of MixerStrip and then see if the widgets are being laid out normally?
Re: Add QScrollArea to QDialog
Ok, Your problem seems to be with in mixer widget, set proper size constraint to it, and also don't use any spacers if designing from Qt Designer (i mean don't place spacers in such a position which will cause the mixer widget to fit the area available)
Re: Add QScrollArea to QDialog
I'm not clear about how to set the size constraint in the widget. I tried adding:
Code:
setSizeConstraint
( QLayout::SetFixedSize );
to the MixerStrip (which inherits from QWidget) but the code won't build.
I'm not using Qt Designer so I don't think that's the problem.
Re: Add QScrollArea to QDialog
setSizeConstraint -> this is QLayout API, so of course it won't work. Have you tried what I suggested in my previous post? How did it work? How is your MixerStrip widget implemented?
Re: Add QScrollArea to QDialog
You can use
Code:
hlayout.
setSizeConstraint(QLayout::SetMinimumSize);
This will set the layout and the containing widget to smallest size possible.
Re: Add QScrollArea to QDialog
Quote:
Originally Posted by
Rachol
Can you try your code with QPushButton instead of MixerStrip and then see if the widgets are being laid out normally?
If I use QPushButtons they're laid out correctly.
Here's the code for the MixerStrip:
Code:
#include "mixerstrip.h"
MixerStrip
::MixerStrip(QWidget *parent
) :{
volSlider->setMinimum(0);
volSlider->setMaximum(100);
pal.
setColor(volSlider
->backgroundRole
(),
QColor(128,
128,
128));
volSlider->setPalette(pal);
volSlider->setAutoFillBackground(TRUE);
volSlider->setGeometry(0,0,20,220);
volSlider->setParent(this);
Meter *meter = new Meter;
meter->setGeometry(20,0,50,220);
meter->setParent(this);
muteButton->setCheckable(true);
soloButton->setCheckable(true);
pal = muteButton->palette();
pal.
setColor(muteButton
->backgroundRole
(),
QColor(128,
128,
128));
muteButton->setPalette(pal);
muteButton->setAutoFillBackground(TRUE);
pal = soloButton->palette();
pal.
setColor(soloButton
->backgroundRole
(),
QColor(128,
128,
128));
soloButton->setPalette(pal);
soloButton->setAutoFillBackground(TRUE);
muteButton->setGeometry(0,220,70,20);
soloButton->setGeometry(0,240,70,20);
muteButton->setParent(this);
soloButton->setParent(this);
panSlider->setMinimum(-100);
panSlider->setMaximum(100);
panSlider->setValue(0);
pal = panSlider->palette();
pal.
setColor(panSlider
->backgroundRole
(),
QColor(128,
128,
128));
panSlider->setPalette(pal);
panSlider->setAutoFillBackground(TRUE);
panSlider->setGeometry(0,260,70,20);
panSlider->setParent(this);
trackName->setAlignment(Qt::AlignCenter);
trackName->setText("Track");
trackName->setGeometry(0,280,70,20);
trackName->setParent(this);
QObject::connect(volSlider,
SIGNAL(valueChanged
(int)), meter,
SLOT(valueChanged
(int)));
volSlider->setValue(0);
setFixedSize(size());
//setSizeConstraint ( QLayout::SetFixedSize );
}
QSize MixerStrip
::sizeHint() const {
return size;
}
Quote:
Originally Posted by
Santosh Reddy
You can use
Code:
hlayout.
setSizeConstraint(QLayout::SetMinimumSize);
This will set the layout and the containing widget to smallest size possible.
I tried what you suggested but it still doesn't fix the problem.