[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");
    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


    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?


    void MainWindow::on_pushButtonAdicionarLancamentoInvestimento_clicked(QObject *modelo)
    QStandardItemModel *modeloLancamentosInvestimento = qobject_cast<QStandardItemModel *>(modelo);

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.