Frage

I got a QTableView looks like the following:

enter image description here

Now I have 2 different functions (Q_SLOT) that do things according to the selected piece of data.

In order to make the table more intuitive, I've done some research and have been considering adding buttons (on column 1 & 2 respectively) to send the signal about which piece of data has been chosen to be the target.

In brief, I am looking for a signal that contains a 2-dimensional information: which function to use, and which piece of data(row) has been chosen. While this could be done outside the table, but I am not considering it since I want the application more intuitive to use.

The number of data counts up to 400~500. In addition to buttons, is there any better Qt-way to work this out? Any directions are welcome, coarse ideas are fine.

War es hilfreich?

Lösung

The buttons can be added as delegates. They are visual mockups at that point - they look like buttons, but they don't act like buttons. The delegate can create a real button widget when the cell is entered. It's then a simple matter to set some properties on the button to indicate the function and row. Recall that QObject has a flexible property system - see the setProperty and property methods. All widgets are QObjects.

The receiver of the button's clicked signal can use the sender() method to access the sending button instance, and read the properties. You can also subclass the QPushButton and emit a custom signal upon the button being clicked.

Andere Tipps

You can use the QSignalMapper class which collects a set of parameterless signals, and re-emits them with integer, string or widget parameters corresponding to the object that sent the signal. So you can have one like:

QSignalMapper * mapper = new QSignalMapper(this);
QObject::connect(mapper,SIGNAL(mapped(int)),this,SLOT(function1(int)));

When adding your buttons in each row of table view, you can connect the clicked() signal of the button to the map() slot of QSignalMapper and add a mapping using setMapping so that when clicked() is signaled from a button, the signal mapped(int) is emitted:

QPushButton * but = new QPushButton(this);
but->setText("Function1");

QObject::connect(but, SIGNAL(clicked()),mapper,SLOT(map()));
mapper->setMapping(but, rowIndex);

ui->tableView->setIndexWidget(model->item(rowIndex,column)->index(),but);

This way whenever you click a button in a row, the mapped(int) signal of the mapper is emitted containing the row number.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top