Results 1 to 2 of 2

Thread: size issues for custom QWidget in QScrollArea

  1. #1
    Join Date
    Apr 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default size issues for custom QWidget in QScrollArea

    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!

  2. #2
    Join Date
    Apr 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: size issues for custom QWidget in QScrollArea

    I found the answer!

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

Similar Threads

  1. Dynamic updates of a QWidget in a QScrollArea
    By plamkata in forum Qt Programming
    Replies: 2
    Last Post: 20th July 2008, 23:45
  2. QScrollArea problem positioning a QWidget inside
    By Spectator in forum Qt Programming
    Replies: 4
    Last Post: 20th February 2006, 22:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.