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>
{
private:
public:
};
#endif
#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
To copy to clipboard, switch view to plain text mode
#include "collection.h"
{
innerLayout.setProperty("name",collectionName);
triClick.setText(collectionName);
caption.setText(collectionCaption);
innerLayout.addWidget(&triClick,0,0);
innerLayout.addWidget(&caption,0,1);
mainLayout->addLayout(&innerLayout);
}
{
return mainLayout;
}
QSize collection
::sizeHint() {
}
#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);
}
To copy to clipboard, switch view to plain text mode
#ifndef scroll_class
#define scroll_class
#include "collection.h"
#include <QScrollArea>
{
public:
collection
* addCollection
(QString collectionName,
QString collectionLabel
);
};
#endif
#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
To copy to clipboard, switch view to plain text mode
#include "scroll.h"
#include <QDebug>
{
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");
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;
}
{
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);
newItem->addWidget(object2Add,newItem->count()-1,0);
return true;
}
}
return false;
}
{
}
#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);
}
To copy to clipboard, switch view to plain text mode
#include <QtGui>
#include <QApplication>
#include "scroll.h"
int main(int argc, char *argv[])
{
scroll test(&windows);
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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
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:
but when i add collections using add collection method which performs the same job,i have sizing issues like following:
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
Bookmarks