#ifndef DELSTRSETTABLE
#define DELSTRSETTABLE
#include <QtWidgets>
#include <QtSql>
{
Q_OBJECT
public:
QString strg
("SELECT trID, trName FROM allTch WHERE trNo>0");
tch->setQuery(strg);
allt = tch->rowCount();
QString str2
("SELECT strID, strName FROM tblstreams WHERE strNo>0");
str->setQuery(str2);
alls = str->rowCount();
font2.setFamily(QStringLiteral("Arial Narrow"));
font2.setPointSize(10);
}
{
// ComboBox ony in column 4
if(index.column() == 3) {
// Create the combobox on stream column and populate it
sbCb->setModel(str);
sbCb->setModelColumn(1);
sbCb->setFont(font2);
sbCb->setFrame(false);
return sbCb;
// Create the combobox on teacher column and populate it
} else if (index.column() == 4) {
tchCb->setModel(tch);
tchCb->setModelColumn(1);
tchCb->setFont(font2);
tchCb->setFrame(false);
return tchCb;
}
else
}
{
if(index.column()==3)
{ QComboBox *combobox
= qobject_cast<QComboBox
*>
(editor
);
Q_ASSERT(combobox);
for(int x = 0;x<alls;++x)
{
int id = ID.data().toInt();
if(index.data().toInt() == id){
int cbIndex = combobox->findText(fulname);
if(cbIndex >= 0)
combobox->setCurrentIndex(cbIndex);
}
}
}
else if(index.column()==4)
{ QComboBox *combobox
= qobject_cast<QComboBox
*>
(editor
);
Q_ASSERT(combobox);
for(int x = 0;x<allt;++x)
{
int id = ID.data().toInt();
if(index.data().toInt() == id){
int cbIndex = combobox->findText(fulname);
if(cbIndex >= 0)
combobox->setCurrentIndex(cbIndex); }
}
} else {
}
}
{
if(index.column()==3)
{ QComboBox *combbox
= qobject_cast<QComboBox
*>
(editor
);
Q_ASSERT(combbox);
for(int x = 0;x<alls;++x)
{
int id = ID.data().toInt();
if(combbox->currentText() == fulname)
model->setData(index,id);
}
}
else if(index.column()==4)
{
QComboBox *combobx
= qobject_cast<QComboBox
*>
(editor
);
Q_ASSERT(combobx);
for(int x = 0;x<allt;++x)
{
int id = ID.data(Qt::EditRole).toInt();
if(combobx->currentText() == fulname)
model->setData(index,id);
}
} else
}
private:
int allt,alls;
};
#endif // DELSTRSETTABLE
#ifndef DELSTRSETTABLE
#define DELSTRSETTABLE
#include <QtWidgets>
#include <QtSql>
class delStrSetTable : public QSqlRelationalDelegate
{
Q_OBJECT
public:
delStrSetTable(QObject *parent=0):QSqlRelationalDelegate(parent){
QString strg("SELECT trID, trName FROM allTch WHERE trNo>0");
tch = new QSqlQueryModel();
tch->setQuery(strg);
allt = tch->rowCount();
QString str2("SELECT strID, strName FROM tblstreams WHERE strNo>0");
str = new QSqlQueryModel();
str->setQuery(str2);
alls = str->rowCount();
font2.setFamily(QStringLiteral("Arial Narrow"));
font2.setPointSize(10);
}
virtual QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
// ComboBox ony in column 4
if(index.column() == 3) {
// Create the combobox on stream column and populate it
QComboBox *sbCb = new QComboBox(parent);
sbCb->setModel(str);
sbCb->setModelColumn(1);
sbCb->setFont(font2);
sbCb->setFrame(false);
return sbCb;
// Create the combobox on teacher column and populate it
} else if (index.column() == 4) {
QComboBox *tchCb = new QComboBox(parent);
tchCb->setModel(tch);
tchCb->setModelColumn(1);
tchCb->setFont(font2);
tchCb->setFrame(false);
return tchCb;
}
else
return QSqlRelationalDelegate::createEditor(parent, option, index);
}
void setEditorData(QWidget *editor, const QModelIndex &index) const
{
if(index.column()==3)
{ QComboBox *combobox = qobject_cast<QComboBox*>(editor);
Q_ASSERT(combobox);
for(int x = 0;x<alls;++x)
{
QModelIndex ID = str->index(x,0);
QModelIndex name = str->index(x,1);
QString fulname = QString(name.data(Qt::DisplayRole).toString());
int id = ID.data().toInt();
if(index.data().toInt() == id){
int cbIndex = combobox->findText(fulname);
if(cbIndex >= 0)
combobox->setCurrentIndex(cbIndex);
}
}
}
else if(index.column()==4)
{ QComboBox *combobox = qobject_cast<QComboBox*>(editor);
Q_ASSERT(combobox);
for(int x = 0;x<allt;++x)
{
QModelIndex ID = tch->index(x,0);
QModelIndex name = tch->index(x,1);
QString fulname = QString(name.data(Qt::DisplayRole).toString());
int id = ID.data().toInt();
if(index.data().toInt() == id){
int cbIndex = combobox->findText(fulname);
if(cbIndex >= 0)
combobox->setCurrentIndex(cbIndex); }
}
} else {
QSqlRelationalDelegate::setEditorData(editor, index);
}
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if(index.column()==3)
{ QComboBox *combbox = qobject_cast<QComboBox*>(editor);
Q_ASSERT(combbox);
for(int x = 0;x<alls;++x)
{
QModelIndex ID = str->index(x,0);
QModelIndex name = str->index(x,1);
QString fulname = QString(name.data().toString());
int id = ID.data().toInt();
if(combbox->currentText() == fulname)
model->setData(index,id);
}
}
else if(index.column()==4)
{
QComboBox *combobx = qobject_cast<QComboBox*>(editor);
Q_ASSERT(combobx);
for(int x = 0;x<allt;++x)
{
QModelIndex ID = tch->index(x,0);
QModelIndex name = tch->index(x,1);
QString fulname = QString(name.data().toString());
int id = ID.data(Qt::EditRole).toInt();
if(combobx->currentText() == fulname)
model->setData(index,id);
}
} else
QSqlRelationalDelegate::setModelData(editor, model, index);
}
private:
QSqlQueryModel *tch,*str;
int allt,alls;
QFont font2;
};
#endif // DELSTRSETTABLE
To copy to clipboard, switch view to plain text mode
Bookmarks