Congratulations to our 2022 Qt Champions!

[SOLVED] 2 QTableViews data

  • Hello all!

    I have a code that create 2 QTableViews dynamically when a QPushButton is clicked:

    void MainWindow::on_pushButtonNovoInvestimento_clicked()
    QTableView *tableViewLancamentosInvestimento = new QTableView(boxInvestimento2);
    QTableView *tableViewIndicadoresInvestimento = new QTableView(boxInvestimento2);

    QStandardItemModel *modeloLancamentosInvestimento = new QStandardItemModel(0, 3, tableViewLancamentosInvestimento);
    QStandardItemModel *modeloIndicadoresInvestimento = new QStandardItemModel(0, 2, tableViewIndicadoresInvestimento);

    connect(modeloLancamentosInvestimento, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(alteraModeloLancamentoInvestimento(QModelIndex,QModelIndex)));

    But I need that when data of the first model changes, some data of the second model changes too.

    How can I do that once the 2 models are created dynamically?

    I am trying to do something like the code above, but its not working:
    void MainWindow::alteraModeloLancamentoInvestimento(QModelIndex topLeft, QModelIndex bottomRight)
    QStandardItemModel *modeloLancamentosInvestimento = qobject_cast<QStandardItemModel *>(sender());

    How can I access the second model?

  • I would suggest you just save a reference to the model or the QTableView in your class?!

    The sender of a signal is always, well the sender (the button in your case?) and not the model, unless that signals is emitted from your first model I can't tell from the code.
    Anyway I would still save a reference to that model or table, why not? it's just a simple pointer :)

  • Hey Xander84!

    It worked, thanks a lot.

Log in to reply