May be I can use QSqlTableModel but in that case also still I have to run the query in Delegate class ComboBoxDelegate and I have to create one more connection to database which I want to avoid.
Is there any way to get ComboBox in UI class AddBankTrxns? If I can get ComboBox in this class then it is straightforward to populate it as already db connection is available in this class.
I have pasted code here for reference:
Class : AddBankTrxns
AddBankTrxns
::AddBankTrxns(QWidget *parent
) : ui(new Ui::AddBankTrxns)
{
connectionName = "addbanktrxns";
loggedInUserid = 0;
ui->setupUi(this);
headerLabels.append("Trxn Date");
headerLabels.append("Trxn Description");
headerLabels.append("Cheque No.");
headerLabels.append("Trxn Type");
headerLabels.append("Amount");
headerLabels.append("Trxn Category");
headerLabels.append("Remarks");
model->setHorizontalHeaderLabels(headerLabels);
ui->bankTrxnsView->setModel(model);
addTransactionRow(0);
setDBConnection();
}
void AddBankTrxns::addTransactionRow(int row){
model->insertRow(row);
ui->bankTrxnsView->setModel(model);
ui->bankTrxnsView->setItemDelegateForColumn(0, new DateEditDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(1, new LineEditDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(2, new LineEditDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(3, new ComboBoxDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(4, new LineEditDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(5, new LineEditDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(6, new LineEditDelegate());
ui->bankTrxnsView->scrollToBottom();
ui->bankTrxnsView->show();
setRowItemWidgets(row);
// qDebug() << "After:rowcount=" << model->rowCount();
}
AddBankTrxns::AddBankTrxns(QWidget *parent) :
QWidget(parent),
ui(new Ui::AddBankTrxns)
{
connectionName = "addbanktrxns";
loggedInUserid = 0;
ui->setupUi(this);
headerLabels.append("Trxn Date");
headerLabels.append("Trxn Description");
headerLabels.append("Cheque No.");
headerLabels.append("Trxn Type");
headerLabels.append("Amount");
headerLabels.append("Trxn Category");
headerLabels.append("Remarks");
model = new QStandardItemModel(0, k_BANK_TRNX_COL_CNT);
model->setHorizontalHeaderLabels(headerLabels);
ui->bankTrxnsView->setModel(model);
addTransactionRow(0);
setDBConnection();
}
void AddBankTrxns::addTransactionRow(int row){
model->insertRow(row);
ui->bankTrxnsView->setModel(model);
ui->bankTrxnsView->setItemDelegateForColumn(0, new DateEditDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(1, new LineEditDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(2, new LineEditDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(3, new ComboBoxDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(4, new LineEditDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(5, new LineEditDelegate());
ui->bankTrxnsView->setItemDelegateForColumn(6, new LineEditDelegate());
ui->bankTrxnsView->scrollToBottom();
ui->bankTrxnsView->show();
setRowItemWidgets(row);
// qDebug() << "After:rowcount=" << model->rowCount();
}
To copy to clipboard, switch view to plain text mode
Class : ComboBoxDelegate
ComboBoxDelegate
::ComboBoxDelegate(QObject *parent
) :{
}
{
return editor;
}
void ComboBoxDelegate
::setEditorData(QWidget *editor,
{
// QString value = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
}
{
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
model->setData(index, comboBox->currentText(), Qt::EditRole);
}
void ComboBoxDelegate
::updateEditorGeometry(QWidget *editor,
{
editor->setGeometry(option.rect);
}
ComboBoxDelegate::ComboBoxDelegate(QObject *parent) :
QItemDelegate(parent)
{
}
QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
return editor;
}
void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
// QString value = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
}
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
model->setData(index, comboBox->currentText(), Qt::EditRole);
}
void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
To copy to clipboard, switch view to plain text mode
Bookmarks