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. QGridLayout itemAtPosition with multiple items in a cell
Forum Updated to NodeBB v4.3 + New Features

QGridLayout itemAtPosition with multiple items in a cell

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 667 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.
  • S Offline
    S Offline
    swurl
    wrote on last edited by swurl
    #1

    Per a suggestion I found on another forum, I implemented grid lines with a custom QWidget with a paintEvent that draws the grid lines for me based on its max rows/cols. I then added it as such:

    m_layout->addWidget(m_gridLine, 0, 0, -1, -1);
    

    This works great, and grid lines are displayed perfectly. However, my application also depends upon the use of the itemAtPosition function within QGridLayout, in my case used to see if a widget is already occupying a space when the user tries to add it there:

    for (int i = 0; i < data.rowSpan; ++i) {
        for (int j = 0; j < data.colSpan; ++j) {
            int row = data.row + i;
            int col = data.col + j;
            QLayoutItem *item = m_layout->itemAtPosition(row, col);
            if (item && item->widget() != m_gridLine) return true;
        }
    }
    

    with data being a struct containing {row, col, rowSpan, colSpan}

    The issue I'm facing, however, is that no matter what, itemAtPosition always returns the grid line widget. This is in spite of me calling lower() on the grid line widget after each widget is added (which is a user-expedited runtime process).

    Thus, essentially what I'm looking for is a way to "prioritize" certain widgets and "deprioritize" others, so that QGridLayout::itemAtPosition will always return the item e.g. at the top.

    Pl45m4P 1 Reply Last reply
    0
    • S swurl

      Per a suggestion I found on another forum, I implemented grid lines with a custom QWidget with a paintEvent that draws the grid lines for me based on its max rows/cols. I then added it as such:

      m_layout->addWidget(m_gridLine, 0, 0, -1, -1);
      

      This works great, and grid lines are displayed perfectly. However, my application also depends upon the use of the itemAtPosition function within QGridLayout, in my case used to see if a widget is already occupying a space when the user tries to add it there:

      for (int i = 0; i < data.rowSpan; ++i) {
          for (int j = 0; j < data.colSpan; ++j) {
              int row = data.row + i;
              int col = data.col + j;
              QLayoutItem *item = m_layout->itemAtPosition(row, col);
              if (item && item->widget() != m_gridLine) return true;
          }
      }
      

      with data being a struct containing {row, col, rowSpan, colSpan}

      The issue I'm facing, however, is that no matter what, itemAtPosition always returns the grid line widget. This is in spite of me calling lower() on the grid line widget after each widget is added (which is a user-expedited runtime process).

      Thus, essentially what I'm looking for is a way to "prioritize" certain widgets and "deprioritize" others, so that QGridLayout::itemAtPosition will always return the item e.g. at the top.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by
      #2

      @swurl

      What you could do to verify your input and to get what you expect later, is testing your "data" with:

      m_layout->getItemPosition(dataIndex, int *row, int *column, int *rowSpan, int *columnSpan)

      • https://doc.qt.io/qt-6/qgridlayout.html#getItemPosition

      Therefore you only have to know the layout index of your data item and then you might be able to receive the valid input.

      This is in spite of me calling lower() on the grid line widget after each widget is added (which is a user-expedited runtime process).

      I dont think this will change something, since all widgets are in the QGridLayout and they all have their own cell and index.

      • https://doc.qt.io/qt-6/qwidget.html#lower

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      S 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @swurl

        What you could do to verify your input and to get what you expect later, is testing your "data" with:

        m_layout->getItemPosition(dataIndex, int *row, int *column, int *rowSpan, int *columnSpan)

        • https://doc.qt.io/qt-6/qgridlayout.html#getItemPosition

        Therefore you only have to know the layout index of your data item and then you might be able to receive the valid input.

        This is in spite of me calling lower() on the grid line widget after each widget is added (which is a user-expedited runtime process).

        I dont think this will change something, since all widgets are in the QGridLayout and they all have their own cell and index.

        • https://doc.qt.io/qt-6/qwidget.html#lower
        S Offline
        S Offline
        swurl
        wrote on last edited by
        #3

        @Pl45m4 Indeed, this worked.

        Another issue: the grid lines seem to still somehow be inconsistent with the widgets depending on how they're placed. Is there any better way to implement basic gridlines?

        1 Reply Last reply
        0
        • S swurl has marked this topic as solved on

        • Login

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