1

3 つQLineEditの s (たとえば、名前、住所、電話番号)、2 QPushButton(追加と変更)、およびQTableView.

すべての にテキストを入力し、QLineEdit[追加] ボタンをクリックすると、 の 3 つのテキストすべてQLineEditが の 1 行目に追加されQTableViewます。
に 3 つのテキストを入力しQLineEditて [追加] ボタンをクリックすると、テキストは の 2 行目に配置されQTableViewます。このように続けるべきです。私はこれをすべてやりましたが、うまくいきます。

から任意の行を選択しQTableView、[変更] ボタンをクリックすると、選択した行を から削除しQTableView、アイテムをそれぞれQLineEditの に再度配置する必要があります。

これどうやってするの ?

Example.h

#ifndef EXAMPLE_H
#define EXAMPLE_H

#include <QWidget>
#include <QStandardItemModel>

namespace Ui {
class Example;
}

class Example : public QWidget
{
    Q_OBJECT

public:
    explicit Example (QWidget *parent = 0);
    ~Example();

private slots:
    void on_addButton_released();
    void on_modifyButton_released();

private:
    Ui::Example*ui;
    QStandardItemModel *model;
};

#endif // EXAMPLE_H

例.CPP

#include "Example.h"

Example::Example(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Example)
{
    ui->setupUi(this);
    model = new QStandardItemModel();

    ui->tableView->setModel(model);

}

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

void Example::on_addButton_released()
{
    model->setHorizontalHeaderItem(0, new QStandardItem(QString(" NAME ")));
    model->setHorizontalHeaderItem(1, new QStandardItem(QString(" ADDRESS ")));
    model->setHorizontalHeaderItem(2, new QStandardItem(QString(" PHONE NO ")));

    QStandardItem *nameItem = new QStandardItem(ui->nameLineEdit->text());
    QStandardItem *addressItem = new QStandardItem(ui->addressLineEdit->text());
    QStandardItem *phoneItem = new QStandardItem(ui->phoneLineEdit->text());

    QList<QStandardItem*> row;
    row << nameItem << addressItem << phoneItem;

    model->appendRow(row);

    ui->nameLineEdit->clear();
    ui->addressLineEdit->clear();
    ui->mobileLineEdit->clear();
    ui->emailLineEdit->clear();
}

void Example::on_modifyButton_released()
{


}
4

1 に答える 1

1

あなたがしたいことは、[変更] ボタンがクリックされたときに、 の からの選択にアクセスすることQItemSelectionModelですQTableView。選択したら、それを処理します。

例えば:

void Example::on_modifyButton_released()
{
    if( ui->myTableView )
    {
         QModelIndex currentIndex = ui->myTableView->selectionModel();

         // Make sure to check the index is valid, as the user
         // may not have selected a row.
         if( currentRow.isValid() )
         {
              // Add your code here to copy the data to 
              // your QLineEdit and remove the row from your
              // QStandardModel.
              ...                  
         }
    }
}

参考のため:

QTableView http://qt-project.org/doc/qt-4.8/QTableView.html

QItemSelectionModel http://qt-project.org/doc/qt-4.8/QItemSelectionModel.html

于 2012-09-26T12:42:48.430 に答える