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 and edit mode
Forum Updated to NodeBB v4.3 + New Features

QTableWidget and edit mode

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 3 Posters 3.6k 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.
  • V Vlad02

    @JonB
    By the way, I have custom delegate:

    QWidget *MyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QLineEdit *editor = new QLineEdit(parent);     
        editor->setStyleSheet(StyleHelper::getEditorItemStyle());   
        connect(editor, &QLineEdit::editingFinished, this, &MyDelegate::commitAndCloseEditor);
        (void) option;      
        (void) index;       
        return editor;
    }
    
    void MyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    {
        QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
        lineEdit->setInputMask("HH");              
        lineEdit->setText(index.data().toString());
    }
    
    void MyDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    {
        QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
        model->setData(index, lineEdit->text());
    }
    
    void MyDelegate::commitAndCloseEditor()
    {
        QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
        emit commitData(editor);
        emit closeEditor(editor);
    }
    

    Maybe it's help you :)

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #11

    @Vlad02 said in QTableWidget and edit mode:

    Maybe it's help you :)

    That may save me all the work I am halfway through!

    Since you already have the createEditor() delegate written, all I want you to do there is something like:

    QLineEdit *editor = new QLineEdit(parent); 
    QTimer::singleShot(100, this, [editor]() { editor->deselect(); } );
    

    That's the idea, does it work? [I have tried this now under Ubuntu 22.04, Qt 5.15, and it works as intended.]

    V 1 Reply Last reply
    0
    • JonBJ JonB

      @Vlad02 said in QTableWidget and edit mode:

      Maybe it's help you :)

      That may save me all the work I am halfway through!

      Since you already have the createEditor() delegate written, all I want you to do there is something like:

      QLineEdit *editor = new QLineEdit(parent); 
      QTimer::singleShot(100, this, [editor]() { editor->deselect(); } );
      

      That's the idea, does it work? [I have tried this now under Ubuntu 22.04, Qt 5.15, and it works as intended.]

      V Offline
      V Offline
      Vlad02
      wrote on last edited by
      #12

      @JonB Hm, I have error that QLineEdit has no member named textCursor. I use Qt 5.6.0. Could there not be such a function or method?

      JonBJ 1 Reply Last reply
      0
      • V Vlad02

        @JonB Hm, I have error that QLineEdit has no member named textCursor. I use Qt 5.6.0. Could there not be such a function or method?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #13

        @Vlad02
        Re-read my code above, I corrected that (it was for QTextEdit not QLineEdit), it's even simpler now :)

        V 1 Reply Last reply
        0
        • JonBJ JonB

          @Vlad02
          Re-read my code above, I corrected that (it was for QTextEdit not QLineEdit), it's even simpler now :)

          V Offline
          V Offline
          Vlad02
          wrote on last edited by
          #14

          @JonB Huh, I'm sorry, I have some problems :`)
          da252eb0-b707-41ba-84da-3c602e9cadf3-image.png
          I work on windows 10, may be related to this?

          JonBJ 1 Reply Last reply
          0
          • V Vlad02

            @JonB Huh, I'm sorry, I have some problems :`)
            da252eb0-b707-41ba-84da-3c602e9cadf3-image.png
            I work on windows 10, may be related to this?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #15

            @Vlad02
            This is because you are Qt 5.6 (why such an old version??). I still think you're supposed to have

            void QTimer::singleShot(int msec, const QObject *context, Functor functor)
            

            and I thought my lambda would match as a Functor? Sigh. Do you really need to stick on Qt 5.6, it's not a good idea...?

            You will have to try something like:

            QTimer::singleShot(100, [editor]() { editor->deselect(); } );
            

            But I suppose that gives the same error message??

            You need a lambda here really to pass the editor in context. I never used Qt 5.6 and I don't know what is acceptable for that, maybe an [old!] expert will see and comment...?

            V 1 Reply Last reply
            0
            • JonBJ JonB

              @Vlad02
              This is because you are Qt 5.6 (why such an old version??). I still think you're supposed to have

              void QTimer::singleShot(int msec, const QObject *context, Functor functor)
              

              and I thought my lambda would match as a Functor? Sigh. Do you really need to stick on Qt 5.6, it's not a good idea...?

              You will have to try something like:

              QTimer::singleShot(100, [editor]() { editor->deselect(); } );
              

              But I suppose that gives the same error message??

              You need a lambda here really to pass the editor in context. I never used Qt 5.6 and I don't know what is acceptable for that, maybe an [old!] expert will see and comment...?

              V Offline
              V Offline
              Vlad02
              wrote on last edited by
              #16

              @JonB
              No, this is work, thanks!!!

              But what if, for example, I need to select the text of only the currently selected cell, but at the same time put everything in edit mode? And while doing this, I want to switch between cells through the Tab key. And when you switch to a cell, so that the text is highlighted completely in it. Is it difficult to implement?
              d94c792f-0442-4f61-aab1-35d974109005-image.png
              I highlighted this with the mouse, but I would like this when switching between cells via Tab

              JonBJ 1 Reply Last reply
              0
              • V Vlad02

                @JonB
                No, this is work, thanks!!!

                But what if, for example, I need to select the text of only the currently selected cell, but at the same time put everything in edit mode? And while doing this, I want to switch between cells through the Tab key. And when you switch to a cell, so that the text is highlighted completely in it. Is it difficult to implement?
                d94c792f-0442-4f61-aab1-35d974109005-image.png
                I highlighted this with the mouse, but I would like this when switching between cells via Tab

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #17

                @Vlad02 said in QTableWidget and edit mode:

                I need to select the text of only the currently selected cell, but at the same time put everything in edit mode?

                For that one, in createEditor() detect whether index refers to "the currently selected cell" and do not do the singleShot() to clear the selection for that one.

                You are asking me too many questions now! I don't know what the Tab key does, I don't know whether that highlights all the content of the new cell or not, and I don't know whether you want the opposite behaviour! I have shown a possible approach using a delay timer to deselect/select in a QLineEdit, maybe you need something like this in your Tab case too. You will have to play.

                V 1 Reply Last reply
                0
                • JonBJ JonB

                  @Vlad02 said in QTableWidget and edit mode:

                  I need to select the text of only the currently selected cell, but at the same time put everything in edit mode?

                  For that one, in createEditor() detect whether index refers to "the currently selected cell" and do not do the singleShot() to clear the selection for that one.

                  You are asking me too many questions now! I don't know what the Tab key does, I don't know whether that highlights all the content of the new cell or not, and I don't know whether you want the opposite behaviour! I have shown a possible approach using a delay timer to deselect/select in a QLineEdit, maybe you need something like this in your Tab case too. You will have to play.

                  V Offline
                  V Offline
                  Vlad02
                  wrote on last edited by
                  #18

                  @JonB Okay, thanks so much! You helped me a lot :) Have a good day!

                  JonBJ 1 Reply Last reply
                  0
                  • V Vlad02

                    @JonB Okay, thanks so much! You helped me a lot :) Have a good day!

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #19

                    @Vlad02
                    If necessary explain exactly what you want now, because I don't understand from what you wrote. So far as I know the Tab key will move to edit the next editable field, does it do that? If then you want it to select or deselect the whole of the new cell's edit's content, I think the QLineEdit will get some "focus" event or signal? Maybe you could put something on that to then do de/selection, if that is the issue?

                    V 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Vlad02
                      If necessary explain exactly what you want now, because I don't understand from what you wrote. So far as I know the Tab key will move to edit the next editable field, does it do that? If then you want it to select or deselect the whole of the new cell's edit's content, I think the QLineEdit will get some "focus" event or signal? Maybe you could put something on that to then do de/selection, if that is the issue?

                      V Offline
                      V Offline
                      Vlad02
                      wrote on last edited by Vlad02
                      #20

                      @JonB said in QTableWidget and edit mode:

                      Tab key will move to edit the next editable field

                      Yes, you're right.

                      Just for convenience, it would be nice, when switching to each next cell, to select all the text in it in order to write something new, and not erase the old one and then write down the new one, that's what I'm talking about. And in the previous cell, just the new text remains and that's it, without any selections

                      P.S. Sorry for lengthy responses, I can write every 10 minutes :)

                      JonBJ 1 Reply Last reply
                      0
                      • V Vlad02

                        @JonB said in QTableWidget and edit mode:

                        Tab key will move to edit the next editable field

                        Yes, you're right.

                        Just for convenience, it would be nice, when switching to each next cell, to select all the text in it in order to write something new, and not erase the old one and then write down the new one, that's what I'm talking about. And in the previous cell, just the new text remains and that's it, without any selections

                        P.S. Sorry for lengthy responses, I can write every 10 minutes :)

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #21

                        @Vlad02
                        If I understand what you want (still not sure). https://stackoverflow.com/questions/2804115/how-to-connect-focus-event-from-qlineedit shows to subclass QLineEdit in order to override its focusIn/OutEvents and emit a signal for them. Which you could attach a slot to to do the same deselect/selectAll() principle as in the timer. Is that what you are looking to do?

                        V 1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Vlad02
                          If I understand what you want (still not sure). https://stackoverflow.com/questions/2804115/how-to-connect-focus-event-from-qlineedit shows to subclass QLineEdit in order to override its focusIn/OutEvents and emit a signal for them. Which you could attach a slot to to do the same deselect/selectAll() principle as in the timer. Is that what you are looking to do?

                          V Offline
                          V Offline
                          Vlad02
                          wrote on last edited by
                          #22

                          @JonB This looks interesting. I'll try to work with this, thanks!

                          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