c++ - 在模型动态插入和删除QTableView中的行时发出信号/槽

标签 c++ qt qtableview qt-signals

我在QPushButton的最后一列中插入了QTableview。使用该按钮,我将使用按钮释放信号和插槽handlebutton(int)删除该特定行。

cpp代码:

MainWindow::MainWindow(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QSortFilterProxyModel *model = new QSortFilterProxyModel(this);
    model = pCApp->guiClient()->getConnectionManagement()->getProxyModel();
    ui->tableView->setModel(model);
    connect(pCApp, SIGNAL(CloseOpenWindowsRequested()), SLOT(close()));
    connect(ui->tableView->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(onRowsNumberChanged()));
    connect(ui->tableView->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(onRowsNumberChanged()));
    ui->tableView->setSortingEnabled(true);
    QPushButton *button;
    QSignalMapper *mapper = new QSignalMapper(this);
    QObject::connect(mapper, SIGNAL (mapped(int)), this, SLOT (handleButton(int)));
    for (int i = 0; i < model->rowCount(); i++)
    {
        button = new QPushButton;
        button->setText("Disconnect " + QString::number(i));
        button->setStyleSheet("QPushButton { color: #E5E5E5; }");
        ui->tableView->setIndexWidget(model->index(i,2, QModelIndex()), button);
        QObject::connect(button, SIGNAL(released()), mapper, SLOT(map()));
        connect(ui->tableView->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(onRowsNumberChanged()));
        connect(ui->tableView->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(onRowsNumberChanged()));
        mapper->setMapping(button, i);
    }
    setAttribute(Qt::WA_DeleteOnClose);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::handleButton(int row)
{
    this->ui->tableView->model()->removeRow(row);
}

void MainWindow::onRowsNumberChanged()
{
    QSortFilterProxyModel *model = new QSortFilterProxyModel(this);
    model = pCApp->guiClient()->getConnectionManagement()->getProxyModel();
    ui->tableView->setModel(model);
    ui->tableView->setSortingEnabled(true);
    QPushButton *button;
    QSignalMapper *mapper = new QSignalMapper(this);
    QObject::connect(mapper, SIGNAL (mapped(int)), this, SLOT (handleButton(int)));
    for (int i = 0; i < model->rowCount(); i++)
    {
        button = new QPushButton;
        button->setText("Disconnect " + QString::number(i));
        button->setStyleSheet("QPushButton { color: #E5E5E5; }");
        ui->tableView->setIndexWidget(model->index(i,2, QModelIndex()), button);
        QObject::connect(button, SIGNAL(released()), mapper, SLOT(map()));
        mapper->setMapping(button, i);
    }

}

hpp代码:
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP

namespace Ui {
class MainWindow;
}

class MainWindow : public QDialog
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
public slots:
    void onLanguageChanged();
    void handleButton(int row);
    void onRowsNumberChanged();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_HPP

在正常情况下,代码可以正常运行。但是,当插入新行和/或删除旧行时,按钮不会按需要出现在最后一列中。我试图使用信号-
connect(ui->tvServStat->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(onRowsNumberChanged()));

connect(ui->tvServStat->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(onRowsNumberChanged()));

两者的插槽都与onRowsNumberChanged()相同,在该插槽中,我再次尝试在最后一列中插入按钮。我的想法是,行数可能正在更改,因此我正在重新实现相同的逻辑。但是,它不起作用。

任何人都可以帮助纠正我的逻辑或其他逻辑以实现此功能。提前致谢!

最佳答案

您应该对该列使用delegate。这样, View 将自动处理行的创建/删除。参见this question

您的代码还存在其他一些问题:

  • 您多次连接rowsInserted / rowsRemoved
  • 在您的代码中,您将创建多个Signal Mapper来映射相同类型的连接。这不是其意图用途
  • 重复onRowsNumberChanged循环
  • 关于c++ - 在模型动态插入和删除QTableView中的行时发出信号/槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45876789/

    相关文章:

    c++ - 斐波那契数列通过串行代码

    c++ - 在有向图中复制多态对象的构造函数

    c++ - Qt音频文件像大胆一样挥舞

    c++ - Qt 表格小部件垂直和水平标题变得不可见

    python - PYQT:QTableView。日期之间的筛选

    qt - 使用 PyQt 连接 QTableView selectionChanged 信号会产生段错误

    c++ - 基于 sizeof c++ 的条件语句的问题

    c++ - C++中运算符重载中的参数数量

    c++ - 如何将文字传递给重载运算符

    c++ - 更改 QHeaderView 空间缓冲区