Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 973571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:17:57+00:00 2026-05-16T03:17:57+00:00

We are using a QTableView with Qt 4.6.3, and need a column that only

  • 0

We are using a QTableView with Qt 4.6.3, and need a column that only has a checkbox in each cell. We’re using a custom subclass of QAbstractTableModel as the model for the QTableView. Right now, we have a checkbox by setting the Qt::ItemIsUserCheckable flag. But we can’t figure out how to get rid of the blank textbox next to the checkbox!

How can we make the column only have a checkbox, nothing else?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:17:57+00:00Added an answer on May 16, 2026 at 3:17 am

    Here is a solution. For this to work properly, your column should not have the Qt::ItemIsEditable or Qt::ItemIsUserCheckable flags set. This reads the boolean values from Qt::DisplayRole and calls setData() with Qt::EditRole (i.e. not Qt::CheckStateRole.)

    #include "check_box_delegate.h"
    
    #include <QtGui/QApplication>
    #include <QtGui/QMouseEvent>
    
    static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) {
      QStyleOptionButton check_box_style_option;
      QRect check_box_rect = QApplication::style()->subElementRect(
          QStyle::SE_CheckBoxIndicator,
          &check_box_style_option);
      QPoint check_box_point(view_item_style_options.rect.x() +
                             view_item_style_options.rect.width() / 2 -
                             check_box_rect.width() / 2,
                             view_item_style_options.rect.y() +
                             view_item_style_options.rect.height() / 2 -
                             check_box_rect.height() / 2);
      return QRect(check_box_point, check_box_rect.size());
    }
    
    CheckBoxDelegate::CheckBoxDelegate(QObject *parent)
      : QStyledItemDelegate(parent) {
    }
    
    void CheckBoxDelegate::paint(QPainter *painter,
                                 const QStyleOptionViewItem &option,
                                 const QModelIndex &index) const {
      bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
    
      QStyleOptionButton check_box_style_option;
      check_box_style_option.state |= QStyle::State_Enabled;
      if (checked) {
        check_box_style_option.state |= QStyle::State_On;
      } else {
        check_box_style_option.state |= QStyle::State_Off;
      }
      check_box_style_option.rect = CheckBoxRect(option);
    
      QApplication::style()->drawControl(QStyle::CE_CheckBox,
                                         &check_box_style_option,
                                         painter);
    }
    
    // This is essentially copied from QStyledItemEditor, except that we
    // have to determine our own "hot zone" for the mouse click.
    bool CheckBoxDelegate::editorEvent(QEvent *event,
                                       QAbstractItemModel *model,
                                       const QStyleOptionViewItem &option,
                                       const QModelIndex &index) {
      if ((event->type() == QEvent::MouseButtonRelease) ||
          (event->type() == QEvent::MouseButtonDblClick)) {
        QMouseEvent *mouse_event = static_cast<QMouseEvent*>(event);
        if (mouse_event->button() != Qt::LeftButton ||
            !CheckBoxRect(option).contains(mouse_event->pos())) {
          return false;
        }
        if (event->type() == QEvent::MouseButtonDblClick) {
          return true;
        }
      } else if (event->type() == QEvent::KeyPress) {
        if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space &&
            static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select) {
          return false;
        }
      } else {
        return false;
      }
    
      bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
      return model->setData(index, !checked, Qt::EditRole);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.