Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why TextButtonDelegate::createEditor not be called ?
Qt 6.11 is out! See what's new in the release blog

Why TextButtonDelegate::createEditor not be called ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 470 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tricoffee
    wrote on last edited by Tricoffee
    #1
    // 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();
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved