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. Add spinbox to a cell and manipulate it individually

Add spinbox to a cell and manipulate it individually

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 275 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.
  • S Offline
    S Offline
    Samuel Ives
    wrote on last edited by
    #1

    I have a program that is used to manage sales and inventory, but I do not know how I can manipulate a spinbox in the cell added by a delegate, I know that programs like qBittorrent do this for example in the list of torrent and each item has its own progress bar

    alt text

    My code:

    #ifndef VENDERPRODUTO_HPP
    #define VENDERPRODUTO_HPP
    
    #include <QItemDelegate>
    #include <QDialog>
    #include "../core/vendas.hpp"
    
    namespace Ui {
    class NovaVenda;
    }
    
    class QCompleter;
    class QStandardItemModel;
    class QRegExpValidator;
    
    class NovaVenda : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit NovaVenda(QWidget *parent = nullptr);
        ~NovaVenda();
    
    private slots:
    
        void on_leInput_returnPressed();
    
        void on_pb_AddCarrinho_clicked();
    
        void on_pbRemoveCarrinho_clicked();
    
        void on_dsbQuantidade_valueChanged(double value);
    
    private:
    
        Ui::NovaVenda *ui;
        QCompleter *nomesCompleter, *codigoCompleter;
        QStandardItemModel *modeloTabela;
        QRegExpValidator *codBarraValidator;
        QHash<QString, ItemVenda> produtos;
        Produtos produto;
        double total;
    };
    
    class TableDelegate : public QItemDelegate{
    
    public:
    
        TableDelegate() = default;
        ~TableDelegate() = default;
    
        QWidget *createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index ) const;
    
    };
    
    #endif // VENDERPRODUTO_HPP
    
    #include <QCompleter>
    #include <QStandardItemModel>
    #include <QMessageBox>
    #include <QRegExpValidator>
    #include "novavenda.hpp"
    #include "ui_novavenda.h"
    #include "../core/produtos.hpp"
    #include "../core/vendas.hpp"
    
    NovaVenda::NovaVenda(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::NovaVenda)
    {
        ui->setupUi(this);
    
    
        static QStringList headers = {"Código", "Nome", "Quantidade"};
    
        modeloTabela = new QStandardItemModel(0, 3);
        modeloTabela->setHorizontalHeaderLabels(headers);
    
        ui->tvCarrinho->setModel(modeloTabela);
        ui->tvCarrinho->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tvCarrinho->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
    
        ui->tvCarrinho->setItemDelegateForColumn(2, new TableDelegate());
    
        // Lista os códigos e nomes para que os campos o auto complete
        QVector<Produtos> produtos = Produtos::listarProdutos();
        if(!produtos.isEmpty()){
    
            QStringList nomes, codigos;
    
            for(int i = 0; i < produtos.size(); i++){
    
                nomes.insert(i, produtos[i].nome());
                codigos.insert(i, produtos[i].codigoVenda());
    
            }
    
            nomesCompleter = new QCompleter(nomes);
            codigoCompleter = new QCompleter(codigos);
    
        }
    
        ui->leInput->setCompleter(codigoCompleter);
    
        codBarraValidator = new QRegExpValidator(QRegExp("^[0-9]*$"));
    
    }
    
    NovaVenda::~NovaVenda()
    {
        delete ui;
    }
    
    
    void NovaVenda::on_leInput_returnPressed()
    {
    
        // Obtem o produto desejado para que seja adicionado no carrinho
        if(ui->rb_CodVenda->isChecked()){
            QVector<Produtos> produtos = Produtos::listarProdutos(FiltroProdutos::CODIGOVENDA, ui->leInput->text());
            produto = produtos[0];
        }else if(ui->rb_CodBarras->isChecked()){
            QVector<Produtos> produtos = Produtos::listarProdutos(FiltroProdutos::CODIGOBARRAS, ui->leInput->text());
            produto = produtos[0];
        }else if(ui->rb_Nome->isChecked()){
            produto = Produtos::produto(ui->leInput->text());
        }
    
    }
    
    void NovaVenda::on_pb_AddCarrinho_clicked()
    {
    
        // Verifica se o item ja nao esta adicionado
        if(!produtos.contains(produto.nome())){
    
            ItemVenda item;
            item.produto = produto;
    
            int row = modeloTabela->rowCount();
            modeloTabela->setItem(row, 0, new QStandardItem(produto.codigoVenda()));
            modeloTabela->setItem(row, 1, new QStandardItem(produto.nome()));
    
        }
    
    }
    
    void NovaVenda::on_pbRemoveCarrinho_clicked()
    {
    
        //Remove o item da tabela
        const QModelIndex index = ui->tvCarrinho->selectionModel()->currentIndex();
        produtos.remove(modeloTabela->data(modeloTabela->index(index.row(), 1)).toString());
        modeloTabela->removeRow(index.row());
    
    }
    
    // Altera a aparencia de uma determinada celula
    QWidget *TableDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index ) const
    {
        QDoubleSpinBox *dsbQuantidade = new QDoubleSpinBox(parent);
        return dsbQuantidade;
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What exact manipulation do you have in mind ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      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