【发布时间】:2017-08-25 07:55:38
【问题描述】:
我在QTableview 的最后一列中插入QPushButton。使用该按钮,我将使用按钮释放信号和插槽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() 保持相同,我再次尝试在最后一列中插入按钮。我的想法是可能是行数正在改变,所以我正在重新实现相同的逻辑。但是,它不起作用。
任何人都可以帮助纠正我的逻辑或其他逻辑以实现此功能。提前致谢!
【问题讨论】:
标签: c++ qt qtableview qt-signals