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. How to align a checkbox added in column of a QTAbleView using QItemDelegate
QtWS25 Last Chance

How to align a checkbox added in column of a QTAbleView using QItemDelegate

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.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.
  • A Offline
    A Offline
    Ankita thakur
    wrote on last edited by
    #1

    Hi All,

    Thanks in advance for the help. Is there any way to fix the alignment of a checkbox added in one of the column in a QTableView.

    Using QitemDelegate to create the checkbox using createEditor:

    QWidget *NetDiagnosticsSelectionItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem
    &option, const QModelIndex &index) const
    {
    QCheckBox *checkBox = new QCheckBox(parent);
    return checkBox;
    }

    I am getting a left aligned QCheckBox
    0_1554811741579_d1b28835-efa4-4a86-b105-72d49af2c949-image.png

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Return a QWidget which contains two spacers and a QCheckbox in between.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      A 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        Return a QWidget which contains two spacers and a QCheckbox in between.

        A Offline
        A Offline
        Ankita thakur
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        Thanks for the response. I am yet to try this approach but is 9it the only way to do it?

        Why my below code in data(const QModelIndex & index, int role) doesnt work for only the column having checkboxes??

        if (role == Qt::TextAlignmentRole)
        return Qt::AlignCenter;

        1 Reply Last reply
        0
        • A Offline
          A Offline
          Ankita thakur
          wrote on last edited by
          #4

          The checkbox is not getting created using the QItem delegate. But its due to the following code in QVariant data(const QModelIndex & index, int role){
          if (role == Qt::CheckStateRole)
          {
          if (index.column() == Selection)
          {
          NetDiagnosticsInfo *value = netDiagnosticsValues_.at(index.row());
          bool aBool = value->getSelection();
          if (aBool)
          return Qt::Checked ;
          else
          return Qt::Unchecked ;
          }
          }
          }

          Is there any way to set the alignment here?

          1 Reply Last reply
          0
          • Christian EhrlicherC Online
            Christian EhrlicherC Online
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            No, Qt does not support aligning the checkbox in the item. You have to do it in your own delegate the way I told you above.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            A 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              No, Qt does not support aligning the checkbox in the item. You have to do it in your own delegate the way I told you above.

              A Offline
              A Offline
              Ankita thakur
              wrote on last edited by
              #6

              @Christian-Ehrlicher Not working . The checkbox disappered when i added the checkox using a createEditor.

              QWidget *NetDiagnosticsSelectionItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem
              &option, const QModelIndex &index) const
              {
              QCheckBox *checkBox = new QCheckBox(parent);
              return checkBox;
              }

              ui->tableView->setItemDelegateForColumn(NetDiagnosticsModel::Selection, new NetDiagnosticsSelectionItemDelegate(this));

              Should i use paint function and not create editor???

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Billy Wu
                wrote on last edited by
                #7

                I had the same problem which I'd left open for years. Lately I revisited it and came up with these simple tweaks that seem working to me. I hope someone may find them useful.

                // relocate checkbox to cell center
                void CheckItemDelegate::drawCheck ( QPainter * painter, const QStyleOptionViewItem & option, const QRect & rect,
                Qt::CheckState state ) const
                {
                int dx = (option.rect.width() - rect.width()) / 2;

                QRect adjRect = rect;
                adjRect.adjust(dx, 0, dx, 0);
                
                QItemDelegate::drawCheck(painter, option, adjRect, state);
                

                }

                // suppress drawing of the background of the empty text which would otherwise cover the relocated checkbox
                void CheckItemDelegate::drawDisplay(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const
                {
                }

                // draw focus frame around the entire cell instead of just the text
                void CheckItemDelegate::drawFocus(QPainter *painter, const QStyleOptionViewItem &option, const QRect &rect) const {
                QItemDelegate::drawFocus(painter, option, option.rect);
                }

                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