[Interest] Question about custom delegates

Murphy, Sean smurphy at walbro.com
Wed Oct 30 17:36:15 CET 2019

I've got a QTableView that I'm putting a custom widget/editor in one column 
to allow editing of data, that I've got a couple of questions about. First, the 
custom widget is simply 3 QRadioButtons in a horizontal layout:

-- QHBoxLayout
---- QRadioButton
---- QRadioButton
---- QRadioButton

And I create the widget in my delegate's createEditor()
    QWidget *myDelegate::createEditor(QWidget *parent,
                                          const QStyleOptionViewItem &option,
                                          const QModelIndex &index) const
        threeRadioButtonWidget* w = new threeRadioButtonWidget(parent);
        connect(w, & threeRadioButtonWidget::clicked, 
                        this, &myDelegate::editorClicked);
        return w;

Then, because I want those editors to always be visible I have the following 
in my parent class:
    mDelegate = new myDelegate(this);
    ui->tableView->setItemDelegateForColumn(3, mDelegate);
    for(int i=0; i < mModel->rowCount(); ++i)
        QModelIndex idx = mModel->index(i, 3, QModelIndex());

1. Is there any way to avoid the openPersistentEditor loop above? The way 
I have it now works, but seems clumsy, especially as changes in the underlying 
data causes row/column counts to change. It seems like I would want to do 
something in my delegate's paint() function to paint the image of the 3 radio 
buttons, but I not seeing how I do that in a way where the real editing widget 
would line up perfectly with the painted image when the user goes to edit.
    a. Also, if I set it up where it's no longer a persistent editor, I want it so the user 
        only has to click once over a radio button to count as a click on the radio 
        button itself. I don't want the user to have to click in the cell first to get the 
        editor to activate, then have to click a second time.

2. My selection behavior for the table is QAbstractItemView::SelectRows. When 
the user clicks on a given row to select it, the other cells in the selected row get 
the highlighted color, but the background of the cells controlled by this delegate 
doesn't change to match. I get why that occurs given the way I have it - since I'm 
using openPersistentEditor(), the real three radio button widget is always shown, 
and I need to propagate the selection color down to that widget's background 
color for it to work, but I'm assuming that if I get the paint() issue from question 
#1 working I could correct it there?

3. I allow the user to drag and drop table rows to reorder the data. As I currently 
have the code, the threeRadioButtonWidget object isn't not included in the drag 
pixmap, so while the user is dragging a row, the drag looks "wrong" - it looks like 
that column's data isn't coming along with the drag. Again, I'm assuming if I fix 
the paint() question above, that might take care of itself?


This message has been scanned for malware by Forcepoint. www.forcepoint.com

More information about the Interest mailing list