Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QLabel and QScrollArea problem

[SOLVED] QLabel and QScrollArea problem

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Gladkov-Art
    wrote on last edited by
    #1

    Hello, I'm new in Qt and even in C++, I've got an issue, hope you can help me.
    I'm trying to put some pictures to the QScrollArea, as shown on the pictured below.
    !http://www.imagebam.com/image/78f76c334011078()!
    I've tried some like this
    @void MainWindow::on_actionOpen_triggered()
    {
    QFileDialog open(this);
    open.setFileMode(QFileDialog::Directory);
    open.setViewMode(QFileDialog::List);
    QStringList fileNames;
    if (open.exec()) fileNames = open.selectedFiles();
    QVBoxLayout labelLayout = new QVBoxLayout(ui->scrollArea);
    QDir dir(fileNames[0]);
    dir.setNameFilters(QStringList() <<"
    .jpg");
    QStringList listOfImages = dir.entryList();
    QLabel *label;
    QPixmap *pic;
    int x = 20;
    int y = 20;
    for (int i=0; i < listOfImages.size();i++)
    {
    pic = new QPixmap(fileNames[0]+ "/" + listOfImages[i]);
    label = new QLabel(this);
    labelLayout->addWidget(label);
    label->setGeometry(QRect(
    QPoint(x,y),
    QSize(45,60)));
    // label->setFrameRect(QRect(
    // QPoint(x,y),
    // QSize(45,60)));
    label->setPixmap(*pic);

        y += -60;
    
    }@
    

    and I managed to get only this result =((
    !http://www.imagebam.com/image/1794ba334011481()!
    Tell me please, what I've made wrong.
    Then I will try to implement pressing on any picture in QScrollArea and opening it in the QTabWidget ( on the right of QScrollArea). Maybe I shouldn't use QLabels for picture's preview? Maybe something like buttons with pictures will be better?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      setGeometry() doesn't work inside a layout, because it's layout's responsibility to place it at the right posiition and shape it to fit (as it does in your case).
      The correct way to set fixed size of the label is to use setFixedSize().

      You also have a memory leak - the label doesn't take ownership of the pixmap you pass to it, so the memory is never released. You should rather create the pixmap on the stack:
      @
      QPixmap pic(fileNames[0]+ "/" + listOfImages[i]);
      label->setPixmap(pic);
      //or just
      label->setPixmap(QPixmap(fileNames[0]+ "/" + listOfImages[i]));
      @

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Gladkov-Art
        wrote on last edited by
        #3

        Thanks for you help.
        I used setFixedSize for my pictures. Then put them on the QVBoxLayout,
        and addSpacing(20) between QLabels. But QScrollArea resizes the QVBoxLayout according to the self size, and all pictures inside layout are resize respectively. QScollArea insted of creating a vertical scroll bar resizes content.
        I feel that I missed something, some setting for scroll area, but can't find what exactly.

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Oh yeah, I haven't noticed it before. You shouldn't put a layout on the scroll area itself or just make a layout with scroll area as a parent.
          You should put the layout on some container widget and put that into the scroll area eg.
          @
          ui->scrollArea()->setWidget(new QWidget());
          ui->scrollArea()->widget()->setLayout(new QVBoxLayout());

          ...
          ui->scrollArea()->widget()->layout()->addWidget(...);
          @

          1 Reply Last reply
          0
          • G Offline
            G Offline
            Gladkov-Art
            wrote on last edited by
            #5

            Thanks you very much!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved