Important: Please read the Qt Code of Conduct -

Incorrect drawn images and checkboxes in table

  • We have following problem. We have custom drawn table with images and checkboxes. They are located in adjacent columns. When we compile and run on Linux, everything works fine, but on Windows when hovering mouse over checkboxes, images will be erased by background. It seems like when checkbox is redrawn, it takes some of the image's area. Can you explain the difference in behavior, and how to make everything to be displayed correctly on both OSes?

  • Well, that would be hard, considdering that you're not showing us the code.
    For starters, it is unclear what you mean by a a "custom drawn table": did you make your own table, or do you use QTableView with a custom QItemDelegate implementation?

    I think you'll have more luck getting a useful answer if you show us the code you use for the drawing.

  • Can you please provide some snapshots and code?
    In general, it should work via different platforms.

    But if it's custom drawn, what do you draw by yourself?

  • Here are sources which I think are related to problem. This is custom delegate that paints everything in table except for checkboxes. Checkboxes are created with standard qt means (CheckStateRole in model).

    DemandJournalistStatusDelegate::paint(QPainter *aPainter,
    const QStyleOptionViewItem &aOption,
    const QModelIndex &aIndex) const
    int cellHeight = 20;
    int rectY = aOption.rect.y();
    int rectWidth = aOption.rect.width();
    int tempX = 3;

    QRect rect;
    QRect source;
    //draw Avatar
    source.setRect(0, -3, 20, 20);
    rect.setRect(tempX, rectY, 20, cellHeight);
    aPainter->drawImage(rect, ImagesCache::instance()->get(Nick), source);
    tempX += 20;

    //draw Name
    rect.setRect(tempX, rectY, mMapWidth[0]-23, cellHeight);
    aPainter->drawText(rect, Qt::AlignVCenter | Qt::TextWordWrap, MetricsName);

    tempX = mMapWidth[0];

  • I assume that you let Qt draw the check boxes by calling the base implementation or something of paint() or something like that? I think that is the key part of the code, and it is a pitty it has been omitted here.

    If you do that, how about changeing the order in which you draw your stuff. If you first draw the checkbox, you can start drawing your own content at whatever place you want. That will ensure the checkbox does not overdraw your own content. Notice that you can get the dimentions of the check box from the style, no need to hard code that.

  • I don't call base paint to draw checkbox. I simply provide Qt::CheckStateRole in model's data() function, and don't use anything else. Thanks for the advice though. Maybe the problem is indeed with hardcoded checkbox size. Can you tell, how I can obtain its dimensions from style?

    ChannelsListTableModel::data ( const QModelIndex & aIndex,
    int role ) const
    if(role == Qt::CheckStateRole)
    if(mDataChek.value(aIndex.row())) return Qt::Checked;
    if(!mDataChek.value(aIndex.row())) return Qt::Unchecked;

    ChannelsListTableModel::setData (const QModelIndex& aIndex,
    const QVariant & aValue,
    int aRole)
    if( aRole == Qt::CheckStateRole && aIndex.column()==1)
    return Qt::Unchecked;
    return Qt::Checked;
    return Qt::Unchecked;

  • ohh! thanks for advice, I tried to obtain dimensions from style and it worked!!!
    In case some needs it here is what I did:
    @tempX += QApplication::style()->subElementRect(QStyle::SE_CheckBoxClickRect,&aOption).x();@

Log in to reply