Results 1 to 3 of 3

Thread: Usage of QSignalMapper with checkboxes in QTableWidget

  1. #1
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Usage of QSignalMapper with checkboxes in QTableWidget

    Hey there,

    I'm new here, but not an absolut newbe with qt.
    I have to ask here, because i didn't find any solution for my problem.

    On My window I have a QTableWidget, wich I'm going to fill with checkboxes at runtime. I need signals for these checkboxes which contain the row count and the state from the checkbox signal toggled(bool state).

    I tried the following, but it doesn't work. I actual don't understand the whole thing with QSignalMapper.


    In the constructor I did:
    Qt Code:
    1. checkboxMapper = new QSignalMapper(this);
    To copy to clipboard, switch view to plain text mode 

    My first try of the signal mapper:
    Qt Code:
    1. ui->table->setRowCount(ui->table->rowCount()+1);
    2.  
    3. int index = ui->table->rowCount()-1;
    4.  
    5. QCheckBox *new_checkbox = new QCheckBox(this);
    6. new_checkbox->setChecked(true);
    7. connect(new_checkbox,SIGNAL(toggled(bool)),checkboxMapper,SLOT(map(bool)));
    8. checkboxMapper->setMapping(new_checkbox,(index,new_checkbox->isChecked()));
    9. connect(checkboxMapper,SIGNAL(mapped(int,bool)),this,SLOT(checkbox_toggled(int,bool)));
    10.  
    11. ui->table->setItem(index,0,new QTableWidgetItem("Some Stuff"));
    12. ui->table->setCellWidget(index,1,new_checkbox);
    To copy to clipboard, switch view to plain text mode 

    I hope my english is not too confusing and you could help me.

    Kind regards

  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: Usage of QSignalMapper with checkboxes in QTableWidget

    The first connect fails because there is no slot QSignalMapper::map(bool).

    I wonder why the setMapping call even compiles, I guess you wanted to call setMapping(QObject*, int)

    The second connect fails because there is no signal QSIgnalMapper::mapped(int, bool);

    The signal mapper will emit a signal with and argument value that was the second argument to the setMapping call for the respective sender object.

    E.g. if you call
    Qt Code:
    1. mapper->setMapping(sender, 1);
    To copy to clipboard, switch view to plain text mode 
    then the connected signal of sender will lead to
    Qt Code:
    1. mapped(1);
    To copy to clipboard, switch view to plain text mode 
    being emitted.

    In your case the row. Which you can then use to retrieve the checkbox.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    quizzmaster (14th September 2014)

  4. #3
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Usage of QSignalMapper with checkboxes in QTableWidget

    Thanks a lot. I wondered if there is an easier solution for this..

    Now the code looks like this and it works!

    Qt Code:
    1. ui->table->setRowCount(ui->table->rowCount()+1);
    2.  
    3. int index = ui->table->rowCount()-1;
    4.  
    5. QCheckBox *new_checkbox = new QCheckBox(this);
    6. new_checkbox->setChecked(true);
    7. connect(new_checkbox,SIGNAL(clicked()),checkboxMapper,SLOT(map()));
    8. checkboxMapper->setMapping(new_checkbox,index);
    9. connect(checkboxMapper,SIGNAL(mapped(int)),this,SLOT(checkbox_toggled(int)));
    10.  
    11. ui->table->setItem(index,0,new QTableWidgetItem(QString::fromStdString(molecule->formula())));
    12. ui->table->setCellWidget(index,1,new_checkbox);
    To copy to clipboard, switch view to plain text mode 

    with the slot:
    Qt Code:
    1. void window::checkbox_toggled(int index)
    2. {
    3. bool state = static_cast<QCheckBox*>(ui->table->cellWidget(index,1))->isChecked();
    4. main_window->do something(index,state);
    5. }
    To copy to clipboard, switch view to plain text mode 

    That produces the behavior I wanted

Similar Threads

  1. QSignalMapper
    By Cremers in forum Newbie
    Replies: 5
    Last Post: 25th July 2013, 20:54
  2. QTableWidget with checkboxes.
    By chris_helloworld in forum Qt Programming
    Replies: 0
    Last Post: 13th January 2011, 15:42
  3. Replies: 2
    Last Post: 7th December 2010, 09:19
  4. Replies: 8
    Last Post: 23rd October 2009, 15:33
  5. ? about QSignalMapper
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2008, 21:21

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.