Results 1 to 8 of 8

Thread: QScrollArea

  1. #1
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QScrollArea

    Hi
    I have a cod like this,

    QIcon icon;
    QStringList files;
    QStringList filters;
    QString fileName;
    int r, c;
    filters << "snapshot-*" ;
    QDir directory;
    directory.setNameFilters(filters);
    files = directory.entryList(filters, QDir::Files | QDir::NoSymLinks);
    NumStates_New=0;
    NumStates_New = files.size()/3;
    if ( files.size()%3 >0 ) NumStates_New++;
    QLabel *pixmapLabels [NumStates_New] [3];
    QScrollArea *scrollArea = new QScrollArea ( SnapWindow );
    scrollArea->setBackgroundRole(QPalette:ark);
    scrollArea->setGeometry(5,5, 340, 550);

    SnapLayout = new QGridLayout(scrollArea);
    for (int i = 0; i < NumStates_New; ++i) {
    for (int j = 0; j < 3; ++j) {
    pixmapLabels[i][j] = createPixmapLabel();
    SnapLayout->addWidget(pixmapLabels[i][j], i + 1, j + 1 );
    } }

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(SnapWindow);
    r=0, c=0;
    for (int i = 0; i < files.size(); ++i) {
    fileName = files.at(i).toLocal8Bit().constData();
    QImage image(fileName);
    if (!image.isNull())
    icon.addPixmap(QPixmap::fromImage(image), QIcon::Active, QIcon::On);
    this->icon = icon;
    this->size = QSize(128,128);
    QPixmap pixmap = icon.pixmap(size, QIcon::Active, QIcon::On);
    pixmapLabels[r][c]->setPixmap(pixmap);
    c++;
    if (c>=3) {
    c=0; r++;
    } }


    It display all the pictures in a widget. I need a scrolbar for the QScrollArea. In this code the scroll bar not display, and all pictures shrink to that QScrollArea. Please help me for adding a scrollbar in this code.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollArea

    Install "SnapLayout" on plain QWidget, and set it inside the scroll are via QScrollArea::setWidget() after you have fully constructed the layout.
    J-P Nurmi

  3. #3
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea

    Hi,
    I try it like this,
    SnapLayout = new QGridLayout(SnapWindow);
    QWidget *ScrAr = new QWidget(SnapLayout);

    but it display some errors like this

    error: no matching function for call to ‘QWidget::QWidget(QGridLayout*&)’
    /usr/local/Trolltech/Qt-4.3.0/include/QtGui/qwidget.h:702: note: candidates are: QWidget::QWidget(const QWidget&)
    /usr/local/Trolltech/Qt-4.3.0/include/QtGui/qwidget.h:647: note: QWidget::QWidget(QWidgetPrivate&, QWidget*, Qt::WindowFlags)
    /usr/local/Trolltech/Qt-4.3.0/include/QtGui/qwidget.h:199: note: QWidget::QWidget(QWidget*, const char*, Qt::WindowFlags)
    /usr/local/Trolltech/Qt-4.3.0/include/QtGui/qwidget.h:197: note: QWidget::QWidget(QWidget*, Qt::WindowFlags)


    Please help me to solve this prons.

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollArea

    Qt Code:
    1. QWidget *widget = new QWidget(scrollArea);
    2. SnapLayout = new QGridLayout(widget);
    3. // ...after you have added labels and set images
    4. scrollArea->setWidget(widget);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea

    Hi,
    I modify my code like this, but now it doesn't display anything.
    Please help me

    Camframe4 = new QFrame(this);
    Camframe4->setObjectName(QString::fromUtf8("Camframe4"));
    Camframe4->setGeometry(QRect(368, 10, 365, 310));
    Camframe4->setFrameShape(QFrame::StyledPanel);
    Camframe4->setFrameShadow(QFrame::Raised);
    QLabel *pixmapLabels [4] [3];
    QScrollArea *scrollArea = new QScrollArea ( Camframe4 );
    QWidget *widget = new QWidget(scrollArea);
    SnapLayout = new QGridLayout(widget);

    for (int i = 0; i < 4; ++i) {
    for (int j = 0; j < 3; ++j) {
    pixmapLabels[i][j] = createPixmapLabel();
    SnapLayout->addWidget(pixmapLabels[i][j], i + 1, j + 1 );
    }
    }

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(SnapWindow);

    r=0, c=0;
    QPixmap pixmap ;
    for (int i = 0; i < files.size(); ++i) {
    fileName = files.at(i).toLocal8Bit().constData();

    QImage image(fileName);
    if (!image.isNull())
    icon.addPixmap(QPixmap::fromImage(image), QIcon::Normal, QIcon::On);

    this->icon = icon;
    this->size = QSize(100,100);
    QPixmap pixmap = icon.pixmap(size, QIcon::Normal, QIcon::On);
    pixmapLabels[r][c]->setPixmap(pixmap);
    c++;
    if (c>=3) {
    c=0; r++;
    }
    }

    scrollArea->setWidget(widget);

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QScrollArea

    Are scrollArea and Camframe4 in layouts?
    J-P Nurmi

  7. #7
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea

    Hi,
    I have a frame named as Camframe4 and I need to display the phots in that frame in a ScrollArea.

  8. #8
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QScrollArea

    Hi,
    I again modify that code like this,
    I create a class like this

    ImageViewer.h
    -----------------------
    class CImageView : public QWidget
    {
    Q_OBJECT
    private:
    enum { NumModes = 4, NumStates = 2 };
    QStringList files;
    QStringList filters;
    QString fileName;
    QDir directory;
    QPixmap pixmap ;
    int r, c, NumStates_New;
    QLabel *pixmapLabels[NumModes][NumStates];
    QGridLayout *SnapLayout;
    QIcon icon;
    QSize size;
    public:
    CImageView(QWidget *parent = 0);
    void LoadImages();
    void ShowImages();
    QLabel *createPixmapLabel();
    };

    ----------------------------------------------------------
    ImageViewer.cpp
    ----------------------------------

    #include "ImageView.h"
    CImageView::CImageView(QWidget *parent) : QWidget(parent)
    {
    }
    void CImageView::LoadImages(){

    filters << "snapshot-*" ;
    directory.setNameFilters(filters);
    files = directory.entryList(filters, QDir::Files | QDir::NoSymLinks);
    NumStates_New=0;
    NumStates_New = files.size()/3;
    SnapLayout = new QGridLayout(this);

    if ( files.size()%3 >0 ) NumStates_New++;

    QLabel *pixmapLabels [NumStates_New] [3];

    for (int i = 0; i < NumStates_New; ++i) {
    for (int j = 0; j < 3; ++j) {
    pixmapLabels[i][j] = createPixmapLabel();
    SnapLayout->addWidget(pixmapLabels[i][j], i + 1, j + 1 );
    }
    }

    r=0, c=0;
    for (int i = 0; i < files.size(); ++i) {
    fileName = files.at(i).toLocal8Bit().constData();
    QImage image(fileName);
    if (!image.isNull())
    icon.addPixmap(QPixmap::fromImage(image), QIcon::Normal, QIcon::On);
    this->icon = icon;
    this->size = QSize(100,100);
    QPixmap pixmap = icon.pixmap(size, QIcon::Normal, QIcon::On);
    pixmapLabels[r][c]->setPixmap(pixmap);
    c++; if (c>=3) { c=0; r++; }
    }
    }

    void CImageView::ShowImages(){

    }

    QLabel *CImageView::createPixmapLabel()
    {
    QLabel *label = new QLabel;
    label->setEnabled(true);
    label->setAlignment(Qt::AlignCenter);
    label->setFrameShape(QFrame::Box);
    label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    label->setBackgroundRole(QPalette::Base);
    label->setAutoFillBackground(true);
    label->setMinimumSize(100, 13);
    return label;
    }

    -----------------------------------------------

    And in my program I call this class like this,

    CImageView *ImageWid = new CImageView (this);
    ImageWid->LoadImages();
    ImageWid->setGeometry(QRect(375, 10, 345, 520));

    Then it display all the pictures, but I can't see some pictures due to the window's size is smaller than the "ImageWid". So I need to add a scrollbar. I use QScrollArea like this,

    QScrollArea *scrollArea = new QScrollArea;
    QWidget *widget = new QWidget(scrollArea);
    widget->setGeometry(QRect(375, 10, 345, 520));
    CImageView *ImageWid = new CImageView ;
    ImageWid->LoadImages();
    ImageWid->setGeometry(QRect(1, 1, 345, 520));
    scrollArea->setWidget(widget);

    then, it doesn't display any errors. But at runtime, it doesn't display any pictures.
    Please help me to solve this probs.

Similar Threads

  1. QScrollArea and resizing
    By rarefluid in forum Qt Programming
    Replies: 8
    Last Post: 22nd July 2007, 15:18
  2. QScrollArea With Custom Widgets
    By VireX in forum Qt Programming
    Replies: 30
    Last Post: 22nd April 2007, 17:48
  3. Replies: 2
    Last Post: 8th October 2006, 21:14
  4. Replies: 7
    Last Post: 20th September 2006, 15:45
  5. is there any signal for QScrollArea?
    By rajesh in forum Qt Programming
    Replies: 1
    Last Post: 21st July 2006, 08:12

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.