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. Display problem with QCheckBox
Qt 6.11 is out! See what's new in the release blog

Display problem with QCheckBox

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.3k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hello world !

    I have a display problem. I explain :
    I'm working on a QTableView and I want to put a QCheckBox in each cell. So I use the setIndexWidget() method.

    The question is : What QWidget do I must to put : a QCheckBox directly or a QFrame which contains the QCheckBox?

    Actually, I want to center the widget. So I use a QFrame.

    @
    for(int i=0;i<m_pNbAnnees;i++)
    {
    //columns
    for(int j=0;j<3;j++)
    {
    QFrame* m_pTabFrame = new QFrame(m_pTab);
    QVBoxLayout* m_pTabLayout = new QVBoxLayout();
    m_pTabFrame->setLayout(m_pTabLayout);
    QCheckBox* m_pTabCheckBox = new QCheckBox();
    m_pTabLayout->addWidget(m_pTabCheckBox,0,Qt::AlignHCenter | Qt::AlignVCenter);

        m_pTab->setIndexWidget(m_pTabModel->item(i,j)->index(), m_pTabFrame);
    }
    

    }
    @

    The problem is the QCheckBox is not diplayed correctly when I put it in a QFrame. Instead of a simple square, I get a kind of line like if the QCheckBox was in relief.

    I really don't understand what happens !

    [Edit: Normalized white space in code snippet; mlong]

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Better yet: neither of these. Item views are by default (through the default delegate) capable of displaying checkboxes. Just make sure your model returns the following:

      From the flags() method, it should return a result that contains the Qt::ItemIsUserCheckable flag, and

      From the data() method, it should return a Qt::CheckState when the Qt::CheckStateRole role is requested.

      That will make your items checkable without fidling around with adding widgets to cells in your table. It depends on what model you use how you can best make sure of that. If you use a [[doc:QStandardItemModel]], there are methods exposed on the StandardModelItem that will set these (setCheckable() and setCheckState() respectively).

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        Thanks for this complete answer Andre.

        I tried the setCheckable() method. It works but the QCheckBox is not centered. And this option implies to put an item on cells. It's not really what i need. The table I mean must be a kind of filter with only a QCheckBox in cell.

        Edit : I wonder if it's not a margin problem which masks a part of the checkbox.

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

          In that case, I would create a custom delegate that renders (only) the checkbox in the center of the cell instead. That really isn't all that hard to do.

          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