Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] QSignalMapper and QStandardItemModel



  • Hello everyone!

    I need to link a button click signal to add a row in my QStandardItemModel and I am trying to do this way, but It's not going OK.

    @
    void MainWindow::on_pushButtonNovoInvestimento_clicked()
    {
    QPushButton *pushButtonAdicionarLancamentoInvestimento = new QPushButton;
    pushButtonAdicionarLancamentoInvestimento->setText("Adicionar lançamento");

    QTableView *tableViewLancamentosInvestimento = new QTableView;
    QStandardItemModel *modeloLancamentosInvestimento = new QStandardItemModel(0, 2, this);
    modeloLancamentosInvestimento->setHeaderData(0, Qt::Horizontal, "Valor");
    modeloLancamentosInvestimento->setHeaderData(1,Qt::Horizontal, "Ações");
    tableViewLancamentosInvestimento->setModel(modeloLancamentosInvestimento);
    
    QSignalMapper *mapper = new QSignalMapper;
    connect(pushButtonAdicionarLancamentoInvestimento, SIGNAL(clicked()), mapper, SLOT(map()));
    mapper->setMapping(pushButtonAdicionarLancamentoInvestimento, modeloLancamentosInvestimento);
    connect(mapper, SIGNAL(mapped(QObject*)), this, SLOT(on_pushButtonAdicionarLancamentoInvestimento_clicked(QObject*)));
    

    }

    void MainWindow::on_pushButtonAdicionarLancamentoInvestimento_clicked(QObject *modeloLancamentosInvestimento)
    {
    QStandardItemModel *modelo = modeloLancamentosInvestimento;
    }
    @

    How can I do that?

    Thanks a lot.


  • Lifetime Qt Champion

    Hi,

    Each time this slot is called you are creating everything anew.

    Please, take some time to look at Qt's documentation examples of the Model View chapter, it will give you a good base to start



  • I start the reading, but could you help me with this code?

    Cheers



  • SOLVED
    @
    void MainWindow::on_pushButtonAdicionarLancamentoInvestimento_clicked(QObject *modelo)
    {
    QStandardItemModel *modeloLancamentosInvestimento = qobject_cast<QStandardItemModel *>(modelo);
    modeloLancamentosInvestimento->insertRow(modeloLancamentosInvestimento->rowCount());
    }
    @


Log in to reply