How to set a delegate for a single cell in Qt item view?

12,155

Solution 1

No you can't set item delegate only for one cell or one column but you can easly set item delegate for whole widget and choose in which cell, column or row you want to use your custom painting or something.

For e.g.

void WidgetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                       const QModelIndex &index) const
{
    if (index.column() == 1) 
    {
        // ohh it's my column
        // better do something creative
    } 
    else // it's just a common column. Live it in default way
        QItemDelegate::paint(painter, option, index);
}

You can find some more information here

Solution 2

I'd recommend reimplementing createEditor function instead:

QWidget * WidgetDelegate::createEditor(
        QWidget *parent,
        const QStyleOptionViewItem &,
        const QModelIndex &index) const
{
    QWidget *widget = 0;
    if (index.isValid() && index.column() < factories.size())
    {
        widget = factories[index.column()]->createEditor(index.data(Qt::EditRole).userType(), parent);
        if (widget)
            widget->setFocusPolicy(Qt::WheelFocus);
    }
    return widget;
}
Share:
12,155
Matt Phillips
Author by

Matt Phillips

I create Machine Learning algorithms to solve problems in computer vision, biomedical image understanding, and EHR (electronic health record) understanding, deployed locally or in the cloud (AWS). I'm always happy to enter into new domains, particularly where the data are well-structured. Deep neural networks (CNNs, RNNs, Autoencoders, etc.) are generally my tools of choice. I use PyTorch and Tensorflow/Keras for DL, and Python/PIL/OpenCV for general ML and CV. I also have extensive experience in software engineering (esp. C++) and have loved using it to create products and applications in ML, signal processing, data analysis and visualization. I also have substantial background in experimental and computational neuroscience, which provides good general conceptual tools to help attack my current research and engineering problems. Some of my open-source software includes WaveSorter for sorting and classifying (neural) wavevforms, as well as an application for the convenient visualization of dynamical systems, DynaSys.

Updated on July 09, 2022

Comments

  • Matt Phillips
    Matt Phillips almost 2 years

    Rather perplexed by this omission--but in Qt's QAbstractItemView class, it's possible to set a QAbstractItemDelegate (i.e., QItemDelegate or QStyledItemDelegate) to the entire view, a single row, or a single column, using the setItemDelegate* methods. In addition the item delegate for an individual cell can be queried, with QAbstractItemView::itemDelegate(const QModelIndex&), along with the delegate for rows, columns. and the entire view. But there appears to be no way to set an item delegate to an individual cell. Am I missing something? Any reason this should be?

  • Matt Phillips
    Matt Phillips over 11 years
    This is my first time using delegates--on your method, if the delegate I set is a spinbox, would editing only reveal the spinbox for column 1? Would the spinbox range not be in effect for the other columns?
  • Blood
    Blood over 11 years
    Every things will affect only column number 1. All other columns will saty as they were earlier.
  • Matt Phillips
    Matt Phillips over 11 years
    Alright, but on this method is it possible to have more than one cell-specific delegate type? Could you have a spinbox at (0,2) and a progress bar at (3,1) with default delegates everywhere else?
  • Blood
    Blood over 11 years
    Yes, all that you can do by one ItemDelegate. Add else if after this one if and check for proper column and row
  • Matt Phillips
    Matt Phillips over 11 years
    I'm trying to implement what you wrote now and I find I have no idea how to specify within the if (index... block what the widget for that particular case should be--can you fill that in with example code?
  • Blood
    Blood over 11 years
    I paste link for you in my answer. There is all written.
  • Barath Ravikumar
    Barath Ravikumar about 10 years
    -1 'No you can't set item delegate only for one cell or one column' Yes you can, check void QAbstractItemView::setItemDelegateForColumn ( int column, QAbstractItemDelegate * delegate )
  • JokerMartini
    JokerMartini over 5 years
    Could you demonstrate this being used with a comboBox?