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

QTableWidget editing signal



  • Hi all,

    is there a way to know when a QTableWidgetItem is being edited?

    My app is listening for UDP broadcast frames and I have a table reporting those records with some additional editable colums... the table is automalically refreshed when new data arrives... but I would like to stop the refresh if the user is actually editing an item into the table...

    I've checked cellEntered(), cellPressed(), cellActivated(), cellDoubleClicked() and the corresponding item versions but none of them seems to tell me when the user is actually editing an item.

       thanks for any hints
    
             Giampaolo


  • @iw2lsi
    Yes: https://doc.qt.io/qt-5/qtablewidget.html#isPersistentEditorOpen. You'll have to loop through the items. Or, if you are sub-classing QTableWidgetItem you could monitor https://doc.qt.io/qt-5/qtablewidget.html#openPersistentEditor & https://doc.qt.io/qt-5/qtablewidget.html#closePersistentEditor to keep your own track of an item being edited to avoid searching.



  • You can just add a custom signal to the delegate.

    class SignalEditDelegate : public QStyledItemDelegate
    {
        Q_OBJECT
        Q_DISABLE_COPY_MOVE(SignalEditDelegate)
    public:
        explicit SignalEditDelegate(QObject* parent = nullptr)
            : QStyledItemDelegate(parent)
        {}
        void setEditorData(QWidget *editor, const QModelIndex &index) const override {
            QStyledItemDelegate::setEditorData(editor, index);
            editStarted();
        }
        void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override {
            QStyledItemDelegate::setModelData(editor, model, index);
            editFinished();
        }
    signals:
        void editStarted() const;
        void editFinished() const;
    };
    

    Then you use it via:

    SignalEditDelegate* signalDelegate =new SignalEditDelegate(this)
    connect(signalDelegate,&SignalEditDelegate::editStarted, /*...*/);
    connect(signalDelegate,&SignalEditDelegate::editFinished, /*...*/);
    tableWidget->setItemDelegate(signalDelegate);
    


  • @JonB thanks! I've solved it by looking at the persistentEditor status...



  • @VRonin Hello Ronin... I'm probably missing something but I'm getting various errors that I do not know how to fix...

    SignalEditDelegate.h:15:5: error: unknown type name 'Q_DISABLE_COPY_MOVE'
    note: Q_DISABLE_COPY(SignalEditDelegate) seems to work.

    SignalEditDelegate.h:24:31: error: cannot initialize object parameter of type 'const QStyledItemDelegate' with an expression of type 'const SignalEditDelegate'
    SignalEditDelegate.h:25:11: error: 'this' argument to member function 'editStarted' has type 'const SignalEditDelegate', but function is not marked const

    Giampaolo



  • @iw2lsi said in QTableWidget editing signal:

    SignalEditDelegate.h:15:5: error: unknown type name 'Q_DISABLE_COPY_MOVE'
    note: Q_DISABLE_COPY(SignalEditDelegate) seems to work.

    Q_DISABLE_COPY_MOVE was added to Qt recently (5.13). you can use Q_DISABLE_COPY instead or just delete the line completely

    SignalEditDelegate.h:24:31: error: cannot initialize object parameter of type 'const QStyledItemDelegate' with an expression of type 'const SignalEditDelegate'
    SignalEditDelegate.h:25:11: error: 'this' argument to member function 'editStarted' has type 'const SignalEditDelegate', but function is not marked const

    the old const-signal phantomime. of course. Just change

    signals:
        void editStarted();
        void editFinished();
    

    to

    signals:
        void editStarted() const;
        void editFinished() const;
    


  • @VRonin said in QTableWidget editing signal:

    the old const-signal phantomime.

    Ah, now, I had wondered whether signals could be declared const.

    Would you mind explaining briefly when you either must or choose to do so, please?



  • @VRonin wooow... it works like a charm!

    there was still a linker error due to the missing Q_OBJECT macro... but it's perfect!

    thanks again,

      Giampaolo


  • @JonB said in QTableWidget editing signal:

    Would you mind explaining briefly when you either must or choose to do so, please?

    Signals can be declared const but it has no material effect. When moc creates the implementation of the signal, sender gets const_casted to lose the const anyway.


Log in to reply