PDA

View Full Version : Custom Scroll Widget Sizing Problem



sepehr
28th November 2008, 07:36
the basic idea of following code is to create a collection of widgets and add the collection to a scroll area,each collection has at least 2 labels describing it's contents


#ifndef collection_class
#define collection_class
#include <QWidget>
#include <QLabel>
#include <QVBoxLayout>
#include <QGridLayout>
class collection:public QWidget
{
private:


static QVBoxLayout* mainLayout;
QGridLayout innerLayout
QLabel caption;

public:
QSize sizeHint();
collection(QWidget *parent,QString collectionName,QString collectionCaption);
static QVBoxLayout* returnMainLayout();
QLabel triClick;

};
#endif



#include "collection.h"

collection::collection(QWidget *parent, QString collectionName, QString collectionCaption)
:QWidget(parent)
{

innerLayout.setProperty("name",collectionName);
triClick.setText(collectionName);
caption.setText(collectionCaption);
innerLayout.addWidget(&triClick,0,0);
innerLayout.addWidget(&caption,0,1);
mainLayout->addLayout(&innerLayout);

}
static QVBoxLayout* collection::returnMainLayout()
{
return mainLayout;
}
QSize collection::sizeHint()
{
return QSize(100,200);
}




#ifndef scroll_class
#define scroll_class
#include "collection.h"
#include <QScrollArea>
class scroll:public QScrollArea
{
public:
scroll(QWidget *parent=0);
collection* addCollection(QString collectionName, QString collectionLabel);
bool addWidget(QString collectionName, QWidget* object2Add,QString objectIdentifier);
QSize sizeHint();
};
#endif



#include "scroll.h"
#include <QDebug>
scroll::scroll(QWidget *parent)
:QScrollArea(parent)
{

setMaximumSize(200,80);
// **** point of interest *****
collection* collectionTmpPtr=new collection(this, "", "");
collectionTmpPtr=new collection(this, "shoot", "wow");
collectionTmpPtr=new collection(this, "shoot3S", "wow");
collectionTmpPtr=new collection(this, "shoot3S", "wow");
collectionTmpPtr=new collection(this, "shoot3S", "wow");
collectionTmpPtr=new collection(this, "shoot3S", "wow");
collectionTmpPtr=new collection(this, "shoot3S", "wow");
collectionTmpPtr=new collection(this, "shoot3S", "wow");
collectionTmpPtr=new collection(this, "shoot3S", "wow");
collectionTmpPtr=new collection(this, "shoot3S", "wow");

QWidget * widget=new QWidget(parent);
VBoxLayout *mainLayout = collection::returnMainLayout() ;
widget->setLayout(mainLayout);
setWidget(widget);

}
collection* scroll::addCollection(QString collectionName,QString collectionLabel)
{
collection* collectionTmpPtr=new collection(this, collectionName, collectionLabel);
return collectionTmpPtr;
}
bool scroll::addWidget(QString collectionName, QWidget* object2Add, QString objectIdentifier)
{

QVBoxLayout *mainLayout = collection::returnMainLayout();

for (int i = 0; i < mainLayout->count(); ++i)
{

QLayout* item=mainLayout->itemAt(i)->layout();
if (item !=0) //if item is a layout
if (item->property("name")==collectionName) //if that's the collection user is looking for to add items to
{
object2Add->setProperty ("name",objectIdentifier);
QGridLayout* newItem=static_cast<QGridLayout*>(item);
newItem->addWidget(object2Add,newItem->count()-1,0);
return true;
}

}
return false;

}
QSize scroll::sizeHint()
{
return QSize(100,80);
}



#include <QtGui>
#include <QApplication>
#include "scroll.h"
QVBoxLayout* collection::mainLayout=new QVBoxLayout;

int main(int argc, char *argv[])
{

QApplication a(argc, argv);
QWidget windows;
scroll test(&windows);
QVBoxLayout layout;
test.addCollection("salam2","2");
test.addCollection("salam3","2");
test.addCollection("salam2","1");
test.addCollection("salam4","3");
test.addCollection("salam5","5");
test.addCollection("salam2","2");
test.addCollection("salam3","2");
test.addCollection("salam2","1");
test.addCollection("salam4","3");
test.addCollection("salam5","5");
test.addCollection("salam2","1");
test.addCollection("salam4","3");
test.addCollection("salam5","5");


windows.setLayout(&layout);
windows.show();

return a.exec();
}


now the problem is that when i add collections in scroll constructor everything is fine like
following which i have used my custom scroll widget alone with 2 other widgets:
http://i35.tinypic.com/33oguad.png
but when i add collections using add collection method which performs the same job,i have sizing issues like following:
http://i36.tinypic.com/2u6i6w3.jpg
the picture above shows my widget when some of the collections are added in scroll widget constructor and some of them are added in main.cpp using addCollection method,
if I add all collections in main then i would have NO collections being seen at all(i think they get zero size so they are not shown) i have played with sizepolicies and minimumSize methods but no luck,I'm Stuck,please help

caduel
28th November 2008, 08:33
i) not related to your problem, but: a global returnMainLayout() is a bad idea: what if you want two instances of that widget?

ii) you should not set the maximumSize of your widget.
now, the available space is quite limited and the layout tries to fit all the widgets into that (too small) space.

sepehr
28th November 2008, 11:42
i) not related to your problem, but: a global returnMainLayout() is a bad idea: what if you want two instances of that widget?

ii) you should not set the maximumSize of your widget.
now, the available space is quite limited and the layout tries to fit all the widgets into that (too small) space.

thanks for reply,since other objects like buttons or textbox and .. can be added to a collection, I want to have a scrolling widget so it won't take alot of space

any ideas how to approach that?