PDA

View Full Version : size issues for custom QWidget in QScrollArea



anotheruser
27th April 2006, 14:17
Hello,

I created a custom QWidget (analogous to the CharacterMap example) with the following header file

class PatchWidget : public QWidget
{
Q_OBJECT

public:
PatchWidget(QWidget *parent = 0);
QSize sizeHint() const;

protected:
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);

private:

bool d_isSelected;
int d_index;

QList <QPixmap> d_patches;

unsigned int d_cols;
unsigned int d_rows;
unsigned int d_patch_size;
unsigned int d_border_size;
unsigned int d_total_size;

};

In the constructor I make some dummy patches/thumbnails, which are shown correctly on the screen.

PatchWidget::PatchWidget(QWidget *parent)
: QWidget(parent)
{
d_isSelected = false;
setMouseTracking(true);

d_cols = 5;
d_rows = 2;
d_patch_size = 64;
d_border_size = 2;
d_total_size = d_patch_size + 2*d_border_size;

for (unsigned int i=0;i<d_cols*d_rows;i++)
{
QPixmap tmp_pixmap = QPixmap(d_patch_size,d_patch_size);
tmp_pixmap.fill(Qt::white);//QPixmap::fromImage(image);
QPainter painter(&tmp_pixmap);
painter.drawRect(i,i,5,5);
d_patches.push_back(tmp_pixmap);
}
}


In the mousePressEvent function I add some patches to the d_patches list:

void CharacterWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
d_isSelected = true;
d_index = (event->y()/(d_total_size))*d_cols + event->x()/(d_total_size);
if (QChar(d_index).category() != QChar::NoCategory)
{
emit characterSelected(QString(QChar(d_index)));
}
// add some patches for testing
if (d_index == 4)
{
d_cols += 1;
d_patches.clear();
for (unsigned int i=0;i<d_cols*d_rows;i++)
{
QPixmap tmp_pixmap = QPixmap(d_patch_size,d_patch_size);
tmp_pixmap.fill(Qt::white);//QPixmap::fromImage(image);
QPainter painter(&tmp_pixmap);
painter.drawRect(i,i,5,5);
d_patches.push_back(tmp_pixmap);
}
}
update();
}
else {
d_isSelected = false;
QWidget::mousePressEvent(event);
}
}


The problem is now, that the new patches are not shown on screen. The width remains the same and only 2*5 patches are shown, although d_cols should contain 6 instead of 5 images. I know that the list is correctly updated and shown, but something goes wrong updating the size.

This widget is used in a QScrollArea and I think there lies the problem. Is there some way to explicitly transfer the new size to the scrollArea? I thought sizeHint (minimumSizeHint, etc.) should do the trick...

Thanks in advance!

anotheruser
27th April 2006, 14:52
I found the answer!

add adjustSize() after rows or columns have changed...