如何显示在QTableWidget中单击QPushButton的行 [英] How to show the row where QPushButton is clicked in QTableWidget

查看:817
本文介绍了如何显示在QTableWidget中单击QPushButton的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除单击了QPushButton的行,我认为使用插槽是合理的,但是如何执行尚不知道,如果您有任何想法如何选择行按钮,请分享,谢谢.

I would like to delete row where QPushButton is clicked how it is possible to I think it is reasonable to use slots but how to do it don't know , if you have any ideas how to get a row of selected button please share, thanks.

这是我的桌子

这是我在QTableWidget中添加行的代码

It is a code where i add rows to my QTableWidget

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    for(int i = 0; i<20;i++)
        ui->tableWidget->insertRow(ui->tableWidget->rowCount());
    QVector<QString>vec;
    vec<<"first"<<"sec"<<"third"<<"for"<<"fif"<<"first"<<"sec"
      <<"third"<<"for"<<"fif";
    vec<<"first"<<"sec"<<"third"<<"for"<<"fif"<<"first"<<"sec"
      <<"third"<<"for"<<"fif";
    for(int i = 0; i<ui->tableWidget->rowCount();i++)
    {
        for(int j = 0; j<ui->tableWidget->columnCount();j++)
        {
            if(j == 0)
            {
                QWidget* pWidget = new QWidget();
                QPushButton* btn_edit = new QPushButton();
                btn_edit->setText("Remove");
                QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
                pLayout->addWidget(btn_edit);
                pLayout->setAlignment(Qt::AlignCenter);
                pLayout->setContentsMargins(0, 0, 0, 0);
                pWidget->setLayout(pLayout);
                ui->tableWidget->setCellWidget(i, j, pWidget);
                continue;

            }
            QTableWidgetItem*item = new QTableWidgetItem(vec[i]);
            item->setFlags(item->flags() ^ Qt::ItemIsEditable);
            ui->tableWidget->setItem(i, j, item);
        }
    }
}

推荐答案

可以使用 removeRow() 方法,但必须先获取该行.首先,我们将所有按钮连接到回路内的插槽,如下所示:

This task can be solved using the removeRow() method but before we must get the row. First of all we will connect all the buttons to a slot inside the loop as shown below:

*.h

private slots:
    void onClicked();

*.cpp

[...]
QPushButton* btn_edit = new QPushButton();
btn_edit->setText("Remove");
connect(btn_edit, &QPushButton::clicked, this, &MainWindow::onClicked);
[...]

在插槽中,我们可以通过 sender() 方法,然后得到QWidget父级(以名称pWidget创建),这是添加到QTableWidget且其位置相对于它的小部件,然后使用方法 indexAt() 以获取与单元格关联的QModelIndex通过方法 row() 获得该行的信息.以上所有内容均在以下几行中实现:

In the slot we can get the button that emits the signal through the sender() method, then we get QWidget parent (created with the name pWidget), this is the widget that is added to the QTableWidget and its position is relative to it, then we use the method indexAt() to get the QModelIndex associated with the cell, and this has the information of the row through the method row(). All of the above is implemented in the following lines:

void MainWindow::onClicked()
{
    QWidget *w = qobject_cast<QWidget *>(sender()->parent());
    if(w){
        int row = ui->tableWidget->indexAt(w->pos()).row();
        ui->tableWidget->removeRow(row);
        ui->tableWidget->setCurrentCell(0, 0);
    }
}

注意: setCurrentCell() 方法用于设置焦点,因为具有该焦点的最后一个单元格已被删除.

Note: The setCurrentCell() method is used to set the focus since the last cell that has it has been deleted.

完整的示例可以在以下

The complete example can be found in the following link.

这篇关于如何显示在QTableWidget中单击QPushButton的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆