Qt 6.11 is out! See what's new in the release
blog
Why TextButtonDelegate::createEditor not be called ?
-
// TextButtonDelegate.cpp file #include "TextButtonDelegate.h" #include <QAbstractItemView> #include <QEvent> TextButtonDelegate::TextButtonDelegate(QObject* parent) : QStyledItemDelegate(parent) { //installEventFilter(this); //setEditTriggers(QAbstractItemView::AllEditTriggers); } void TextButtonDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { //qDebug() << "void TextButtonDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const "; // draw text QString displayedText = option.fontMetrics.elidedText(index.data(Qt::DisplayRole).toString(), Qt::ElideRight, option.rect.width()); painter->drawText(option.rect, displayedText); // draw addButton QPixmap addButtonPixmap(":/XVisual/images/bold.png"); int addButtonX = option.rect.right() - 60; int addButtonY = option.rect.center().y() - addButtonPixmap.height() / 2; painter->drawPixmap(addButtonX, addButtonY, addButtonPixmap); // draw deleteButton QPixmap deleteButtonPixmap(":/XVisual/images/delete.png"); int deleteButtonX = option.rect.right() - 20; int deleteButtonY = option.rect.center().y() - deleteButtonPixmap.height() / 2; painter->drawPixmap(deleteButtonX, deleteButtonY, deleteButtonPixmap); QStyledItemDelegate::paint(painter, option, index); } QWidget* TextButtonDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const { qDebug() << "Creating editor for item:" << index.data(Qt::DisplayRole).toString(); QWidget* editor = new QWidget(parent); QHBoxLayout* layout = new QHBoxLayout(); layout->setContentsMargins(0, 0, 0, 0); QLabel* label = new QLabel(index.data(Qt::DisplayRole).toString(), editor); QPushButton* addButton = new QPushButton("+", editor); addButton->setProperty("itemText", index.data(Qt::DisplayRole).toString()); QPushButton* deleteButton = new QPushButton("-", editor); deleteButton->setProperty("itemText", index.data(Qt::DisplayRole).toString()); layout->addWidget(label); layout->addWidget(addButton); layout->addWidget(deleteButton); editor->setLayout(layout); return editor; } void TextButtonDelegate::setEditorData(QWidget* editor, const QModelIndex& index) const { // You can set data for the editor if needed } void TextButtonDelegate::setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const { // You can set model data if needed } void TextButtonDelegate::updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option, const QModelIndex& index) const { editor->setGeometry(option.rect); } bool TextButtonDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) { Q_UNUSED(model); if (event->type() == QEvent::MouseButtonPress) { return true; } return false; } // CustomComboBox.cpp file #include "CustomComboBox.h" #include <QAbstractItemView> CustomComboBox::CustomComboBox(QWidget* parent) : QComboBox(parent) { // Set to editable state setEditable(true); // Get the underlying QAbstractItemView object QAbstractItemView* itemView = view(); // Set edit triggers if (itemView) { //qDebug() << "itemView ------- CustomComboBox::CustomComboBox(QWidget* parent) : QComboBox(parent) "; itemView->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked); } // Create TextButtonDelegate textButtonDelegate = new TextButtonDelegate(); // Create a standard item model model = new QStandardItemModel(); // Set the model for the combo box setModel(model); // Set size policy setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); // Set delegate setItemDelegate(textButtonDelegate); } void CustomComboBox::setupItem(const QString &text) { item = new QStandardItem(text); item->setFlags(item->flags() | Qt::ItemIsEditable); // Add the ItemIsEditable flag // Add the item to the model model->appendRow(item); // Set data for the item model->setData(model->index(model->rowCount() - 1, 0), text, Qt::DisplayRole); } // main.cpp file #include <QApplication> #include <QMainWindow> #include <QWidget> #include <QHBoxLayout> #include "CustomComboBox.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); // Create the main window QMainWindow* mainWindow = new QMainWindow(); // Create a QWidget as the central widget of the main window QWidget* widget = new QWidget(); // Create CustomComboBox CustomComboBox* customComboBox = new CustomComboBox(); // Add items for (int i = 1; i <= 5; ++i) { customComboBox->setupItem("Item " + QString::number(i)); } // Create a horizontal layout QHBoxLayout* layout = new QHBoxLayout(widget); layout->addWidget(customComboBox); // Set the layout as the layout of the central widget widget->setLayout(layout); // Set the central widget of the main window mainWindow->setCentralWidget(widget); // Set the size and title of the main window mainWindow->setGeometry(100, 100, 400, 200); mainWindow->setWindowTitle("CustomComboBox Demo"); // Show the main window mainWindow->show(); // Run the application event loop return app.exec(); } -
Hi,
AFAIK, the selection model's currentChanged signal is connected for the combo box to work so it might just be that your edit triggers are shorted by this.
Note that editing the content of a QComboBox dropdown would be pretty counter intuitive. What is your goal with that paradigm ?