Seite 1 von 1
Komplexe QTables: QCombobox
Verfasst: 4. Juli 2010 21:45
von Illuminatus
Hi ich habe ein paar generelle Fragen zu Qt Tables:
a) Was ist der unterschied zwischen QTableView und QTableWidgets?
b) Wie kann ich erreichen, dass die Elemente einer bestimmten Zeile als QComboBoxen dargestellt werden?
Viele Grüße und Danke !
Verfasst: 4. Juli 2010 22:01
von upsala
a)
The QTableWidget class provides an item-based table view with a default model.
b) Beschäftige dich mit dem QItemDelegate.
Verfasst: 7. Juli 2010 11:14
von Illuminatus
Hi hab es hinbekommen. hier der source code falls jemand dassselbe problem hat.
Ich habe jetzt nur noch das Problem, dass die Comboboxen erst angezeigt werden wenn ich in das Feld klicke! Hat jemand ne idee, wie man das löst?
VG
Code: Alles auswählen
QWidget * ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStandardItemModel *model = createComboboxModel();
if(index.row()==0)
{
QComboBox *editor = new QComboBox(parent);
editor->setModel(model);
editor->setCurrentIndex(index.column());
return editor;
}
QLineEdit *editor = new QLineEdit(parent);
editor->setEnabled(false);
return editor;
}
void ComboDelegate::commitAndCloseEditor(){
QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
if(edit){
edit->setText(index.model()->data(index, Qt::EditRole).toString());
}else{
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
if(comboBox){
QString selectedString = index.model()->data(index, Qt::EditRole).toString();
comboBox->setEditText(selectedString);
}
}
}
void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
if(edit){
model->setData(index, edit->text());
}else{
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
if(comboBox){
model->setData(index, comboBox->currentText());
}
}
}
Verfasst: 7. Juli 2010 12:14
von upsala
Das ist das Standardverhalten, da die ComboBox erst bei Bedarf erzeugt wird.
Verfasst: 7. Juli 2010 12:57
von Illuminatus
Gibt es da keinen Hack wie ich das ereichen kann?
Verfasst: 7. Juli 2010 15:37
von Nash
Wenn es das ist was du willst, dann kannst du die funktion:
void setCellWidget ( int row, int column, QWidget * widget )
vom QTableWidget nehmen. Dann kannst du dir das delegate sparen.
Ansonsten musst du die paint routine implementieren,
Der Vorteil beim delegate ist halt, dass es nur eine ComboBox für alle items.
gibt.