Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Getting the signal when a checkbox is toggled in a QtableWidget
Forum Updated to NodeBB v4.3 + New Features

Getting the signal when a checkbox is toggled in a QtableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 4.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ikraiemI Offline
    ikraiemI Offline
    ikraiem
    wrote on last edited by
    #1

    Hello everyone!

    I am struggling with getting the signal when a checkbox of an item is checked in QTableWidget. I created the checkboxes this way:
    for (int i=0; i<rowcount ; i++)
    { QTableWidgetItem *item = new QTableWidgetItem();
    ui->tablewidget->setItem(i,2,item);
    item->setCheckState(Qt::Unchecked);}

    the problem is when ever a checkbox is checked in this QtableWidget i need to store the state of this checkbox . I also have a QListWidget containing a number of items and when ever i select an item in this QList the checkboxes in the QtableWidget need to be updated. So every item in the QlistWidget have different states of the checkboxes according to the user choices.

    I was searching for the solution on my own for a long time, but because I did not find anything suitable I am writing my question here.

    Thank you for your help!

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi and welcome to the forums.

      The QListWidget/QTableWidget::itemChanged is triggered when the checked state is change so you can
      get it like you would for a text change .

      // This hook signal up to a lambda slot an changed color of item if checked or not. 
      QObject::connect(ui->listWidget, &QListWidget::itemChanged, this, [](QListWidgetItem * item) {
              if (item->checkState() == Qt::Checked)
                  item->setBackgroundColor(QColor("#ffffb2"));//yellow
              else
                  item->setBackgroundColor(QColor("#ffffff"));
          });
      

      That said. What does the QList and QtableWidget have in common item wise ?
      Im asking as using the View version of List and Table actually allows to share a model between them
      so if one sets check on some item, it will also show checked in the other. automatically.
      However, it requires that the data can arranged in the model so it makes sense to share it.

      JonBJ 1 Reply Last reply
      6
      • mrjjM mrjj

        Hi and welcome to the forums.

        The QListWidget/QTableWidget::itemChanged is triggered when the checked state is change so you can
        get it like you would for a text change .

        // This hook signal up to a lambda slot an changed color of item if checked or not. 
        QObject::connect(ui->listWidget, &QListWidget::itemChanged, this, [](QListWidgetItem * item) {
                if (item->checkState() == Qt::Checked)
                    item->setBackgroundColor(QColor("#ffffb2"));//yellow
                else
                    item->setBackgroundColor(QColor("#ffffff"));
            });
        

        That said. What does the QList and QtableWidget have in common item wise ?
        Im asking as using the View version of List and Table actually allows to share a model between them
        so if one sets check on some item, it will also show checked in the other. automatically.
        However, it requires that the data can arranged in the model so it makes sense to share it.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @mrjj said in Getting the signal when a checkbox is toggled in a QtableWidget:

        Im asking as using the View version of List and Table actually allows to share a model between them
        so if one sets check on some item, it will also show checked in the other. automatically.

        Ooohhh! :)

        But I don't quite get why you'd use it? By the time you can select/check something in the TableView anyway, why do you also want a ListView to select/check as well?

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved