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. Check state change only when mouse clicks on checkbox
QtWS25 Last Chance

Check state change only when mouse clicks on checkbox

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlistwidgetmousecheckstate
6 Posts 2 Posters 3.8k Views
  • 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.
  • N Offline
    N Offline
    nebulaekg
    wrote on last edited by
    #1

    Hi,

    I am implementing a qlistwidget in a main window, and I need to toggle the checkstate of a list widget item when clicking the checkbox before the item text. Does anyone know how to make the item checkstate change ONLY when the mouse clicks on the checkbox leading the item, instead of anywhere of the item?

    Thank you very much!

    A 1 Reply Last reply
    0
    • N nebulaekg

      Hi,

      I am implementing a qlistwidget in a main window, and I need to toggle the checkstate of a list widget item when clicking the checkbox before the item text. Does anyone know how to make the item checkstate change ONLY when the mouse clicks on the checkbox leading the item, instead of anywhere of the item?

      Thank you very much!

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @nebulaekg Without seeing some code I'm not 100% sure what you're trying to do, so there may be an easier way than my suggestion here...

      You can handle left click for the entire list item, check to see if it's in the checkbox widget and process accordingly.

      I feel like this is probably way overkill but I don't know what you already have and what you are trying to accomplish with the information you provided. I.e. how did you add the checkbox to the list widget? Share some code and someone can probably help you out more ..

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      N 1 Reply Last reply
      0
      • A ambershark

        @nebulaekg Without seeing some code I'm not 100% sure what you're trying to do, so there may be an easier way than my suggestion here...

        You can handle left click for the entire list item, check to see if it's in the checkbox widget and process accordingly.

        I feel like this is probably way overkill but I don't know what you already have and what you are trying to accomplish with the information you provided. I.e. how did you add the checkbox to the list widget? Share some code and someone can probably help you out more ..

        N Offline
        N Offline
        nebulaekg
        wrote on last edited by
        #3

        @ambershark Below is how I dynamically add a listwidgetitem to the listwidget residing in the main app:

                    QFileInfo fs(f);
                    QListWidgetItem * item = new QListWidgetItem();
                    ui->listItem->addItem(item);
                    QLabel *s = new QLabel;
                    s->setText(fs.baseName());
                    ui->listItem->setItemWidget(item, s);
                    item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
                    item->setCheckState(Qt::Checked);
        

        The list widget can correctly show the items when I add the item, and is checkable. But my goal is: when I click the added item ONLY at its checkbox, its checkstate is toggled (checked -> unchecked, unchecked -> checked), not some other place. Currently, when I click anywhere that belongs to the item, the item's checkstate is toggled. This is not what I want, partially because I have reserved the doubleclick signal to some other important functionality. I wonder if there are any simple way to achieving this "checkbox-click-only" thing.

        And an additional question, how to emit a signal corresponding to the click of the item's checkbox if the checkbox-click-only is achievable?

        Thank you very much!

        A 1 Reply Last reply
        0
        • N nebulaekg

          @ambershark Below is how I dynamically add a listwidgetitem to the listwidget residing in the main app:

                      QFileInfo fs(f);
                      QListWidgetItem * item = new QListWidgetItem();
                      ui->listItem->addItem(item);
                      QLabel *s = new QLabel;
                      s->setText(fs.baseName());
                      ui->listItem->setItemWidget(item, s);
                      item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable);
                      item->setCheckState(Qt::Checked);
          

          The list widget can correctly show the items when I add the item, and is checkable. But my goal is: when I click the added item ONLY at its checkbox, its checkstate is toggled (checked -> unchecked, unchecked -> checked), not some other place. Currently, when I click anywhere that belongs to the item, the item's checkstate is toggled. This is not what I want, partially because I have reserved the doubleclick signal to some other important functionality. I wonder if there are any simple way to achieving this "checkbox-click-only" thing.

          And an additional question, how to emit a signal corresponding to the click of the item's checkbox if the checkbox-click-only is achievable?

          Thank you very much!

          A Offline
          A Offline
          ambershark
          wrote on last edited by
          #4

          @nebulaekg Ok I get it now .. so this will be pretty complicated because you are using a QListWidget instead of QListView and a model.

          My first recommendation is to use a QListView, a model, and delegates. Then you can control every aspect of your view including your checkbox and where the clicks matter.

          If you want to stick with what you have then I think you will have to make a custom QListWidgetItem that handles the checkbox separately. It will probably be a lot of work to do that. It's definitely not the route I would take. Look into views/models/delegates since I think that will be a lot more of what you are wanting.

          http://doc.qt.io/qt-5/model-view-programming.html

          My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

          N 1 Reply Last reply
          1
          • A ambershark

            @nebulaekg Ok I get it now .. so this will be pretty complicated because you are using a QListWidget instead of QListView and a model.

            My first recommendation is to use a QListView, a model, and delegates. Then you can control every aspect of your view including your checkbox and where the clicks matter.

            If you want to stick with what you have then I think you will have to make a custom QListWidgetItem that handles the checkbox separately. It will probably be a lot of work to do that. It's definitely not the route I would take. Look into views/models/delegates since I think that will be a lot more of what you are wanting.

            http://doc.qt.io/qt-5/model-view-programming.html

            N Offline
            N Offline
            nebulaekg
            wrote on last edited by
            #5

            @ambershark It seems from the beginning I have chosen the wrong approach.... Thanks. I will look into it.

            A 1 Reply Last reply
            0
            • N nebulaekg

              @ambershark It seems from the beginning I have chosen the wrong approach.... Thanks. I will look into it.

              A Offline
              A Offline
              ambershark
              wrote on last edited by
              #6

              @nebulaekg It wasn't wrong, it was just the easy method. With easy comes very little customization. Now that you want to change it's behavior it becomes much harder. For simple lists you can definitely use the widget but for anything more you want a view/model and then delegates to control display.

              Check into it I'm sure you'll find it's not that much harder than the widget and will give you the control you want. :)

              My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

              1 Reply Last reply
              0

              • Login

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