Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with a QCombobox on relational table
Forum Updated to NodeBB v4.3 + New Features

Problem with a QCombobox on relational table

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arsinte_andrei
    wrote on last edited by
    #1

    I have in my form a QCombobox that is displaying duplicates and also is not working properly
    what i've done so far:

    • module_materials.h
      @#ifndef MODULE_MATERIALS_H
      #define MODULE_MATERIALS_H

    #include <QDialog>
    #include "atp_db.h"
    #include <QSqlRelationalTableModel>
    #include <QSortFilterProxyModel>

    namespace Ui {
    class Module_Materials;
    }

    class Module_Materials : public QDialog
    {
    Q_OBJECT

    public:
    explicit Module_Materials(QWidget *parent = 0);
    ~Module_Materials();

    private slots:
    void on_buttonOk_clicked();

    void on_buttonNew_clicked();

    void on_buttonDelete_clicked();

    void on_buttonEdit_clicked();

    void on_buttonCancel_clicked();

    void on_editSearch_textChanged(const QString &arg1);

    void on_tableMaterials_clicked(const QModelIndex &index);

    private:
    Ui::Module_Materials *ui;

    void diesableAll();
    void enableAll();
    void updateDb(QSqlRelationalTableModel *model);
    ATP_db *db;
    QMap<QString, QVariant> *data;
    QSqlRelationalTableModel *model;
    QSortFilterProxyModel *proxyModel;
    int matId;
    };

    #endif // MODULE_MATERIALS_H@

    • module_materials.cpp
      @#include "module_materials.h"
      #include "ui_module_materials.h"
      #include <QSqlRelationalDelegate>

    Module_Materials::Module_Materials(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Module_Materials)
    {
    ui->setupUi(this);

    db = new ATP_db;
    data = new QMap<QString, QVariant>;
    model = new QSqlRelationalTableModel;
    updateDb(model);

    this->setFixedSize(760, 445);
    diesableAll();
    ui->buttonEdit->setEnabled(false);
    ui->buttonDelete->setEnabled(false);

    ui->comboSupplier->setModel(model);
    ui->comboSupplier->setEnabled(true);
    ui->comboSupplier->setModelColumn(4);
    ui->comboSupplier->setDuplicatesEnabled(false);
    // mapper = new QDataWidgetMapper(this);
    // mapper->setModel(model);
    // mapper->setItemDelegate(new Delegate(this));
    // mapper->addMapping(nameEdit, 0);
    // mapper->addMapping(addressEdit, 1);
    // mapper->addMapping(typeComboBox, 2);
    }

    Module_Materials::~Module_Materials()
    {
    delete ui;
    }

    void Module_Materials::enableAll() {
    ui->editName->setEnabled(true);
    ui->editBuy->setEnabled(true);
    ui->editSale->setEnabled(true);
    ui->comboSupplier->setEnabled(true);
    ui->editSearch->setEnabled(false);
    }

    void Module_Materials::diesableAll() {
    ui->editName->setEnabled(false);
    ui->editBuy->setEnabled(false);
    ui->editSale->setEnabled(false);
    ui->comboSupplier->setEnabled(false);
    ui->editSearch->setEnabled(true);
    }

    void Module_Materials::updateDb(QSqlRelationalTableModel *model) {
    model->setTable("materiale_tbl");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->setRelation(4, QSqlRelation("supplier_tbl", "supplier_id", "supplier_name"));
    model->setHeaderData(0, Qt::Horizontal,"Id");
    model->setHeaderData(1, Qt::Horizontal,"Name");
    model->setHeaderData(2, Qt::Horizontal,"Buy");
    model->setHeaderData(3, Qt::Horizontal,"Sale");
    model->setHeaderData(4, Qt::Horizontal,"Supplier");
    model->select();

    proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel(model);
    proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    ui->tableMaterials->setModel(proxyModel);
    ui->tableMaterials->setSortingEnabled(true);
    ui->tableMaterials->setItemDelegate(new QSqlRelationalDelegate(ui->tableMaterials));
    ui->tableMaterials->setColumnHidden(0, true);
    ui->tableMaterials->setColumnWidth(1, 300);
    ui->tableMaterials->setColumnWidth(2, 100);
    ui->tableMaterials->setColumnWidth(3, 100);
    ui->tableMaterials->setColumnWidth(4, 150);

    ui->tableMaterials->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableMaterials->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableMaterials->setAlternatingRowColors(true);

    //ui->companyTableView->verticalHeader()->setStyleSheet("QHeaderView::section:checked {"
    // " background-color: black; }"
    // " background-image: url(imgs/arrow.png)"
    // );
    }

    void Module_Materials::on_buttonOk_clicked(){
    this->accept();
    }

    void Module_Materials::on_buttonNew_clicked(){

    }

    void Module_Materials::on_buttonDelete_clicked(){

    }

    void Module_Materials::on_buttonEdit_clicked(){

    }

    void Module_Materials::on_buttonCancel_clicked(){
    this->reject();
    }

    void Module_Materials::on_editSearch_textChanged(const QString &arg1){
    int columnToSort = proxyModel->sortColumn();
    if (columnToSort < 1) {
    columnToSort = 1;
    }
    proxyModel->setFilterKeyColumn(columnToSort);
    proxyModel->setFilterFixedString(arg1);
    }

    void Module_Materials::on_tableMaterials_clicked(const QModelIndex &index){
    int rand = index.row();
    QString mat_id = ui->tableMaterials->model()->data(ui->tableMaterials->model()->index(rand,0)).toString();
    QString mat_name = ui->tableMaterials->model()->data(ui->tableMaterials->model()->index(rand,1)).toString();
    QString mat_price1 = ui->tableMaterials->model()->data(ui->tableMaterials->model()->index(rand,2)).toString();
    QString mat_price2 = ui->tableMaterials->model()->data(ui->tableMaterials->model()->index(rand,3)).toString();
    QString mat_supplier_id = ui->tableMaterials->model()->data(ui->tableMaterials->model()->index(rand,4)).toString();

    ui->editName->setText(mat_name);
    ui->editBuy->setText(mat_price1);
    ui->editSale->setText(mat_price2);

    matId = mat_id.toInt();
    if (matId > 0) {
    ui->buttonEdit->setEnabled(true);
    ui->buttonDelete->setEnabled(true);
    } else {
    ui->buttonEdit->setEnabled(false);
    ui->buttonDelete->setEnabled(false);
    }
    // qDebug() << index.column();
    }@

    how it behave...

    the combobox is showing all items in duplicate acourding to how many they are in the main table (where is accepted duplicate) but not according to the source table from where it take the data where is only ID and field name with no duplicate.

    another problem I can not use it to add the id (only) to the main table file.

    is not changing the combo value when i click on different row in the tableView

    Thanks for help

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved