PDA

View Full Version : QListWidget problem [solved]



satoshi
4th May 2009, 18:43
Hi,
I've a problem with QListWidget class. In the following program, when trying to add the 5th image, it goes bottom (it is hidden). My idea is that the 5th and the following images go on the same row with an horizontal scrollbar. Note that if I add 4 images, then I enlarge the window, I add the 5th image and I restore the previous size, the scrollbar appears!
Can anyone help me solving the problem?

Thanks! Dario

(sorry for my bad english)


main.cpp:

#include "window.h"

#include <QtGui>
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
}


window.h:

#ifndef WINDOW_H
#define WINDOW_H

#include <QtGui/QMainWindow>

class QListWidget;

class Window : public QMainWindow
{
Q_OBJECT

public:
Window(QWidget *parent = 0);

private:
QListWidget *thumbsList;

private slots:
void addImage();
};

#endif // WINDOW_H


window.cpp

#include "window.h"

#include <QtGui>

Window::Window(QWidget *parent)
: QMainWindow(parent)
{
this->resize(640, 480);

QFrame *frame = new QFrame;
QVBoxLayout *frameLayout = new QVBoxLayout(frame);

this->thumbsList = new QListWidget(frame);
this->thumbsList->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored);
this->thumbsList->setFixedHeight(116);
this->thumbsList->setGridSize(QSize(126, 116));
this->thumbsList->setIconSize(QSize(96, 96));
this->thumbsList->setFlow(QListView::LeftToRight);
this->thumbsList->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff) ;
this->thumbsList->setViewMode(QListView::IconMode);

QPushButton *addImageButton = new QPushButton(tr("Add image"));
addImageButton->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
connect(addImageButton, SIGNAL(clicked()), this, SLOT(addImage()));

frameLayout->addWidget(addImageButton);
frameLayout->addWidget(this->thumbsList);
this->setCentralWidget(frame);
}

void Window::addImage()
{
QListWidgetItem *item = new QListWidgetItem(this->thumbsList);
item->setFlags(Qt::ItemIsEnabled);
item->setIcon(QIcon(QPixmap::fromImage(QImage("C:/image.jpg"))));
this->thumbsList->addItem(item);
}

wysota
4th May 2009, 19:48
Make sure the wrapping property of the list widget is set to false.

satoshi
4th May 2009, 19:58
Really, really thanks :D