Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Change cell contents before editing QTableWidget cell?



  • If the user double-clicks on a cell, it is the current contents of the cell that are what is used in the editing box within that cell.

    Is there a way for me to replace the text in the cell just after the double click and just before the cell enters editing mode?



  • @Publicnamer Use a delegate:

    class Delegate: public QStyledItemDelegate{
    public:
        using QStyledItemDelegate::QStyledItemDelegate;
        void setEditorData(QWidget *editor, const QModelIndex &index) const{
            if(QLineEdit *lineEdit = qobject_cast<QLineEdit *>(editor)){
                lineEdit->setText("Foo");
            }
        }
    };
    
    tableWidget->setItemDelegate(new Delegate);
    

Log in to reply