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. QScrollArea with QGridLayout
QtWS25 Last Chance

QScrollArea with QGridLayout

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 20.5k Views
  • 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.
  • C Offline
    C Offline
    CrimsonGT
    wrote on last edited by
    #1

    I currently have a QGridLayout that is used to show a table of data, but I would only like to show around 3x5 cells, and allow for the rest of them to be scrolled to as needed. After reading up on using QScrollArea with QGridLayout, I ended up with this...
    @
    QScrollArea *scrollArea = new QScrollArea();
    QWidget *container = new QWidget(scrollArea);
    QGridLayout *gridEPG = new QGridLayout(container);
    scrollArea->setWidget(container);
    ui->gridLayout->addWidget(w, 1, 0, 1, 3);
    //ui->gridLayout->addLayout(gridEPG, 1, 0, 1, 3);
    @
    It displays fine, but as the scrolling doesn't work. As I add/remove elements from the "gridEPG" layout, it simply shrinks/expands everything to fit. What changes need to be set so that I can actually make it display X by X cells and scroll to the rest?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      You need to apply the layout (QWidget::setLayout()) to the container widget and give it some definite size. It look like you are trying to do a programme guide so you might set the width of container to 24x pixels to give yourself x pixels per hour.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        CrimsonGT
        wrote on last edited by
        #3

        Hi Chris,

        Thank you so much. I realized after you pointed that out that I was also still assigning the container widget to my main form's grid layout. Once I set the layout as you mentioned on my container widget and instead assigned the scroll area to my main form layout (instead of the container) it worked perfectly.

        I am still a bit confused on the sizing. As my EPG grid (and scroll area) are part of a larger grid that resize it when the window is resized, I can't set a static size for it. I would however like to set it so no matter what the window is resized to, it always shows the 3 columns and 5 rows, just in different sizes, then scrolling simply slides over by 1 column or 1 row. How would that be done?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          The QScrollArea is resized by the layout you insert it into. The widget inside the QScrollArea has a size independent of the QScrollArea's size. When that size is larger than the viewport you get scroll bars, otherwise you do not.

          @
          #include <QtGui>
          #include <QDebug>

          class MainWidget: public QWidget {
          Q_OBJECT
          public:
          MainWidget(QWidget *p = 0): QWidget(p) {
          // Fake up a grid to scroll
          QWidget *client = new QWidget(this);
          QGridLayout *gl = new QGridLayout(client);
          for (int row = 0; row < 5; ++row) {
          for (int col = 0; col < 24; ++col) {
          QLabel *l = new QLabel(QString("R%1 C%2").arg(row).arg(col), client);
          l->setFixedSize(100, 40); // makes the whole client widget abut 2400 px wide
          gl->addWidget(l, row, col);
          }
          }
          client->setLayout(gl);

              // Put it into a scroll area
              QScrollArea *area = new QScrollArea(this);
              area->setWidget(client);
          
              // Make the scroll step the same width as the fixed widgets in the grid
              area->horizontalScrollBar()->setSingleStep(client->width() / 24);
          
              QVBoxLayout *layout = new QVBoxLayout(this);
              layout->addWidget(new QLabel("Something else", this));
              layout->addWidget(area);
          
              setLayout(layout);
          }
          

          public slots:
          private:
          };

          int main(int argc, char *argv[])
          {
          QApplication app(argc, argv);

          MainWidget m;
          m.show();
          return app.exec&#40;&#41;;
          

          }
          #include "main.moc"
          @

          1 Reply Last reply
          0
          • C Offline
            C Offline
            CrimsonGT
            wrote on last edited by
            #5

            Thanks for the code snippet Chris. I posted my own here http://qt-project.org/forums/viewthread/20924/ (made a new thread since it was pretty massive) and I am still at a loss. When I made a new project and tried your project it was all jumbled up in the top left corner.

            My code snippet and screenshot show the issue I am having, but still just not sure where its wrong. I noticed your code is a little different from mine in terms of how it is ordered, but everything is being done the same way as far as I can tell. I just can't figure out why mine is causing the columns to overlap each other rather than just spreading out and making horizontal scroll bars.

            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