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. How to get clicked item in QGridLayout?

How to get clicked item in QGridLayout?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 8.0k 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.
  • S Offline
    S Offline
    ShinSat
    wrote on last edited by
    #1

    Hi Gurus,

    Does anybody tell me how to get clicked item(cell) in QGridLayout?

    Thanks in advance for your help. :)
    Regards,
    Sat

    T 1 Reply Last reply
    0
    • S ShinSat

      Hi Gurus,

      Does anybody tell me how to get clicked item(cell) in QGridLayout?

      Thanks in advance for your help. :)
      Regards,
      Sat

      T Offline
      T Offline
      Tirupathi Korla
      wrote on last edited by Tirupathi Korla
      #2

      @ShinSat
      gridLayout.indexOf(QWidget_Clickded_item) will give index of clicked item..

      1 Reply Last reply
      1
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by Paul Colby
        #3

        Just expanding on what @ShinSat said, with a contrived working example going from a mouse click to a grid layout item:

        MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags)
        {
           QGridLayout * const grid = new QGridLayout();
           for (int row = 0; row < 5; ++row)
              for (int col = 0; col < 5; ++col)
                  grid->addWidget(new QLabel(tr("%1:%2").arg(row).arg(col)), row, col);
            setCentralWidget(new QWidget);
            centralWidget()->setLayout(grid);
        }
        
        void MainWindow::mousePressEvent(QMouseEvent *event)
        {
            QWidget * const widget = childAt(event->pos());
            qDebug() << "child widget" << widget;
            if (widget) {
                const QLabel * const label = qobject_cast<QLabel *>(widget);
                if (label) {
                    qDebug() << "label" << label->text();
                }
        
                const int index = centralWidget()->layout()->indexOf(widget);
                qDebug() << "layout index" << index;
                if (index >=0) {
                    const QLayoutItem * const item = centralWidget()->layout()->itemAt(index);
                    qDebug() << "layout item" << item;
                }
            }
        }
        

        Cheers.

        1 Reply Last reply
        6
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          If you need something like this I automatically suspect you are abusing QGridLayout and you really need a QTableView/QTableWidget or even a QGraphicsView or you should really reconsider your design

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          S 1 Reply Last reply
          3
          • VRoninV VRonin

            If you need something like this I automatically suspect you are abusing QGridLayout and you really need a QTableView/QTableWidget or even a QGraphicsView or you should really reconsider your design

            S Offline
            S Offline
            ShinSat
            wrote on last edited by ShinSat
            #5

            @VRonin said in How to get clicked item in QGridLayout?:

            you really need a QTableView/QTableWidget or even a QGraphicsView

            Well, I'm still considering the pros and cons between QGridLayout and TableView.
            Can anybody share main difference(advantages/disadvantages) between them?

            What I'm trying is exactly same as Ms-Excell where multiple tables linking together including sorting capability.
            Some widgets such as QComboBox and QLineEdit are on the tables.

            Regards,
            Sat

            VRoninV 1 Reply Last reply
            0
            • S ShinSat

              @VRonin said in How to get clicked item in QGridLayout?:

              you really need a QTableView/QTableWidget or even a QGraphicsView

              Well, I'm still considering the pros and cons between QGridLayout and TableView.
              Can anybody share main difference(advantages/disadvantages) between them?

              What I'm trying is exactly same as Ms-Excell where multiple tables linking together including sorting capability.
              Some widgets such as QComboBox and QLineEdit are on the tables.

              Regards,
              Sat

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @ShinSat said in How to get clicked item in QGridLayout?:

              What I'm trying is exactly same as Ms-Excell where multiple tables linking together including sorting capability.

              The QTableView + Custom model is 100% the way to go. This is one of the rare cases I suggest a custom model from the start as Qt's models treat Qt::EditRole and Qt::DisplayRole (the formula and the value in Excel) as the same. You can probably get away with QStandardItemModel + a custom proxy that redirects Qt::EditRole to a user role though

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              S 1 Reply Last reply
              2
              • VRoninV VRonin

                @ShinSat said in How to get clicked item in QGridLayout?:

                What I'm trying is exactly same as Ms-Excell where multiple tables linking together including sorting capability.

                The QTableView + Custom model is 100% the way to go. This is one of the rare cases I suggest a custom model from the start as Qt's models treat Qt::EditRole and Qt::DisplayRole (the formula and the value in Excel) as the same. You can probably get away with QStandardItemModel + a custom proxy that redirects Qt::EditRole to a user role though

                S Offline
                S Offline
                ShinSat
                wrote on last edited by
                #7

                @VRonin Thanks a lot for sharing informative thoughts. :)
                Do you have any thoughts on performance between QGridLayout and QTableView?

                I'm currently testing QGridLayout with 200 * 20 matrix and feeling initialization(creating widgets and adding it(addWidget) on the layout) is very slow. :<

                Sat

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  You are doing the equivalent of digging a ditch using a spoon. A spoon is great to eat soup but if you think of using it instead of a spade you are going to have a hard time

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  S 1 Reply Last reply
                  3
                  • VRoninV VRonin

                    You are doing the equivalent of digging a ditch using a spoon. A spoon is great to eat soup but if you think of using it instead of a spade you are going to have a hard time

                    S Offline
                    S Offline
                    ShinSat
                    wrote on last edited by
                    #9

                    @VRonin said in How to get clicked item in QGridLayout?:

                    You are doing the equivalent of digging a ditch using a spoon.

                    Hahaha,,, you are right. :)
                    But I need to find a solution to that using Qt. (I'm using pyqt, though)

                    Sat

                    S 1 Reply Last reply
                    0
                    • S ShinSat

                      @VRonin said in How to get clicked item in QGridLayout?:

                      You are doing the equivalent of digging a ditch using a spoon.

                      Hahaha,,, you are right. :)
                      But I need to find a solution to that using Qt. (I'm using pyqt, though)

                      Sat

                      S Offline
                      S Offline
                      ShinSat
                      wrote on last edited by
                      #10

                      @ShinSat Designing well-formed integrated view from multiple data sets is the most challenging part of mine.
                      To simplify complex data into Qt-friendly data model is headache. :<
                      I think every data item will need to be saved together with its position(row, column). This looks too much but necessary, I guess.

                      Sat

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        ShinSat
                        wrote on last edited by
                        #11

                        Thank you very much for your help, all!
                        I gave up QGridLayout and now working with QTableView/QAbstractTableModel.
                        It's working well up to now. :)

                        Regards,
                        Sat

                        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