Komplexe QTables: QCombobox

Alles rund um die Programmierung mit Qt
Antworten
Illuminatus
Beiträge: 84
Registriert: 3. Dezember 2008 12:48

Komplexe QTables: QCombobox

Beitrag 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 !
upsala
Beiträge: 3946
Registriert: 5. Februar 2006 20:52
Wohnort: Landshut
Kontaktdaten:

Beitrag von upsala »

a)
The QTableWidget class provides an item-based table view with a default model.
b) Beschäftige dich mit dem QItemDelegate.
Illuminatus
Beiträge: 84
Registriert: 3. Dezember 2008 12:48

Beitrag 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());
        }
    }
}
upsala
Beiträge: 3946
Registriert: 5. Februar 2006 20:52
Wohnort: Landshut
Kontaktdaten:

Beitrag von upsala »

Das ist das Standardverhalten, da die ComboBox erst bei Bedarf erzeugt wird.
Illuminatus
Beiträge: 84
Registriert: 3. Dezember 2008 12:48

Beitrag von Illuminatus »

Gibt es da keinen Hack wie ich das ereichen kann?
Nash
Beiträge: 118
Registriert: 27. April 2007 14:49

Beitrag 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.
Antworten