2017-08-25 10 views
0

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())); 

スロット - 私は、信号を使用しようとしました。私の考えは、行数が変更されている可能性がありますので、私は同じロジックを再実装しています。しかし、それは動作しません。

この機能を実現するには、誰かが自分のロジックやロジックを修正するのを手助けできますか?前もって感謝します!

答えて

0

この列にはdelegateを使用する必要があります。このようにして、ビューは行の作成/削除を自動的に処理します。 this question


はあなたのコードといくつかの他の問題もあります参照してください:あなたはあなたのコードではrowsInserted/rowsRemoved複数回

  • を接続

    1. が、あなたは、同じ種類をマッピングするための複数の信号マッパーを作成します接続。これは、ウィジェットを使用して、あなたの問題を解決するためにonRowsNumberChangedループ
  • +0

    私は前に、デリゲートを使用していたが、それはスタイリングする必要が標準スタイルのボタンを生成します。私は2日間試したが、そのスタイルには合わなかった。このアプローチでは、行の自動作成/削除の処理を除いて、最大限の機能を達成することができました。このアプローチを変更する手助けはできますか?あなたのお時間をありがとうございました。 – tanmayb

    +0

    また、複数のエントリは、正しい位置を把握しようとしているからです。また、あなたが助けることができる場合。 – tanmayb

    +0

    リンクされた質問(およびqtcenterへのフォローアップリンク)に記載されているように、ウィジェットを使用することはうまく拡張されません。あなたは実際に代理人を使うべきです。デリゲートのアプローチで新しい質問を投稿することをお勧めします –

    0

    のそのintendend使用

  • 複製ではありません、あなたは以下のアプローチを使用することができます。これは、Qtの5の新しい接続構文とC++ラムダを使用していますので、信号マッパの必要性を排除することができます。

    #include "form.h" 
    #include <QtWidgets> 
    #include <QStandardItemModel> 
    
    Form::Form(QWidget *parent) : 
        QWidget(parent) 
    { 
        QPushButton *b = new QPushButton("add row"); 
        m_tree = new QTreeView(this); 
        QBoxLayout *l = new QVBoxLayout(this); 
        l->addWidget(m_tree, 1); 
        l->addWidget(b); 
    
        QStandardItemModel *m = new QStandardItemModel(0, 3, this); 
        m_tree->setModel(m); 
    
        connect(b, &QPushButton::clicked, this, &Form::addRow); 
        connect(m, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(addItemButtons())); 
        connect(m, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(addItemButtons())); 
    } 
    
    Form::~Form() 
    { 
    } 
    
    void Form::addRow() 
    { 
        m_tree->model()->insertRow(m_tree->model()->rowCount()); 
    } 
    
    void Form::addItemButtons() 
    { 
        for (int i = 0; i < m_tree->model()->rowCount(); ++i) { 
         auto idx = m_tree->model()->index(i, 2); 
         QPushButton *b = new QPushButton("X"); 
         b->setStyleSheet("QPushButton {color: #E5E5E5;}"); 
         m_tree->setIndexWidget(idx, b); 
         connect(b, &QPushButton::clicked, [=](){ 
          m_tree->model()->removeRow(i); 
         }); 
        } 
    } 
    
  • 関連する問題