Results 1 to 6 of 6

Thread: [SOLVED] QSignalMapper and QStandardItemModel

  1. #1
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default [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.

    Qt Code:
    1. void MainWindow::on_pushButtonNovoInvestimento_clicked()
    2. {
    3. QPushButton *pushButtonAdicionarLancamentoInvestimento = new QPushButton;
    4. pushButtonAdicionarLancamentoInvestimento->setText("Adicionar lançamento");
    5.  
    6. QTableView *tableViewLancamentosInvestimento = new QTableView;
    7. QStandardItemModel *modeloLancamentosInvestimento = new QStandardItemModel(0, 2, this);
    8. modeloLancamentosInvestimento->setHeaderData(0, Qt::Horizontal, "Valor");
    9. modeloLancamentosInvestimento->setHeaderData(1,Qt::Horizontal, "Ações");
    10. tableViewLancamentosInvestimento->setModel(modeloLancamentosInvestimento);
    11.  
    12. connect(pushButtonAdicionarLancamentoInvestimento, SIGNAL(clicked()), mapper, SLOT(map()));
    13. mapper->setMapping(pushButtonAdicionarLancamentoInvestimento, modeloLancamentosInvestimento);
    14. connect(mapper, SIGNAL(mapped(QObject*)), this, SLOT(on_pushButtonAdicionarLancamentoInvestimento_clicked(QObject*)));
    15. }
    16.  
    17. void MainWindow::on_pushButtonAdicionarLancamentoInvestimento_clicked(QObject *modeloLancamentosInvestimento)
    18. {
    19. QStandardItemModel *modelo = modeloLancamentosInvestimento;
    20. }
    To copy to clipboard, switch view to plain text mode 

    How can I do that?

    Thanks a lot
    Last edited by guidupas; 18th April 2014 at 00:12.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSignalMapper and QStandardItemModel

    Wouldn't it be easier to just have the model as a member of MainWindow?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSignalMapper and QStandardItemModel

    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

    Qt Code:
    1. void MainWindow::on_pushButtonNovoInvestimento_clicked()
    2. {
    3. QPushButton *pushButtonAdicionarLancamentoInvestimento = new QPushButton;
    4. pushButtonAdicionarLancamentoInvestimento->setText("Adicionar lançamento");
    5.  
    6. QTableView *tableViewLancamentosInvestimento = new QTableView;
    7. QStandardItemModel *modeloLancamentosInvestimento = new QStandardItemModel(0, 2, this);
    8. modeloLancamentosInvestimento->setHeaderData(0, Qt::Horizontal, "Valor");
    9. modeloLancamentosInvestimento->setHeaderData(1,Qt::Horizontal, "Ações");
    10. tableViewLancamentosInvestimento->setModel(modeloLancamentosInvestimento);
    11.  
    12. connect(pushButtonAdicionarLancamentoInvestimento, SIGNAL(clicked()), mapper, SLOT(map()));
    13. mapper->setMapping(pushButtonAdicionarLancamentoInvestimento, modeloLancamentosInvestimento);
    14. connect(mapper, SIGNAL(mapped(QObject*)), this, SLOT(on_pushButtonAdicionarLancamentoInvestimento_clicked(QObject*)));
    15. }
    16.  
    17. void MainWindow::on_pushButtonAdicionarLancamentoInvestimento_clicked(QObject *modeloLancamentosInvestimento)
    18. {
    19. QStandardItemModel *modeloLancamentosInvestimento = qobject_cast<QStandardItemModel *>(modelo);
    20. modeloLancamentosInvestimento->insertRow(modeloLancamentosInvestimento->rowCount());
    21. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by guidupas; 18th April 2014 at 00:11.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSignalMapper and QStandardItemModel

    Quote Originally Posted by guidupas View Post
    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,
    _

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: [SOLVED] QSignalMapper and QStandardItemModel

    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

  6. #6
    Join Date
    Feb 2014
    Posts
    94
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [SOLVED] QSignalMapper and QStandardItemModel

    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?

    Qt Code:
    1. void MainWindow::fecharAbasInvestimento(int index)
    2. {
    3. QWidget *tabConteudo = this->tabWidgetInvestimento->widget(index);
    4.  
    5. this->tabWidgetInvestimento->removeTab(index);
    6.  
    7. delete (tabConteudo);
    8. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QSignalMapper
    By Cremers in forum Newbie
    Replies: 5
    Last Post: 25th July 2013, 21:54
  2. QSignalMapper
    By Ali Reza in forum Newbie
    Replies: 35
    Last Post: 30th November 2012, 10:12
  3. QSignalMapper
    By axisdj in forum Newbie
    Replies: 6
    Last Post: 16th September 2010, 02:52
  4. help with QSignalMapper and actions
    By andreime in forum Newbie
    Replies: 1
    Last Post: 9th August 2009, 19:24
  5. ? about QSignalMapper
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2008, 22:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.