Example #1
0
QWidget *MySqlRelationDelegate::createEditor(QWidget *aParent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());
    QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
    if (!childModel)
    {
        if (HandbookDialog::m_record.fieldName(index.column()).contains("IS_", Qt::CaseSensitive))
        {
            QCheckBox * checkBox = new QCheckBox(aParent);
            checkBox->setChecked(index.data().toInt() > 0);
            return checkBox;
        }
        else if (HandbookDialog::m_record.fieldName(index.column()).contains("DATE_", Qt::CaseSensitive))
        {
            QDateEdit * dateEdit = new QDateEdit(aParent);
            dateEdit->setDate(QDate::fromString(index.data().toString(), "yyyy-MM-dd"));
            return dateEdit;
        }
        else
        {
            return QItemDelegate::createEditor(aParent, option, index);
        }
    }

    QComboBox *combo = new QComboBox(aParent);
    combo->setModel(childModel);
    combo->setModelColumn(childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn()));
    combo->installEventFilter(const_cast<MySqlRelationDelegate *>(this));

    return combo;
}
QWidget *mySqlRelationalDelegate::createEditor(QWidget *aParent, const QStyleOptionViewItem &option, const QModelIndex &index) const {

    const QSqlRelationalTableModel *sqlModel = qobject_cast<const QSqlRelationalTableModel *>(index.model());
    QSqlTableModel *childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;

    if (!childModel )
    {
        const QSortFilterProxyModel* proxyModel = qobject_cast<const QSortFilterProxyModel *>(index.model());
        if (proxyModel)
        {
            sqlModel = qobject_cast<const QSqlRelationalTableModel *>(proxyModel->sourceModel());
            childModel = sqlModel ? sqlModel->relationModel(index.column()) : 0;
        }
    }

    if (!childModel)
    {
        return QItemDelegate::createEditor(aParent, option, index);
    }

    QComboBox *combo = new QComboBox(aParent);
    combo->setModel(childModel);
    combo->setModelColumn(childModel->fieldIndex(sqlModel->relation(index.column()).displayColumn()));
    combo->installEventFilter(const_cast<mySqlRelationalDelegate *>(this));

    return combo;

}
void PretrazivanjeSredstavaD1::setModelGodina()
{
	QComboBox *view = ui->cbGodina;
	modelGodina = new QSqlQueryModel();
	modelGodina->setQuery("select 'Sve' union select distinct(substr(datum, -4)) as godina from spisak order by godina;");
	view->setModel(modelGodina);
	view->setModelColumn(0);
	view->setCurrentIndex(view->findText("Sve"));
}
void PretrazivanjeSredstavaD1::setModelSredstva()
{
	QComboBox *view = ui->cbSredstva;
	modelSredstva = new QSqlQueryModel();
	modelSredstva->setQuery("select -1 as A, 'Svi' as B union select * from artikal;");
	view->setModel(modelSredstva);
	view->setModelColumn(1);
	view->setCurrentIndex(view->findText("Svi"));
}
QWidget *wComboBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */) const
{
    QComboBox *editor = new QComboBox(parent);
    editor->setModel(modelData);
    editor->setModelColumn(modelIndexValue);

    return editor;
}
int TransactionActionDialog::m_addTrans()
{
    // Get last row index
    int currRow = ui->tableWidget->rowCount();

    // Insert row after current row
    ui->tableWidget->insertRow(currRow);

    //ui->tableWidget->item(currRow, ID)->setData(Qt::DisplayRole, QVariant(-1));

    auto* plusMinusWidget = new PlusMinusPushWidget(ui->tableWidget);
    // Set to minus by default
    plusMinusWidget->click();
    // set widget in to Positive/Negative row
    ui->tableWidget->setCellWidget(currRow, POS_NEG, plusMinusWidget);

    // Create combo box that links to stock table model
    // and add to row and column
    QComboBox* comboBox = new QComboBox(ui->tableWidget);
    comboBox->setModel(m_stockTable);
    comboBox->setModelColumn(StockTable::REF);
    // add combo box in to stock item column
    ui->tableWidget->setCellWidget(currRow, STOCK_ITEM, comboBox);

    // create line edit, default to 0 value
    QLineEdit* lineEdit = new QLineEdit(QString::number(0), ui->tableWidget);
    // set numeric regex as validator
    lineEdit->setValidator(DB::GetNumericValidator(lineEdit));

    // set line edit in to amount column
    ui->tableWidget->setCellWidget(currRow, AMOUNT,
               lineEdit);

    // create new line edit containing number of stock data
    lineEdit = new QLineEdit(QString::number(0), ui->tableWidget);
    // set validator as non negative regex
    lineEdit->setValidator(DB::GetNonNegativeNumericValidator(lineEdit));
    // set the line edit widget in to the no sold column
    ui->tableWidget->setCellWidget(currRow, NO_SOLD,
               lineEdit);

    // create line edit, add id number and insert in to id column
    ui->tableWidget->setCellWidget(currRow, ID,
               new QLineEdit(QString::number(-1), ui->tableWidget));

    // create line edit, add deleted mark and insert in to deleted column
    ui->tableWidget->setCellWidget(currRow, DELETED,
               new QLineEdit(QString::number(0), ui->tableWidget));

    // return new row index to caller
    return currRow;
}
void PretrazivanjeSredstavaD1::setModelOrgJedinica()
{
	QComboBox *view = ui->cbOrgJedinica;
	modelOrgJedinica = new QSqlQueryModel();

	if(!tipNaloga.compare("rukovodilac"))
	{
		modelOrgJedinica->setQuery("select o.id_ogranka, o.naziv \
									from ogranak o join nalog n on o.id_ogranka = n.id_ogranka \
									where n.id = " + QString("%1").arg(_userId) + ";");
		view->setModel(modelOrgJedinica);
		view->setModelColumn(1);
		view->setDisabled(true);
	}
// Предоставление редактора
QWidget*  ComboBoxFileldDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option,
												 const QModelIndex & index ) const
{
	if (index.column()>2){

		// QMessageBox::critical(0,"",index.model()->data(index.sibling(index.row(),0)).toString());
		QString pole = index.model()->data(index.sibling(index.row(),0)).toString();
		int j;
		for(int i=0;i<fieldName.count();i++){
				QString s = model->headerData( fieldName.at(i) , Qt::Horizontal).toString();
				s.replace("\n"," ");
				if (s==pole){
					j=fieldName.at(i);
					break;
				}
		}

		//QMessageBox::critical(0,"",QString("%1").arg(j));
		QSqlRelation rel =  model->relation(j);
		if (rel.indexColumn()!=QString("")){
			//QMessageBox::critical(0,"",rel.indexColumn()+" "+rel.displayColumn()+" "+rel.tableName());
			QComboBox * pRes = new QComboBox(parent);
			QSqlTableModel* relModel = new QSqlTableModel;

			relModel->setTable(rel.tableName());
			relModel->select();

			pRes->setModel(relModel);
			pRes->setModelColumn(relModel->fieldIndex(rel.displayColumn()));

			return pRes;

		}

		if (model->data(model->index(0,j), Qt::EditRole).type() == QVariant::Date){
			QDateEdit* pRes = new  QDateEdit(parent);
			pRes->setCalendarPopup(true);
			pRes->setDisplayFormat("dd.MM.yyyy");
			return pRes;
		}

		if (model->data(model->index(0,j), Qt::EditRole).type() == QVariant::Bool){
			QComboBox * pRes = new QComboBox(parent);

			pRes->addItem(tr("Нет"));
			pRes->addItem(tr("Да"));
			return pRes;
		}


		return QItemDelegate::createEditor(parent,option,index);
	}

	QComboBox * pRes = new QComboBox(parent);

	switch (index.column()) {
		case 0: {

			QStringList field;
			for(int i=0;i<fieldName.count();i++){
				QString s = model->headerData( fieldName.at(i) , Qt::Horizontal).toString();
				s.replace("\n"," ");
				field<<s;
			}
			pRes->addItems(field);
			break;
		}
		case 1: {
			pRes->addItem(tr(" "));
			pRes->addItem(tr("не"));
			break;
		}
		case 2: {
			pRes->addItem(tr("равно"));
			pRes->addItem(tr("похоже на"));
			pRes->addItem(tr("больше"));
			pRes->addItem(tr("меньше"));
			break;
		}

	}

	// это строка нужна для того чтобы по enter и esc завершалось редактирование итд
	pRes->installEventFilter(const_cast<ComboBoxFileldDelegate*>(this));

	return pRes;

};
Example #9
0
void importDialog::afterCreateGroup()
{
    ui->stackedWidget->setCurrentIndex(ui->stackedWidget->currentIndex()+1);
    ui->btnImportar->setEnabled(true);

    MainWindow w(_empDir + "/Divisas.dbf");
    QSqlQuery q(QSqlDatabase::database("dbfEditor"));
    if(!q.exec("Select * from d_Divisas"))
    {
        QMessageBox::critical(this,"Error",q.lastError().text());
        return;
    }
    q.first();

    QSqlQueryModel * modelMoneda = new QSqlQueryModel(this);
    modelMoneda->setQuery("SELECT * FROM monedas;",QSqlDatabase::database("grupo"));

    QSqlQueryModel * modelPais = new QSqlQueryModel(this);
    modelPais->setQuery("SELECT * FROM paises;",QSqlDatabase::database("grupo"));

    QWidget* container = new QWidget(this);
    QVBoxLayout * _layout = new QVBoxLayout(container);
    do
    {
        QSqlRecord r = q.record();
        QString codigo = r.value("CCODDIV").toString().trimmed();
        QString desc = r.value("CDETDIV").toString().trimmed();

        QComboBox * combo = new QComboBox(this);
        combo->setModel(modelMoneda);
        combo->setModelColumn(1);

        QComboBox * combo2 = new QComboBox(this);
        combo2->setModel(modelPais);
        combo2->setModelColumn(1);

        QLabel * label = new QLabel(desc,this);

        QHBoxLayout * lay = new QHBoxLayout(this);

        QCheckBox * check = new QCheckBox(this);
        check->setChecked(true);

        lay->addWidget(check);
        lay->addWidget(label);
        lay->addWidget(combo);
        lay->addWidget(combo2);

        _layout->addLayout(lay);
        _combos.insert(combo,codigo);
        _combosMonedaPais.insert(combo2,codigo);
        _validDivisas.insert(combo,check);
        combo->setCurrentIndex(0);

    }while(q.next());
    _layout->addSpacerItem(new QSpacerItem(1,1,QSizePolicy::Preferred,QSizePolicy::Expanding));
    container->setLayout(_layout);
    ui->scrollArea->setWidget(container);

    w.openDb(_empDir + "/Ivas.dbf");
    if(!q.exec("Select * from d_Ivas"))
    {
        QMessageBox::critical(this,"Error",q.lastError().text());
        return;
    }
    q.first();

    QSqlQueryModel * q2 = new QSqlQueryModel(this);
    q2->setQuery("SELECT * FROM tiposiva;",QSqlDatabase::database("grupo"));

    QWidget* container2 = new QWidget(this);
    QVBoxLayout * _layout2 = new QVBoxLayout(container);
    do
    {
        QSqlRecord r = q.record();
        QString codigo = r.value("CTIPOIVA").toString().trimmed();
        QString desc = r.value("CDETIVA").toString().trimmed();

        QComboBox * combo = new QComboBox(this);
        combo->setModel(q2);
        combo->setModelColumn(2);

        QLabel * label = new QLabel(desc,this);

        QHBoxLayout * lay = new QHBoxLayout(this);

        lay->addWidget(label);
        lay->addWidget(combo);

        _layout2->addLayout(lay);
        _combos2.insert(combo,codigo);
        combo->setCurrentIndex(0);

    }while(q.next());
    _layout2->addSpacerItem(new QSpacerItem(1,1,QSizePolicy::Preferred,QSizePolicy::Expanding));
    container2->setLayout(_layout2);
    ui->scrollIva->setWidget(container2);

    w.openDb(ui->txtRutaBD->text() + "/dbf/Naciones.dbf");
    if(!q.exec("Select * from d_Naciones"))
    {
        QMessageBox::critical(this,"Error",q.lastError().text());
        return;
    }
    q.first();

    QWidget* container3 = new QWidget(this);
    QVBoxLayout * _layout3 = new QVBoxLayout(container);
    do
    {
        QSqlRecord r = q.record();
        QString codigo = r.value("CCODIGO").toString().trimmed();
        QString desc = r.value("CNOMBRE").toString().trimmed();

        QComboBox * combo = new QComboBox(this);
        combo->setModel(modelPais);
        combo->setModelColumn(1);

        QLabel * label = new QLabel(desc,this);

        QHBoxLayout * lay = new QHBoxLayout(this);

        lay->addWidget(label);
        lay->addWidget(combo);

        _layout3->addLayout(lay);
        _combos3.insert(combo,codigo);
        combo->setCurrentIndex(0);

    }while(q.next());
    _layout3->addSpacerItem(new QSpacerItem(1,1,QSizePolicy::Preferred,QSizePolicy::Expanding));
    container3->setLayout(_layout3);
    ui->scrollPaises->setWidget(container3);
}
Example #10
0
// Предоставление редактора
QWidget*  ComboBoxMailDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
        const QModelIndex& index) const
{

    if (index.column() == 1) {

        // QMessageBox::critical(0,"",index.model()->data(index.sibling(index.row(),0)).toString());
        QString pole = index.model()->data(index.sibling(index.row(), 0)).toString();
        int j;
        for (int i = 0; i < fieldName.count(); i++) {
            QString s = model->headerData(fieldName.at(i) , Qt::Horizontal).toString();
            s.replace("\n", " ");

            if (s == pole) {
                j = fieldName.at(i);
                break;
            }
        }

        //QMessageBox::critical(0,"",QString("%1").arg(j));
        QSqlRelation rel =  model->relation(j);
        if (rel.indexColumn() != QString("")) {
            //QMessageBox::critical(0,"",rel.indexColumn()+" "+rel.displayColumn()+" "+rel.tableName());
            QComboBox* pRes = new QComboBox(parent);
            QSqlTableModel* relModel = new QSqlTableModel;
            relModel->setTable(rel.tableName());

            /* Удаление выбранных значений в кому */
            if (pole == tr("Кому")) {
                QString relFilter = QString("user_id != '00000000-0000-0000-0000-000000000000'");
                for (int i = 0; i < index.model()->rowCount(); i++) {

                    QString pole = index.model()->data(index.sibling(i, 0)).toString();

                    if (pole == tr("Кому") && index.row() != i) {
                        QString val = index.model()->data(index.sibling(i, 1)).toString();
                        if ( val != QString(""))
                            relFilter = QString("%1 and not user_id = '%2'").arg(relFilter).arg(val);
                    }
                    //QMessageBox::critical(0,"",index.model()->data(index.sibling(i,1)).toString());
                }
                //QMessageBox::critical(0,"","-"+relFilter+"-");
                relModel->setFilter(relFilter);
            }

            relModel->select();

            pRes->setModel(relModel);
            pRes->setModelColumn(relModel->fieldIndex(rel.displayColumn()));

            return pRes;

        }

        if (model->data(model->index(0, j)).type() == QVariant::Date) {
            QDateEdit* pRes = new  QDateEdit(parent);
            pRes->setCalendarPopup(true);
            pRes->setDisplayFormat("dd.MM.yyyy");
            return pRes;
        }

        if (model->data(model->index(0, j)).type() == QVariant::Bool) {
            QComboBox* pRes = new QComboBox(parent);

            pRes->addItem(tr("Нет"));
            pRes->addItem(tr("Да"));
            return pRes;
        }


        return QItemDelegate::createEditor(parent, option, index);
    }

    if (index.column() == 0) {
        QComboBox* pRes = new QComboBox(parent);

        bool typeflag       = true;
        bool priorflag      = true;
        bool recipientflag  = true;
        bool beginflag  = true;
        bool endflag  = true;

        for (int i = 0; i < index.model()->rowCount(); i++) {

            QString pole = index.model()->data(index.sibling(i, 0)).toString();

            if (pole == tr("Тип") && index.row() != i)
                typeflag = false;
            if (pole == tr("Приоритет") && index.row() != i)
                priorflag = false;
            if (pole == tr("Начало") && index.row() != i)
                beginflag = false;
            if (pole == tr("Конец") && index.row() != i)
                endflag = false;
        }

        //if (recipientflag)
        pRes->addItem(tr("Кому"));

        if (typeflag)
            pRes->addItem(tr("Тип"));
        if (priorflag)
            pRes->addItem(tr("Приоритет"));
        if (beginflag)
            pRes->addItem(tr("Начало"));
        if (endflag)
            pRes->addItem(tr("Конец"));

        //pRes->addItem(tr("Копия"));

        // это строка нужна для того чтобы по enter и esc завершалось редактирование итд
        pRes->installEventFilter(const_cast<ComboBoxMailDelegate*>(this));

        return pRes;
    }


    return QItemDelegate::createEditor(parent, option, index);

};
void TransactionActionDialog::setData(const QVector<TransactionTable::Data> &data)
{
    for(const TransactionTable::Data& item: data)
    {
        // Add new transaction
        int currRow = ui->tableWidget->rowCount();

        // Insert row after current row
        ui->tableWidget->insertRow(currRow);

        // Create new plus minus widget
        PlusMinusPushWidget* plusMinusWidget = new PlusMinusPushWidget(
                    ui->tableWidget);

        // Create temporary variable to hold the item amount.
        // This is due to the const restrictions, we can not modify the value
        // directly
        int tempNoStock = item.noSold;

        // Check if value is negative
        if(tempNoStock < 0)
        {
            // Click the button so that its set to -
            plusMinusWidget->click();
            // Make value positive
            tempNoStock *= -1;
        }


        // Set the plus minus widget to the table
        ui->tableWidget->setCellWidget(currRow, POS_NEG, plusMinusWidget);

        // Create combo box that links to stock table model
        // and add to row and column
        QComboBox* comboBox = new QComboBox(ui->tableWidget);
        comboBox->setModel(m_stockTable);
        // show the reference column
        comboBox->setModelColumn(StockTable::REF);

        // get view index from model
        int stock_index = DB::GetViewIndex(item.stock_id,
                         static_cast<QSqlQueryModel*>(m_stockTable));

        // Set index of row to view index we found earlier
        comboBox->setCurrentIndex(stock_index);
        // Set widget to table
        ui->tableWidget->setCellWidget(currRow, STOCK_ITEM, comboBox);

        // create new line edit containing amount data
        QLineEdit* lineEdit = new QLineEdit(QString::number(item.amount, 'd', 2), ui->tableWidget);
        // set numeric values only regex
        lineEdit->setValidator(DB::GetNumericValidator(lineEdit));

        // set line edit in to amount column
        ui->tableWidget->setCellWidget(currRow, AMOUNT,
                   lineEdit);

        // create new line edit containing number of stock data
        lineEdit = new QLineEdit(QString::number(tempNoStock), ui->tableWidget);
        // set validator as non negative regex
        lineEdit->setValidator(DB::GetNonNegativeNumericValidator(lineEdit));

        // set the line edit widget in to the no sold column
        ui->tableWidget->setCellWidget(currRow, NO_SOLD, lineEdit);

        // create line edit, add id number and insert in to id column
        ui->tableWidget->setCellWidget(currRow, ID,
                   new QLineEdit(QString::number(item.id), ui->tableWidget));

        // create line edit, add deleted mark and insert in to deleted column
        ui->tableWidget->setCellWidget(currRow, DELETED,
                   new QLineEdit(QString::number(item.deleted, 'd', 2), ui->tableWidget));
    }
}