Text from a QTableView's cell shows through inline QComboBox - OSX
-
Hi,
I'm seeing an issue with a QComboBox in a QTableView letting the original value of the cell showing through as if the QComboBox is behind the table's cell.
Image of when the QComboBox is expanded - fine
Apart from this visual glitch, the QComboBox works as expected.
Qt's version is 6.0.3.
This is on Mac OS X 11.2.3. I haven't had the chance to test this on any other platforms.
Thank you.
-
Hi and welcome to devnet,
Are you calling setCellWidget ?
-
@SGaist If that's a method from the QTableWidget, then, nope. I'm not using a QTableWidget, I'm using a QTableView.
Btw, I have other types of delegates to embed other widgets (e.g. QSpinBox) in the table and those work fine. It's just the QComboBox that seems to have this problem.
-
Can you show the code of that delegate ?
-
@SGaist Sure.
class ComboBoxDelegate : public QStyledItemDelegate { Q_OBJECT public: explicit ComboBoxDelegate(QObject* parent); ~ComboBoxDelegate() override = default; QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override; void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override; }; ComboBoxDelegate::ComboBoxDelegate(QObject* parent) : QStyledItemDelegate(parent) { } QWidget* ComboBoxDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { QComboBox* comboBox = new QComboBox(parent); comboBox->setModel(new ValueListModel(index)); return comboBox; } void ComboBoxDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { qobject_cast<QComboBox*>(editor)->setCurrentText(index.data(Qt::EditRole).toString()); } void ComboBoxDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { model->setData(index, qobject_cast<QComboBox*>(editor)->currentText()); }
Thank you for looking into this.
-
@rpadrela I feel there is some thing wrong here(not sure)
{ qobject_cast<QComboBox*>(editor)->setCurrentText(index.data(Qt::EditRole).toString()); }
Why are you setting the combo box text here?
Is it editable comboBox?
Set the comboBox setCurrentIndex here. If comboBox list of elements is set through model. -
@nagesh The current code is correct.
from https://doc.qt.io/qt-5/qcombobox.html#currentText-prop
The setter setCurrentText() simply calls setEditText() if the combo box is editable. Otherwise, if there is a matching text in the list, currentIndex is set to the corresponding index.
-
@rpadrela said in Text from a QTableView's cell shows through inline QComboBox - OSX:
Could this be an issue currently affecting Mac OS X only?
On windows, both the windows style and Fusion work correctly with this delegate.
Could you try adding
QApplication::setStyle(QStyleFactory::create("Fusion"));
after you create theQApplication
in yourmain()
? and see if you still have the problem on Fusion? -
Did you test with 6.1 ?
From the looks of it's indeed macOS specific. IIRC there have been several glitches in the rendering of widgets with item views.