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. QTableWidget editing signal
QtWS25 Last Chance

QTableWidget editing signal

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.8k Views
  • 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.
  • I Offline
    I Offline
    iw2lsi
    wrote on last edited by
    #1

    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
    
    JonBJ 1 Reply Last reply
    0
    • I iw2lsi

      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
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @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.

      I 1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #3

        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);
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        I 1 Reply Last reply
        2
        • JonBJ JonB

          @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.

          I Offline
          I Offline
          iw2lsi
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • VRoninV VRonin

            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);
            
            I Offline
            I Offline
            iw2lsi
            wrote on last edited by
            #5

            @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

            VRoninV 1 Reply Last reply
            0
            • I iw2lsi

              @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

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              @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;
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              JonBJ I 2 Replies Last reply
              1
              • VRoninV VRonin

                @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;
                
                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #7

                @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?

                VRoninV 1 Reply Last reply
                0
                • VRoninV VRonin

                  @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;
                  
                  I Offline
                  I Offline
                  iw2lsi
                  wrote on last edited by
                  #8

                  @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
                  
                  1 Reply Last reply
                  0
                  • JonBJ JonB

                    @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?

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by VRonin
                    #9

                    @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.

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    1 Reply Last reply
                    1
                    • JonBJ JonB referenced this topic on

                    • Login

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