在表格单元格中添加标签和按钮

目前我只会两种,貌似可以用delegate,但是还不晓得怎么用

方法1:如何只是为了用图标示意,然后点击该图标之后能够实现特定功能,那么可以使用QICon与itemClicked()。

/*********************************************************************/
// 构造函数
myTableWidget::myTableWidget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("pushButton delegate embedded in table"));
    setFixedSize(450, 250);
 
    
 
    QLabel* label = new QLabel(QObject::tr("table widget with icon."));
    QStringList headerLabels = QStringList()<<QObject::tr("nickname")
                                           <<QObject::tr("shortNum")
                                          <<QObject::tr("")
                                         <<QObject::tr("");
 
    m_tableWidget = new QTableWidget(4, 4, this);
    m_tableWidget->setHorizontalHeaderLabels(headerLabels);
 
    for(int i=0; i<4; ++i)
    {
        QTableWidgetItem* item0 = new QTableWidgetItem(QObject::tr("tom%1").arg(i));
        QTableWidgetItem* item1 = new QTableWidgetItem(QObject::tr("010%1").arg(i));
        QTableWidgetItem* item2 = new QTableWidgetItem(QIcon(QObject::tr(":/icons/dial.png")), QObject::tr("Dial"));
        QTableWidgetItem* item3 = new QTableWidgetItem(QIcon(QObject::tr(":/icons/delete.png")), QObject::tr(""));
 
        m_tableWidget->setItem(i, 0, item0);
        m_tableWidget->setItem(i, 1, item1);
        m_tableWidget->setItem(i, 2, item2);
        m_tableWidget->setItem(i, 3, item3);
    }
 
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(label);
    mainLayout->addWidget(m_tableWidget);
    connect(m_tableWidget, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(onItemClicked(QTableWidgetItem*)));
}

// 槽函数:
void myTableWidget::onItemClicked(QTableWidgetItem* item)
{
    int row = item->row();
    int colum = item->column();
    qDebug()<<tr("row: %1, colum: %2").arg(row).arg(colum);
    // 第三列:dial
    if(colum == 2)
    {
        qDebug() << QObject::tr("dial.");
    }
    // 第四列:remove
    if(colum == 3)
    {
        qDebug() << QObject::tr("remove row.");
        m_tableWidget->removeRow(row);
    }
}
如图:

/*********************************************************************/

方法2:如何执意要用QPushButton,那么可以在槽函数中通过获得信号发送者sender的坐标,得到被点击的button在表格中的index值,从而获得对应的行row与列colum:

myTableWidget::myTableWidget(QWidget *parent)
    : QWidget(parent)
{
    setWindowTitle(tr("pushButton delegate embedded in table"));
    setFixedSize(450, 250);
 
    
 
    QLabel* label = new QLabel(QObject::tr("table widget with icon."));
    QStringList headerLabels = QStringList()<<QObject::tr("nickname")
                                           <<QObject::tr("shortNum")
                                          <<QObject::tr("")
                                         <<QObject::tr("");
 
    m_tableWidget = new QTableWidget(4, 4, this);
    m_tableWidget->setHorizontalHeaderLabels(headerLabels);
 
    for(int i=0; i<4; ++i)
    {
        QTableWidgetItem* item0 = new QTableWidgetItem(QObject::tr("tom%1").arg(i));
        QTableWidgetItem* item1 = new QTableWidgetItem(QObject::tr("010%1").arg(i));
        QPushButton* dialBtn = new QPushButton;
        connect(dialBtn, SIGNAL(clicked()), this, SLOT(dial()));
        //connect(delBtn, SIGNAL(clicked()), this, SLOT(onBtnClicked()));
        dialBtn->setIcon(QIcon(QObject::tr(":/icons/dial.png")));
 
        QPushButton* delBtn = new QPushButton;
        connect(delBtn, SIGNAL(clicked()), this, SLOT(removeRow()));
        //connect(delBtn, SIGNAL(clicked()), this, SLOT(onBtnClicked()));
        delBtn->setIcon(QIcon(QObject::tr(":/icons/delete.png")));
 
        m_tableWidget->setItem(i, 0, item0);
        m_tableWidget->setItem(i, 1, item1);
        m_tableWidget->setCellWidget(i, 2, dialBtn);
        m_tableWidget->setCellWidget(i, 3, delBtn);
    }
 
    QVBoxLayout* mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(label);
    mainLayout->addWidget(m_tableWidget);
}
 
void myTableWidget::dial()
{
    QPushButton *pushBtn = dynamic_cast<QPushButton*>(this->sender());
    if(pushBtn == 0)
        return;
    int x = pushBtn->frameGeometry().x();
    int y = pushBtn->frameGeometry().y();
    QModelIndex index = m_tableWidget->indexAt(QPoint(x, y));
    int row = index.row();
    int colum = index.column();
    qDebug() << QObject::tr("Dial, row: %1, colum: %2").arg(row).arg(colum);
}
void myTableWidget::removeRow()
{
    QPushButton *pushBtn = dynamic_cast<QPushButton*>(this->sender());
    if(pushBtn == 0)
        return;
    int x = pushBtn->frameGeometry().x();
    int y = pushBtn->frameGeometry().y();
    QModelIndex index = m_tableWidget->indexAt(QPoint(x, y));
    int row = index.row();
    int colum = index.column();
    qDebug() << QObject::tr("Remove row, row: %1, colum: %2").arg(row).arg(colum);
    m_tableWidget->removeRow(row);
}
如下图:

 
也可以使用同一个槽函数:
void myTableWidget::onBtnClicked()
{
    QPushButton *pushBtn = dynamic_cast<QPushButton*>(this->sender());
    if(pushBtn == 0)
        return;
    int x = pushBtn->frameGeometry().x();
    int y = pushBtn->frameGeometry().y();
    QModelIndex index = m_tableWidget->indexAt(QPoint(x, y));
    int row = index.row();
    int colum = index.column();
    qDebug() << (QObject::tr("onBtnClicked,row: %1, colum: %2").arg(row).arg(colum));
    // dial
    if(colum == 2)
    {
        qDebug() << QObject::tr("Dial");
    }
    // remove
    else if(colum == 3)
    {
        qDebug() << QObject::tr("Remove row.");
        m_tableWidget->removeRow(row);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值