Create QScrollArea on heap
Qt Code:
To copy to clipboard, switch view to plain text mode
 
    
    
       
    
    
    
    
    
   
    
    
       
    
    
       
    
    
    
    
   Re: Add QScrollArea to QDialog
 Re: Add QScrollArea to QDialog
		Create QScrollArea on heap
Qt Code:
To copy to clipboard, switch view to plain text mode
 Novice
					
					
						Novice
					
					
                                        
					
						
							
								 
							
						
					
					
						 Re: Add QScrollArea to QDialog
 Re: Add QScrollArea to QDialog
		Hi,
I tried the following:
Qt Code:
setWindowTitle("Mixer Strip");
setMinimumSize(600, 350);
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setWidget(viewport);
scroll->setWidgetResizable(true);
setLayout(l);To copy to clipboard, switch view to plain text mode
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
 Re: Add QScrollArea to QDialog
		If you are adding widgets to QHBoxLayout *l then
Qt Code:
viewport->setLayout(l);To copy to clipboard, switch view to plain text mode
Last edited by Santosh Reddy; 23rd June 2011 at 17:52.
 Novice
					
					
						Novice
					
					
                                        
					
						
							
								 
							
						
					
					
						 Re: Add QScrollArea to QDialog
 Re: Add QScrollArea to QDialog
		I tried the following:
Qt Code:
scroll->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
scroll->setWidget(viewport);
scroll->setWidgetResizable(true);
viewport->setLayout(l);To copy to clipboard, switch view to plain text mode
The scrollbars are visible however they're still not attached to the dialog.
I also tried removing the call to
Qt Code:
scroll->setWidget(viewport)To copy to clipboard, switch view to plain text mode
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
 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
Qt 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();
}To copy to clipboard, switch view to plain text mode
1. If using QDialog directly, then
Qt 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();To copy to clipboard, switch view to plain text mode
Edit: your actual problem is you are adding QScrollArea to QDialog, where as you should be adding QScrollArea to QDialog's layout
Last edited by Santosh Reddy; 24th June 2011 at 03:02. Reason: updated contents
cpsmusic (24th June 2011)
 Novice
					
					
						Novice
					
					
                                        
					
						
							
								 
							
						
					
					
						 Re: Add QScrollArea to QDialog
 Re: Add QScrollArea to QDialog
		Thanks, that fixed the problem.
 Novice
					
					
						Novice
					
					
                                        
					
						
							
								 
							
						
					
					
						 Re: Add QScrollArea to QDialog
 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.
Qt Code:
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();
}To copy to clipboard, switch view to plain text mode
 
    
    
       
    
    
    
    
    
   
    
    
       
    
    
       
    
    
    
    
   Re: Add QScrollArea to QDialog
 Re: Add QScrollArea to QDialog
		You code should work, hlayout->addStretch(); does the magic...Post a screenshot indicating the of the probelm
 Novice
					
					
						Novice
					
					
                                        
					
						
							
								 
							
						
					
					
						 Re: Add QScrollArea to QDialog
 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
 Re: Add QScrollArea to QDialog
		Have you tried hlayout->setSizeConstraint ( QLayout::SetFixedSize ); ?
 Novice
					
					
						Novice
					
					
                                        
					
						
							
								 
							
						
					
					
						 Re: Add QScrollArea to QDialog
 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
 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)
 Novice
					
					
						Novice
					
					
                                        
					
						
							
								 
							
						
					
					
						 Re: Add QScrollArea to QDialog
 Re: Add QScrollArea to QDialog
		I'm not clear about how to set the size constraint in the widget. I tried adding:
Qt Code:
To copy to clipboard, switch view to plain text mode
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.
Last edited by cpsmusic; 25th June 2011 at 12:08. Reason: Text wrong!
 Re: Add QScrollArea to QDialog
 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?
Bookmarks