How to get clicked item in QGridLayout?



  • Hi Gurus,

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

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



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



  • 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.



  • 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



  • @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



  • @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



  • @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



  • 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



  • @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



  • @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



  • 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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.