QPushButtonをtableviewの最後の列に挿入します。そのボタンを使用して、ボタンリリース信号とスロット 'handlebutton(int)'を使用して特定の行を削除しています。モデルがQTableViewの行を動的に挿入および削除するときの信号/スロット
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
、コードが正しく機能しています。しかし、新しい行が挿入されたり古い行が削除されたりすると、ボタンは最後の列には表示されません。私は()、ここで私は再び最後の列にボタンを挿入しようとしていますonRowsNumberChangedと同じ保管しております、両方のために
connect(ui->tvServStat->model(), SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(onRowsNumberChanged()));
connect(ui->tvServStat->model(), SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(onRowsNumberChanged()));
スロット - 私は、信号を使用しようとしました。私の考えは、行数が変更されている可能性がありますので、私は同じロジックを再実装しています。しかし、それは動作しません。
この機能を実現するには、誰かが自分のロジックやロジックを修正するのを手助けできますか?前もって感謝します!
私は前に、デリゲートを使用していたが、それはスタイリングする必要が標準スタイルのボタンを生成します。私は2日間試したが、そのスタイルには合わなかった。このアプローチでは、行の自動作成/削除の処理を除いて、最大限の機能を達成することができました。このアプローチを変更する手助けはできますか?あなたのお時間をありがとうございました。 – tanmayb
また、複数のエントリは、正しい位置を把握しようとしているからです。また、あなたが助けることができる場合。 – tanmayb
リンクされた質問(およびqtcenterへのフォローアップリンク)に記載されているように、ウィジェットを使用することはうまく拡張されません。あなたは実際に代理人を使うべきです。デリゲートのアプローチで新しい質問を投稿することをお勧めします –