PDA

View Full Version : [SOLVED] QSignalMapper and QStandardItemModel



guidupas
17th April 2014, 20:23
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(pushButtonAdicionarLancamentoInvestimen to, modeloLancamentosInvestimento);
connect(mapper, SIGNAL(mapped(QObject*)), this, SLOT(on_pushButtonAdicionarLancamentoInvestimento_ clicked(QObject*)));
}

void MainWindow::on_pushButtonAdicionarLancamentoInvest imento_clicked(QObject *modeloLancamentosInvestimento)
{
QStandardItemModel *modelo = modeloLancamentosInvestimento;
}


How can I do that?

Thanks a lot

anda_skoa
17th April 2014, 21:50
Wouldn't it be easier to just have the model as a member of MainWindow?

Cheers,
_

guidupas
17th April 2014, 23:11
Thank you for the reply.

This model can be deleted, so with I have a model as a member of a MainWIndow it can be deleted and my program crashs

I need to know to do something like the code above.

Cheers

Added after 1 8 minutes:

Solved



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(pushButtonAdicionarLancamentoInvestimen to, modeloLancamentosInvestimento);
connect(mapper, SIGNAL(mapped(QObject*)), this, SLOT(on_pushButtonAdicionarLancamentoInvestimento_ clicked(QObject*)));
}

void MainWindow::on_pushButtonAdicionarLancamentoInvest imento_clicked(QObject *modeloLancamentosInvestimento)
{
QStandardItemModel *modeloLancamentosInvestimento = qobject_cast<QStandardItemModel *>(modelo);
modeloLancamentosInvestimento->insertRow(modeloLancamentosInvestimento->rowCount());
}

anda_skoa
18th April 2014, 08:33
This model can be deleted, so with I have a model as a member of a MainWIndow it can be deleted and my program crashs

QPointer

And you have the same problem with the signal mapper.

Cheers,
_

stampede
18th April 2014, 08:38
You have a memory leak, each on_pushButtonNovoInvestimento_clicked() call creates QPushButton, QTableView and QSignalMapper without a parent and never deletes them.
btw. I think you have the longest variable names I've ever seen :D

guidupas
22nd April 2014, 13:29
KKKKK
Yes, its a long variable name. I like it with a good specifications.

About the memory leak, I the function above to delete the itens

Is this enough?



void MainWindow::fecharAbasInvestimento(int index)
{
QWidget *tabConteudo = this->tabWidgetInvestimento->widget(index);

this->tabWidgetInvestimento->removeTab(index);

delete (tabConteudo);
}