PDA

View Full Version : QTableView, editable, 3 columns: QString, QWidget and QWidget



hml
28th March 2010, 12:30
I'm trying to have an editable table of entries. Each entry has 3 columns.

The 1st is a choice of a string among a dynamic list of strings. So graphically, it should be a "pull-down menu"

The 2nd is, for the select string from the 1st column, there is a list of fields, values. I have a function that returns a QWidget that is made of QLabels and QLineEdits. I would like this QWidget to be in the 2nd column.

The 3rd column is a file path in a QLineEdit with a QPushButton.

The whole table is editable.

This QTableView uses a class derived from QAbstractTableModel. However I am unable to return QWidgets from the
QVariant data(const QModelIndex& index, int role) const function.

What should I use when the cells of the table are complex widgets?

Regards,

Lykurg
28th March 2010, 13:02
You have to use delegates for that. See "Delegate Classes" and/or "An Introduction to Model/View Programming" in the documentation for a first start.

hml
31st March 2010, 20:28
Thanks,

I am writing the

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
function of the delegate.

Column 0 is a non editable QComboBox where the user selects between a list of application objects represented by QStrings.
Columm 1 is an editable QWidget with (QLabels and QLineEdits). This cell depends on what is selected in the QComboBox.

There can be a different selection in the QComboBox for each row, of course.

This is the start of the implementation of createEditor:

{
switch(index.column()) {
case 0: {
QComboBox* systems_widget = new QComboBox(this); /// check parenting is ok
systems_widget->setEditable(false);
for (....) /// pseudo code here
systems_widget->addItem( QString(someStdString.c_str()) );
}
return systems_widget;
}
case 1:
return .... /// How do I pick up the selected item from the QComboBox in column 0 ?
case 2:
/// to implement
default:
return 0;
};
}

hml
1st April 2010, 23:56
Hello,

My QTableView is basically in Edit mode all the time.
The
createEditor returns a widget that is placed on top of the display widget for each cell when editing.
I don't need this.
Can I reuse the normal display widget (is there a default one?)

Rds,