c++ - QTableWidget、Cellwidget、QLabel

标签 c++ qt qt5 qtablewidget qlabel

我已经创建了一个 QTableWidget。一些单元格填充有单元格小部件(修改后的 QLabel,发送点击信号)。 现在,我想对点击这个标签使用react。我添加了一些调试功能。

单击空单元格会将正确的行和列写入控制台。 单击标签被识别为单击,但行和列错误(使用之前的单元格数据)。

问题:如何获得点击标签的正确行和列。

问候和感谢,迈克尔

MtTimeTable::MtTimeTable( QWidget* parent )
    : QTableWidget { parent }, mIdArray { MtIdArray() },
      mDate { QDate::currentDate() }
{
    this->fillLesssonWidgets();

    connect( this, &MtTimeTable::cellClicked,
             this, &MtTimeTable::slotCellActivated );
}

void MtTimeTable::fillLessonWidgets()
{
    MtLessonVec lessonVec { true }; // Data to show, loaded from file

    auto cit { lessonVec.begin() };
    while( cit != lessonVec.end() ) {
        // Class MtLessonWidget derived from QLabel
        MtLessonWidget* lessonWidget { new MtLessonWidget };
        lessonWidget->setLesson( *cit );
        // Using member functions of MtLessonwidget, working correct
        this->setCellWidget( lessonWidget->startingRow(), 
                             lessonWidget->startingCol(), 
                             lessonWidget );

        }

        connect( lessonWidget, &MtLessonWidget::sigClicked,
                 this, &MtTimeTable::slotLessonWidgetClicked );

        ++cit;
    }

}

我试图将代码减少到最少。

void MtTimeTable::slotLessonWidgetClicked()
 {        
    std::cerr << "Table Cell: [" << this->currentRow() << "," 
          << this->currentColumn() << "]" << std::endl;
}

最佳答案

根据docs :

int QTableWidget::currentColumn() const

Returns the column of the current item.

int QTableWidget::currentRow() const

Returns the row of the current item.

也就是item的位置,它指的是一个QTableWidgetItem,但是如果我们用setCellWidget没有创建一个item,所以那些位置不合适,我们必须寻找其他方法来获取行和与小部件关联的列。

一种方法是使用 indexAt()返回与单元格关联的 QModelIndex 的方法,给定它相对于 QTableWidgetviewport() 的位置,而这应该是使用:

void MtTimeTable::slotLessonWidgetClicked(){
    auto lbl = qobject_cast<MtLessonWidget *>(sender());
    if(lbl){
       auto ix = indexAt(lbl->pos());
       qDebug()<<"Table Cell: [" <<ix.row()<< ","  <<ix.column()<< "]";
    }
}

完整示例:

#include <QApplication>
#include <QLabel>
#include <QTableWidget>
#include <QDebug>

class CustomLabel: public QLabel{
    Q_OBJECT
protected:
    void mousePressEvent(QMouseEvent *){
        emit clicked();
    }
signals:
    void clicked();
};

class TableWidget: public QTableWidget{
    Q_OBJECT
public:
    TableWidget(QWidget* parent=Q_NULLPTR):QTableWidget(parent){
        setRowCount(10);
        setColumnCount(10);
        for(int i=0; i<rowCount(); i++){
            for(int j=0; j<columnCount(); j++){
                auto lbl = new CustomLabel;
                setCellWidget(i, j, lbl);
                connect(lbl, &CustomLabel::clicked, this, &TableWidget::onClicked);
            }
        }
    }
private slots:
    void onClicked(){
        auto lbl = qobject_cast<CustomLabel *>(sender());
        if(lbl){
           auto ix = indexAt(lbl->pos());
           qDebug()<<"Table Cell: [" <<ix.row()<< ","  <<ix.column()<< "]";
        }
    }
};

#include "main.moc"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    TableWidget w;
    w.show();

    return a.exec();
}

在下面的链接中,我展示了完整的 example .

关于c++ - QTableWidget、Cellwidget、QLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47984647/

相关文章:

c++ - 使用来自另一个命名空间 C++ 的函数

c++ - extern是否破坏封装

qt - 如何在 Qt 中向上移动选定的行

c++ - 使用 Visual Studio 构建 MFC C++ 项目时出现 MBCS 错误

c++ - 简单的 CUDA vector 搜索/索引程序无法正常工作

c++ - 使用 fstream 在 C++ 中读取和写入二进制文件

c++ - Qt QMetaObjects 和从 QWidget 到 QObject 的转换

c++ - Qt Blocking Master 示例中 Mutex 的使用

c++ - 触发时无法将 QAction 链接到函数 (Qt 5)

c++ - 如何更改 QProgressBar 的状态?