Seite 1 von 1

QComboBox mit Model

Verfasst: 9. November 2008 23:42
von XxM4ST3RxX
Also mein Problem ist folgendes, ich habe ein ComboBox mit einem Model gefüllt und wie bekomme ich den zuzeit ausgewählten ModelIndex raus ? wie zb. bein nem TreeView mit currentIndex

Verfasst: 10. November 2008 08:00
von Ginsengelf
Moin, wo brauchst du den denn? Die allermeisten Methoden der Models haben den QModelIndex, den sie gerade bearbeiten, als Parameter.

Ginsengelf

Verfasst: 10. November 2008 09:07
von upsala
Über rootModelIndex() und currentIndex() selbst konstruieren.

Verfasst: 10. November 2008 15:54
von XxM4ST3RxX
Ginsengelf hat geschrieben:Moin, wo brauchst du den denn? Die allermeisten Methoden der Models haben den QModelIndex, den sie gerade bearbeiten, als Parameter.

Ginsengelf
ich brauche nur de id aus der Datenbank und wen die Daten in der ComboBox sind komme ich nicht an den ModelIndex ran das mit dem rootIndex schau ich mir mal an

Verfasst: 10. November 2008 16:47
von Ginsengelf
Nach dem zweiten Mal lesen habe ich jetzt deine Frage richtig verstanden...
Mit upsalas Vorschlag sollte es funktionieren.

Ginsengelf

Verfasst: 10. November 2008 17:47
von XxM4ST3RxX
könnte mir vielleicht jemmand ein Konkretes Beispiel zeigen, denn ich komme immer noch nicht so dahinter was ich mit dem rootModelIndex() erhalte.

Ich habe ein Tabelle mit Klassen id | name
und noch eine mit Schülern id | name | geburtstag | klassen_id
die Schüler werden in einem TreeView mit einem SqlRelationTableModel angezeigt
wenn man jetzt einen Datensatz im TreeView auswählt werden auchtomatisch name -> lineEdit | geburtstag -> dateEdit ausgefüllt und zu letzt soll nur noch die Klasse in der ComboBox ausgewählt werden welche schon mit einem Klassen Model gefüllt wurde und danach will ich aber auch wieder die klassen_id erfahren die in der ComboBox gewählt wurde. Wäre echt froh wenn mir jemmand ein beispiel nur für die ComboBox liefern kann der rest läuft schon. Bin schon richtig am verzweifeln ...

Verfasst: 10. November 2008 19:58
von upsala

Code: Alles auswählen

index=combo->model()->index(combo->currentIndex(), 0, combo->rootModelIndex());
eventuell geht auch:

Code: Alles auswählen

combo->rootModelIndex().child(combo->currentIndex(), 0)

Verfasst: 10. November 2008 20:33
von XxM4ST3RxX
vielen dank jetzt klappt es ich bekomme aus der ComboBox die richtige id aus der Datenban und noch eine Frage ^^ jetzt habe ich noch den index von dem TreeView mit dem ich die ID(Datenbank id) der Klasse bekomme wie kann ich jetzt den currentIndex der ComboBox auf diese id setzen weil bei mir ist es gerade so: meine Datenbank id ist 4 und ind er ComboBox ist der currentIndex für diese id -> 2

Verfasst: 11. November 2008 23:56
von XxM4ST3RxX
niemand ne Ahnung ?

hier ist der Code der Klasse vll hilf das ^^ und nicht über den Aufbau wundern ist mein erstes Qt Programm.

pupils.h

Code: Alles auswählen

#ifndef PUPILS_H
#define PUPILS_H

#include <QtGui/QWidget>
#include <QtSql>
#include <QHeaderView>
#include <QMessageBox>
#include "ui_pupils.h"

class Pupils : public QWidget
{
    Q_OBJECT

public:
    Pupils(QWidget *parent = 0);
    ~Pupils();

private:
    Ui::PupilsClass ui;

signals:
	void refreshDisplay();

private slots:
	void refreshList();
	void submit();
	void getEdit(const QModelIndex & index);
};

#endif // PUPILS_H
pupils.cpp

Code: Alles auswählen

#include "pupils.h"

Pupils::Pupils(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);

	connect(this, SIGNAL(refreshDisplay()), this, SLOT(refreshList()));
	connect(ui.pbAbort, SIGNAL(clicked()), this, SLOT(close()));
	connect(ui.pbSubmit, SIGNAL(clicked()), this, SLOT(submit()));
	connect(ui.tvList, SIGNAL(clicked(QModelIndex)), this, SLOT(getEdit(QModelIndex)));

	emit refreshDisplay();
}

Pupils::~Pupils()
{

}

void Pupils::refreshList()
{
	QSqlRelationalTableModel *model = new QSqlRelationalTableModel;
	model->setTable("pupils");
	model->setHeaderData(0, Qt::Horizontal, tr("ID"));
	model->setHeaderData(1, Qt::Horizontal, tr("Name"));;
	model->setHeaderData(2, Qt::Horizontal, tr("Geburtstag"));
	model->setHeaderData(3, Qt::Horizontal, tr("Klasse"));
	model->setRelation(3, QSqlRelation("classes", "id", "name"));

	ui.tvList->setModel(model);
	ui.tvList->hideColumn(0);
	QHeaderView *header = ui.tvList->header();
	header->setResizeMode(QHeaderView::ResizeToContents);

	QSqlRelationalTableModel *classesModel = new QSqlRelationalTableModel;
	classesModel->setTable("classes");
	classesModel->select();

	ui.cbAddClass->setModel(classesModel);
	ui.cbAddClass->setModelColumn(1);
	ui.cbEditClass->setModel(classesModel);
	ui.cbEditClass->setModelColumn(1);
}

void Pupils::submit()
{
	QSqlQuery query;
	QMessageBox msgBox(this);
	QModelIndex index = ui.tvList->currentIndex();
	QDate checkDate(2000, 1, 1);

	switch(ui.lwActions->currentRow())
	{
	case 0 :
		query.exec("INSERT INTO `pupils` ("
				   "`id` ,"
				   "`name` ,"
				   "`bday` ,"
				   "`classes_id`"
				   ")"
				   "VALUES ("
				   " NULL , '" + ui.leAddName->text() + "', '" + ui.deAddBday->date().toString(Qt::ISODate) + "', '" + ui.cbAddClass->model()->index(ui.cbAddClass->currentIndex(), 0, ui.cbAddClass->rootModelIndex()).data(0).toString() + "'"
				   ");");

    	msgBox.setText("Schüler '" + ui.leAddName->text() + "' hinzugefügt.");
    	msgBox.setIcon(QMessageBox::Information);
    	msgBox.setDetailedText(query.lastQuery());
    	msgBox.exec();

		break;
	case 1 :
		query.exec("UPDATE `pupils` SET `name` = '" + ui.leEditName->text() + "',"
				"`bday` = '" + ui.deEditBday->date().toString(Qt::ISODate) + "',"
				"`classes_id` = '" + ui.cbEditClass->model()->index(ui.cbEditClass->currentIndex(), 0, ui.cbEditClass->rootModelIndex()).data(0).toString() + "' WHERE `id` = " + index.sibling(index.row(), 0).data().toString());

    	msgBox.setText("Schüler '" + index.sibling(index.row(), 1).data().toString() + "' bearbeitet.");
    	msgBox.setIcon(QMessageBox::Information);
    	msgBox.setDetailedText(query.lastQuery());
    	msgBox.exec();

		break;
	case 2 :
		query.exec("SELECT *"
				   "FROM `shares`"
				   "WHERE `pupils_id_name` = " + index.sibling(index.row(), 0).data().toString());

		if(!query.next())
		{
			query.exec("DELETE FROM `pupils`"
					   " WHERE `id` = " + index.sibling(index.row(), 0).data().toString());

	    	msgBox.setText("Schüler '" + index.sibling(index.row(), 1).data().toString() + "' gelöscht.");
	    	msgBox.setIcon(QMessageBox::Information);
	    	msgBox.setDetailedText(query.lastQuery());
	    	msgBox.exec();
		}
		else
		{
	    	msgBox.setText("Schüler '" + index.sibling(index.row(), 1).data().toString() + "' kann nicht gelöscht werden, weil er noch Bücher hat.");
	    	msgBox.setIcon(QMessageBox::Information);
	    	msgBox.setDetailedText(query.lastQuery());
	    	msgBox.exec();
		}

		break;
	}

	ui.leAddName->clear();
	ui.deAddBday->setDate(checkDate);

	ui.leEditName->clear();
	ui.deEditBday->setDate(checkDate);

	emit refreshDisplay();
}

void Pupils::getEdit(const QModelIndex & index)
{
	ui.leEditName->setText(index.sibling(index.row(), 1).data().toString());
	ui.deEditBday->setDate(index.sibling(index.row(), 2).data().toDate());
	//TODO ui.cbEditClass ...
}
ui.pupils.h

Code: Alles auswählen

/********************************************************************************
** Form generated from reading ui file 'pupils.ui'
**
** Created: Mon 10. Nov 22:17:33 2008
**      by: Qt User Interface Compiler version 4.4.3
**
** WARNING! All changes made in this file will be lost when recompiling ui file!
********************************************************************************/

#ifndef UI_PUPILS_H
#define UI_PUPILS_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QComboBox>
#include <QtGui/QDateEdit>
#include <QtGui/QFormLayout>
#include <QtGui/QGroupBox>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QListWidget>
#include <QtGui/QPushButton>
#include <QtGui/QSpacerItem>
#include <QtGui/QStackedWidget>
#include <QtGui/QTreeView>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>

QT_BEGIN_NAMESPACE

class Ui_PupilsClass
{
public:
    QHBoxLayout *horizontalLayout_2;
    QListWidget *lwActions;
    QVBoxLayout *verticalLayout_6;
    QTreeView *tvList;
    QStackedWidget *swActions;
    QWidget *_add;
    QVBoxLayout *verticalLayout_2;
    QGroupBox *gbAdd;
    QVBoxLayout *verticalLayout;
    QFormLayout *formLayout;
    QLabel *lAddName;
    QLineEdit *leAddName;
    QLabel *lAddBday;
    QDateEdit *deAddBday;
    QLabel *lAddClass;
    QComboBox *cbAddClass;
    QWidget *_edit;
    QVBoxLayout *verticalLayout_4;
    QGroupBox *gbEdit;
    QVBoxLayout *verticalLayout_3;
    QFormLayout *formLayout_2;
    QLabel *lEditName;
    QLineEdit *leEditName;
    QLabel *lEditBday;
    QDateEdit *deEditBday;
    QLabel *lEditClass;
    QComboBox *cbEditClass;
    QWidget *_delete;
    QVBoxLayout *verticalLayout_5;
    QGroupBox *gbDelete;
    QHBoxLayout *horizontalLayout;
    QSpacerItem *horizontalSpacer;
    QPushButton *pbSubmit;
    QPushButton *pbAbort;

    void setupUi(QWidget *PupilsClass)
    {
    if (PupilsClass->objectName().isEmpty())
        PupilsClass->setObjectName(QString::fromUtf8("PupilsClass"));
    PupilsClass->setWindowModality(Qt::ApplicationModal);
    PupilsClass->resize(500, 400);
    PupilsClass->setMinimumSize(QSize(500, 400));
    QIcon icon;
    icon.addPixmap(QPixmap(QString::fromUtf8(":/img/icons/note")), QIcon::Normal, QIcon::Off);
    PupilsClass->setWindowIcon(icon);
    horizontalLayout_2 = new QHBoxLayout(PupilsClass);
    horizontalLayout_2->setSpacing(6);
    horizontalLayout_2->setMargin(11);
    horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
    lwActions = new QListWidget(PupilsClass);
    QIcon icon1;
    icon1.addPixmap(QPixmap(QString::fromUtf8(":/img/icons/add")), QIcon::Normal, QIcon::Off);
    QListWidgetItem *__listItem = new QListWidgetItem(lwActions);
    __listItem->setIcon(icon1);
    QIcon icon2;
    icon2.addPixmap(QPixmap(QString::fromUtf8(":/img/icons/edit")), QIcon::Normal, QIcon::Off);
    QListWidgetItem *__listItem1 = new QListWidgetItem(lwActions);
    __listItem1->setIcon(icon2);
    QIcon icon3;
    icon3.addPixmap(QPixmap(QString::fromUtf8(":/img/icons/remove")), QIcon::Normal, QIcon::Off);
    QListWidgetItem *__listItem2 = new QListWidgetItem(lwActions);
    __listItem2->setIcon(icon3);
    lwActions->setObjectName(QString::fromUtf8("lwActions"));
    lwActions->setMinimumSize(QSize(100, 0));
    lwActions->setMaximumSize(QSize(100, 16777215));
    lwActions->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    lwActions->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    lwActions->setMovement(QListView::Static);
    lwActions->setGridSize(QSize(96, 64));
    lwActions->setViewMode(QListView::IconMode);

    horizontalLayout_2->addWidget(lwActions);

    verticalLayout_6 = new QVBoxLayout();
    verticalLayout_6->setSpacing(6);
    verticalLayout_6->setObjectName(QString::fromUtf8("verticalLayout_6"));
    tvList = new QTreeView(PupilsClass);
    tvList->setObjectName(QString::fromUtf8("tvList"));
    tvList->setEditTriggers(QAbstractItemView::NoEditTriggers);
    tvList->setRootIsDecorated(false);
    tvList->setSortingEnabled(true);
    tvList->setAllColumnsShowFocus(true);

    verticalLayout_6->addWidget(tvList);

    swActions = new QStackedWidget(PupilsClass);
    swActions->setObjectName(QString::fromUtf8("swActions"));
    swActions->setMinimumSize(QSize(0, 135));
    swActions->setMaximumSize(QSize(16777215, 135));
    _add = new QWidget();
    _add->setObjectName(QString::fromUtf8("_add"));
    verticalLayout_2 = new QVBoxLayout(_add);
    verticalLayout_2->setSpacing(6);
    verticalLayout_2->setMargin(11);
    verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
    gbAdd = new QGroupBox(_add);
    gbAdd->setObjectName(QString::fromUtf8("gbAdd"));
    verticalLayout = new QVBoxLayout(gbAdd);
    verticalLayout->setSpacing(6);
    verticalLayout->setMargin(11);
    verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
    formLayout = new QFormLayout();
    formLayout->setSpacing(6);
    formLayout->setObjectName(QString::fromUtf8("formLayout"));
    formLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
    lAddName = new QLabel(gbAdd);
    lAddName->setObjectName(QString::fromUtf8("lAddName"));

    formLayout->setWidget(0, QFormLayout::LabelRole, lAddName);

    leAddName = new QLineEdit(gbAdd);
    leAddName->setObjectName(QString::fromUtf8("leAddName"));
    leAddName->setMaxLength(64);

    formLayout->setWidget(0, QFormLayout::FieldRole, leAddName);

    lAddBday = new QLabel(gbAdd);
    lAddBday->setObjectName(QString::fromUtf8("lAddBday"));

    formLayout->setWidget(1, QFormLayout::LabelRole, lAddBday);

    deAddBday = new QDateEdit(gbAdd);
    deAddBday->setObjectName(QString::fromUtf8("deAddBday"));
    deAddBday->setCalendarPopup(true);

    formLayout->setWidget(1, QFormLayout::FieldRole, deAddBday);

    lAddClass = new QLabel(gbAdd);
    lAddClass->setObjectName(QString::fromUtf8("lAddClass"));

    formLayout->setWidget(2, QFormLayout::LabelRole, lAddClass);

    cbAddClass = new QComboBox(gbAdd);
    cbAddClass->setObjectName(QString::fromUtf8("cbAddClass"));
    cbAddClass->setModelColumn(1);

    formLayout->setWidget(2, QFormLayout::FieldRole, cbAddClass);


    verticalLayout->addLayout(formLayout);


    verticalLayout_2->addWidget(gbAdd);

    swActions->addWidget(_add);
    _edit = new QWidget();
    _edit->setObjectName(QString::fromUtf8("_edit"));
    verticalLayout_4 = new QVBoxLayout(_edit);
    verticalLayout_4->setSpacing(6);
    verticalLayout_4->setMargin(11);
    verticalLayout_4->setObjectName(QString::fromUtf8("verticalLayout_4"));
    gbEdit = new QGroupBox(_edit);
    gbEdit->setObjectName(QString::fromUtf8("gbEdit"));
    verticalLayout_3 = new QVBoxLayout(gbEdit);
    verticalLayout_3->setSpacing(6);
    verticalLayout_3->setMargin(11);
    verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));
    formLayout_2 = new QFormLayout();
    formLayout_2->setSpacing(6);
    formLayout_2->setObjectName(QString::fromUtf8("formLayout_2"));
    formLayout_2->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
    lEditName = new QLabel(gbEdit);
    lEditName->setObjectName(QString::fromUtf8("lEditName"));

    formLayout_2->setWidget(0, QFormLayout::LabelRole, lEditName);

    leEditName = new QLineEdit(gbEdit);
    leEditName->setObjectName(QString::fromUtf8("leEditName"));
    leEditName->setMaxLength(64);

    formLayout_2->setWidget(0, QFormLayout::FieldRole, leEditName);

    lEditBday = new QLabel(gbEdit);
    lEditBday->setObjectName(QString::fromUtf8("lEditBday"));

    formLayout_2->setWidget(1, QFormLayout::LabelRole, lEditBday);

    deEditBday = new QDateEdit(gbEdit);
    deEditBday->setObjectName(QString::fromUtf8("deEditBday"));
    deEditBday->setCalendarPopup(true);

    formLayout_2->setWidget(1, QFormLayout::FieldRole, deEditBday);

    lEditClass = new QLabel(gbEdit);
    lEditClass->setObjectName(QString::fromUtf8("lEditClass"));

    formLayout_2->setWidget(2, QFormLayout::LabelRole, lEditClass);

    cbEditClass = new QComboBox(gbEdit);
    cbEditClass->setObjectName(QString::fromUtf8("cbEditClass"));
    cbEditClass->setModelColumn(1);

    formLayout_2->setWidget(2, QFormLayout::FieldRole, cbEditClass);


    verticalLayout_3->addLayout(formLayout_2);


    verticalLayout_4->addWidget(gbEdit);

    swActions->addWidget(_edit);
    _delete = new QWidget();
    _delete->setObjectName(QString::fromUtf8("_delete"));
    verticalLayout_5 = new QVBoxLayout(_delete);
    verticalLayout_5->setSpacing(6);
    verticalLayout_5->setMargin(11);
    verticalLayout_5->setObjectName(QString::fromUtf8("verticalLayout_5"));
    gbDelete = new QGroupBox(_delete);
    gbDelete->setObjectName(QString::fromUtf8("gbDelete"));

    verticalLayout_5->addWidget(gbDelete);

    swActions->addWidget(_delete);

    verticalLayout_6->addWidget(swActions);

    horizontalLayout = new QHBoxLayout();
    horizontalLayout->setSpacing(6);
    horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
    horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);

    horizontalLayout->addItem(horizontalSpacer);

    pbSubmit = new QPushButton(PupilsClass);
    pbSubmit->setObjectName(QString::fromUtf8("pbSubmit"));

    horizontalLayout->addWidget(pbSubmit);

    pbAbort = new QPushButton(PupilsClass);
    pbAbort->setObjectName(QString::fromUtf8("pbAbort"));

    horizontalLayout->addWidget(pbAbort);


    verticalLayout_6->addLayout(horizontalLayout);


    horizontalLayout_2->addLayout(verticalLayout_6);


    retranslateUi(PupilsClass);
    QObject::connect(lwActions, SIGNAL(currentRowChanged(int)), swActions, SLOT(setCurrentIndex(int)));

    lwActions->setCurrentRow(0);
    swActions->setCurrentIndex(0);


    QMetaObject::connectSlotsByName(PupilsClass);
    } // setupUi

    void retranslateUi(QWidget *PupilsClass)
    {
    PupilsClass->setWindowTitle(QApplication::translate("PupilsClass", "Sch\303\274ler", 0, QApplication::UnicodeUTF8));

    const bool __sortingEnabled = lwActions->isSortingEnabled();
    lwActions->setSortingEnabled(false);
    lwActions->item(0)->setText(QApplication::translate("PupilsClass", "Hinzuf\303\274gen", 0, QApplication::UnicodeUTF8));
    lwActions->item(1)->setText(QApplication::translate("PupilsClass", "Bearbeiten", 0, QApplication::UnicodeUTF8));
    lwActions->item(2)->setText(QApplication::translate("PupilsClass", "L\303\266schen", 0, QApplication::UnicodeUTF8));

    lwActions->setSortingEnabled(__sortingEnabled);
    gbAdd->setTitle(QApplication::translate("PupilsClass", "Hinzuf\303\274gen", 0, QApplication::UnicodeUTF8));
    lAddName->setText(QApplication::translate("PupilsClass", "Name", 0, QApplication::UnicodeUTF8));
    lAddBday->setText(QApplication::translate("PupilsClass", "Geburtstag", 0, QApplication::UnicodeUTF8));
    lAddClass->setText(QApplication::translate("PupilsClass", "Klasse", 0, QApplication::UnicodeUTF8));
    gbEdit->setTitle(QApplication::translate("PupilsClass", "Bearbeiten", 0, QApplication::UnicodeUTF8));
    lEditName->setText(QApplication::translate("PupilsClass", "Name", 0, QApplication::UnicodeUTF8));
    lEditBday->setText(QApplication::translate("PupilsClass", "Geburtstag", 0, QApplication::UnicodeUTF8));
    lEditClass->setText(QApplication::translate("PupilsClass", "Klasse", 0, QApplication::UnicodeUTF8));
    gbDelete->setTitle(QApplication::translate("PupilsClass", "L\303\266schen", 0, QApplication::UnicodeUTF8));
    pbSubmit->setText(QApplication::translate("PupilsClass", "Ausf\303\274hren", 0, QApplication::UnicodeUTF8));
    pbAbort->setText(QApplication::translate("PupilsClass", "Abbrechen", 0, QApplication::UnicodeUTF8));
    Q_UNUSED(PupilsClass);
    } // retranslateUi

};

namespace Ui {
    class PupilsClass: public Ui_PupilsClass {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_PUPILS_H