【问题标题】:How get coordinates of widget in qgrid layout in Qt如何在 Qt 的 qgrid 布局中获取小部件的坐标
【发布时间】:2013-06-08 23:47:07
【问题描述】:

我正在尝试设计一个带有充满 qpushbuttons 的网格布局的数独游戏,在将一个数字插入到 qpush 按钮中的那一刻,当点击它们时我需要检查它是否是有效的移动,具体取决于其他“细胞”。我的问题是我需要在 qgridlayout 中单击但无法找到如何操作的 qpushbutton 的行和列。有没有办法获取qgridlayout上点击的qpushbutton的坐标?

【问题讨论】:

    标签: qt qpushbutton qgridlayout


    【解决方案1】:

    http://doc.qt.io/qt-4.8/layout.html#horizontal-vertical-grid-and-form-layouts

    http://doc.qt.io/qt-4.8/qgridlayout.html#getItemPosition

    http://doc.qt.io/qt-4.8/qlayout.html#indexOf

    您的代码应该类似于:

    int index = my_grid_layout->indexOf(clicked_widget);
    
    if(index != -1)
    {
        int row, col, col_span, row_span;
        my_grid_layout->getItemPosition(index, &row, &col, &col_span, &row_span);
        qDebug() << "Clicked Item is at:" << row << col 
                 << "spanning" << row_span << col_span;
    }
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-06
      • 2011-03-05
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 2014-12-17
      • 1970-01-01
      • 2020-05-05
      相关资源
      最近更新 更多