PDA

View Full Version : Help me about QGridLayout and ScrollArea



homerux
10th August 2013, 15:13
Please help me this problem.
I using QGridLayout and array of QToolButton to display a array of images (Image is the icon of button). But the button (image) overlap...
I want to display the array button in 3 columns....
help me!
Thanks!
this is my code



QDir currentImageDir(LibPath+"/"+dir);
currentImageDir.setFilter(QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot);
QStringList ImageEntries = currentImageDir.entryList();

singalImageMapper = new QSignalMapper(this);
QGridLayout* gridEPG;
QScrollArea *scrollArea = new QScrollArea();
QWidget *contentsWidget = new QWidget(scrollArea);
gridEPG = new QGridLayout(contentsWidget);

scrollArea->setWidgetResizable(true);
scrollArea->setWidget(contentsWidget);
contentsWidget->setLayout(gridEPG);
contentsWidget->setMinimumSize(scrollArea->width(), scrollArea->height());

ui->imageLayout->addWidget(scrollArea, 1, 0, 2, 3);
QString filename[ImageEntries.count()];

for (int x = 0; x < ImageEntries.count(); x++)
{
// int row = x;
//int col = i%3;
filename[x]= ImageEntries.at(x);
QToolButton* button = new QToolButton();
button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
button->setIcon(QIcon(LibPath + "/" + dir + "/" + filename[x]));
button->setFixedSize(QSize(96,76));
button->setIconSize(QSize(96,76));
connect(button, SIGNAL(clicked()), singalImageMapper, SLOT(map()));
singalImageMapper->setMapping(button, filename[x]);
for(int i=0; i<3; i++)
gridEPG->addWidget(button, x, i%3);

}
ui->fileImgUsing->setText(QString(imgPath + "/" + dir));
connect(singalImageMapper, SIGNAL(mapped(QString)),this, SIGNAL(clicked(QString)));
connect(this, SIGNAL(clicked(QString)),this, SLOT(onButtonClicked(QString)));


and my result

9399

ChrisW67
10th August 2013, 21:45
[QUOTE=homerux;248996]

for(int i=0; i<3; i++)
gridEPG->addWidget(button, x, i%3);


You are trying to put the same button object in each of three columns? If that is what you intend then you need to duplicate the button for each column (i.e. three different objects with the same image). If that is not what you intended then remove the for loop and change "i % 3" to " x % 3".

Set the sizeConstraint() of the layout in the scroll area contents widget to QLayout::SetFixedSize and it should adjust the size of the contentsWidget to match the fixed size of the contained tool buttons.

homerux
11th August 2013, 07:47
[QUOTE=homerux;248996]

for(int i=0; i<3; i++)
gridEPG->addWidget(button, x, i%3);


You are trying to put the same button object in each of three columns? If that is what you intend then you need to duplicate the button for each column (i.e. three different objects with the same image). If that is not what you intended then remove the for loop and change "i % 3" to " x % 3".

Set the sizeConstraint() of the layout in the scroll area contents widget to QLayout::SetFixedSize and it should adjust the size of the contentsWidget to match the fixed size of the contained tool buttons.

I want put a array of button in 3 columns in a scroll layout, but it not work. Can you help me?

ChrisW67
12th August 2013, 00:11
#include <QtGui>

class Window: public QScrollArea
{
Q_OBJECT
public:
explicit Window(QWidget *p = 0): QScrollArea(p) {
QWidget *contents = new QWidget(this);

QGridLayout *layout = new QGridLayout(this);
for (int n = 0; n < 150; ++n) {
QToolButton *b = new QToolButton(this);
b->setIcon(qApp->style()->standardIcon(QStyle::SP_FileIcon));
layout->addWidget(b, n / 3, n % 3);
}
contents->setLayout(layout);

setWidget(contents);
}
};

int main(int argc, char **argv)
{
QApplication app(argc, argv);
Window w;
w.resize(200, 200);
w.show();
return app.exec();
}
#include "main.moc"