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. Widget with checkbox as an editor for ItemDelegate derivative

Widget with checkbox as an editor for ItemDelegate derivative

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.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.
  • starryeyedS Offline
    starryeyedS Offline
    starryeyed
    wrote on last edited by starryeyed
    #1

    Hello.
    I needed a centered checkbox for column in my QTableView.
    I followed this post and everything worked as soon as I realized that part had to be in .h file. My two problems are:

    • How to make this BooleanWidget appear not near upper left corner of my screen, but in the edited cell? Tried changing QWidget to QFrame, got nothing

    • How do I toggle editor's state just by 1 click? Can I override some 2 slots related to focus events? Like, onCellWithDelegateSelected to just auto toggle and onCellWithDelegateClickedOff to save the state into model?

    P.S. Header & .cpp

    1 Reply Last reply
    0
    • starryeyedS Offline
      starryeyedS Offline
      starryeyed
      wrote on last edited by
      #2

      0_1530016922136_Безымянный.png

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

        Hi
        Does it sort of become a window?
        That happens if no parent is given. ( or NULL)
        Try to check if you give it a null parent where you create it.

        The issue is that CheckBoxDelegate dont get a parent.
        I assume you do like:
        ui->listWidget->setItemDelegate( new CheckBoxDelegate());
        So
        You need to make its constructor take parent as normally.
        and give it parent.
        .h
        CheckBoxDelegate(QObject* parent);
        .cpp
        CheckBoxDelegate::CheckBoxDelegate(QObject *parent) : QItemDelegate(parent) {
        }

        and then
        ui->listWidget->setItemDelegate( new CheckBoxDelegate(this));

        Then profit:
        alt text

        starryeyedS 1 Reply Last reply
        2
        • mrjjM mrjj

          Hi
          Does it sort of become a window?
          That happens if no parent is given. ( or NULL)
          Try to check if you give it a null parent where you create it.

          The issue is that CheckBoxDelegate dont get a parent.
          I assume you do like:
          ui->listWidget->setItemDelegate( new CheckBoxDelegate());
          So
          You need to make its constructor take parent as normally.
          and give it parent.
          .h
          CheckBoxDelegate(QObject* parent);
          .cpp
          CheckBoxDelegate::CheckBoxDelegate(QObject *parent) : QItemDelegate(parent) {
          }

          and then
          ui->listWidget->setItemDelegate( new CheckBoxDelegate(this));

          Then profit:
          alt text

          starryeyedS Offline
          starryeyedS Offline
          starryeyed
          wrote on last edited by starryeyed
          #4

          @mrjj thank you very much for advice, it sounds legit, and now when I check with qDebug it shows that parent isn't null. However! What you can see on previous screenshot still happens... I mean, it's still grossly misplaced, can you zip me a working code sample with QListView? Or QTableView? Here are my current .h/.cpp

          P.S.For some reason I can see neither 'Locals' nor 'Expressions' (the locals are ever-invisible & expressions are visible when you add them but always with blank (unavailable) values). Iе worked before and now just doesn't. I declined updating from 5.10.1 to 5.11

          mrjjM 1 Reply Last reply
          0
          • starryeyedS starryeyed

            @mrjj thank you very much for advice, it sounds legit, and now when I check with qDebug it shows that parent isn't null. However! What you can see on previous screenshot still happens... I mean, it's still grossly misplaced, can you zip me a working code sample with QListView? Or QTableView? Here are my current .h/.cpp

            P.S.For some reason I can see neither 'Locals' nor 'Expressions' (the locals are ever-invisible & expressions are visible when you add them but always with blank (unavailable) values). Iе worked before and now just doesn't. I declined updating from 5.10.1 to 5.11

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @starryeyed
            Hi
            sure. here is test project.
            https://www.dropbox.com/s/qfa31g3cjoo0wlr/MyDelegateCombo.zip?dl=0
            its your code but i just added to mainwindow.h to test fast.

            After given it parent, it stays in the cells.

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

              About the expressions.
              Do you have this ON ?
              alt text

              1 Reply Last reply
              1
              • mrjjM mrjj

                @starryeyed
                Hi
                sure. here is test project.
                https://www.dropbox.com/s/qfa31g3cjoo0wlr/MyDelegateCombo.zip?dl=0
                its your code but i just added to mainwindow.h to test fast.

                After given it parent, it stays in the cells.

                starryeyedS Offline
                starryeyedS Offline
                starryeyed
                wrote on last edited by
                #7

                @mrjj I've found a way to do this without additional subclassing at all. Thanks for sample code! I am making a QButtonGroup of radio buttons using it.
                Here's how to center the box with a stylesheet

                QWidget* CheckBoxDelegate::createEditor(<...>)
                    {
                        QCheckBox *cb = new QCheckBox(parent);
                        cb->setStyleSheet("QCheckBox {margin-left: 43%; margin-right: 57%;}");
                        return cb;
                    }
                

                P.S. Resetting that checkbox and restarting the IDE helped! Thanks for making those views work :)

                mrjjM 1 Reply Last reply
                1
                • starryeyedS starryeyed

                  @mrjj I've found a way to do this without additional subclassing at all. Thanks for sample code! I am making a QButtonGroup of radio buttons using it.
                  Here's how to center the box with a stylesheet

                  QWidget* CheckBoxDelegate::createEditor(<...>)
                      {
                          QCheckBox *cb = new QCheckBox(parent);
                          cb->setStyleSheet("QCheckBox {margin-left: 43%; margin-right: 57%;}");
                          return cb;
                      }
                  

                  P.S. Resetting that checkbox and restarting the IDE helped! Thanks for making those views work :)

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @starryeyed
                  Clever margin trick :)
                  btw i assume you know that QListWidget/all views can be set to be checkable?
                  new_item->setFlags(new_item->flags() | Qt::ItemIsUserCheckable);

                  but i assume you goal was to have it centered and hence the delegate.

                  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