Pergunta

I have the following code:

model = new QStandardItemModel();
channel = new QTableView(this);
channel->setModel(model);

model->setData(model->index(d,0,QModelIndex()), 0 );
model->setData(model->index(d,1,QModelIndex()),Channel);
model->setData(model->index(d,2,QModelIndex()),Unit);
model->setData(model->index(d,3,QModelIndex()),dimension);
model->setData(model->index(d,4,QModelIndex()),code);
model->setData(model->index(d,5,QModelIndex()),description);

I want you to just click on a box, return the row number where the selected field.

any idea how to do this?

Foi útil?

Solução

What you are referring to is called SIGNALS and SLOTS. QObjects emit various signals that you can connect to functions that perform actions. In your case you are probably interested in the clicked signal of the QTableView (Actually provided by the super class QAbstractItemView)

connect(channel, SIGNAL(clicked(QModelIndex), 
           this, SLOT(handleTableClick()));

handleTableClick can really be named anything you want and would be a public SLOT you have defined to handle this signal:

public slots:
    void handleTableClick(const QModelIndex &);

When a user clicks on a valid cell, your slot will be called and you will be passed the QModelIndex. From there you can look up the row.

void Foo::handleTableClick(const QModelIndex &idx) {
    int row = idx.row();
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top