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

QTableWidget and edit mode

Scheduled Pinned Locked Moved Unsolved General and Desktop
22 Posts 3 Posters 3.1k 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.
  • M mpergand
    10 Apr 2023, 22:40

    @Vlad02
    Looks like the selection mode is MultiSelection
    Try:
    tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);

    V Offline
    V Offline
    Vlad02
    wrote on 11 Apr 2023, 06:40 last edited by
    #4

    @mpergand This is not work in edit mode

    1 Reply Last reply
    0
    • J JonB
      10 Apr 2023, 20:51

      @Vlad02
      Maybe this just shows that when the QTextEdit editor widget is created it has its content selected? You might have to set a timer to deselect content after a short time delay? Unless someone suggests better.

      V Offline
      V Offline
      Vlad02
      wrote on 11 Apr 2023, 06:58 last edited by
      #5

      @JonB Can you show what you mean?

      J 1 Reply Last reply 11 Apr 2023, 07:13
      0
      • V Vlad02
        11 Apr 2023, 06:58

        @JonB Can you show what you mean?

        J Offline
        J Offline
        JonB
        wrote on 11 Apr 2023, 07:13 last edited by JonB 4 Nov 2023, 07:16
        #6

        @Vlad02
        The sort of thing I have in mind is: I do not think you can access the editing widget created during QAbstractItemView::openPersistentEditor(). That means you would have to create your own QStyledItemDelegate for QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate). You would have that create and return a QTextEdit for QAbstractItemDelegate::createEditor(). If that still marks its content as selected upon initial display, then you would put it a short QTimer to deselect the content just after it is made visible.

        Could you at least confirm that what you are seeing is just a selection which can be removed? From the picture you show, confirm that if you click into (each of) those blue 00 cells the text becomes unselected and the blue disappears? And stays disappeared even if you click into another cell?

        If I have time this morning I might give it a try to show code. It's a bit of work all for the sake of not wanting what appears to be a default selection, but it may be what you have to do.

        V 1 Reply Last reply 11 Apr 2023, 07:18
        0
        • J JonB
          11 Apr 2023, 07:13

          @Vlad02
          The sort of thing I have in mind is: I do not think you can access the editing widget created during QAbstractItemView::openPersistentEditor(). That means you would have to create your own QStyledItemDelegate for QAbstractItemView::setItemDelegate(QAbstractItemDelegate *delegate). You would have that create and return a QTextEdit for QAbstractItemDelegate::createEditor(). If that still marks its content as selected upon initial display, then you would put it a short QTimer to deselect the content just after it is made visible.

          Could you at least confirm that what you are seeing is just a selection which can be removed? From the picture you show, confirm that if you click into (each of) those blue 00 cells the text becomes unselected and the blue disappears? And stays disappeared even if you click into another cell?

          If I have time this morning I might give it a try to show code. It's a bit of work all for the sake of not wanting what appears to be a default selection, but it may be what you have to do.

          V Offline
          V Offline
          Vlad02
          wrote on 11 Apr 2023, 07:18 last edited by
          #7

          @JonB cef7fee5-4d4c-49c5-8cad-88b0c6c1fda7-image.png
          Okay, I will wait your example. Thanks!

          J 1 Reply Last reply 11 Apr 2023, 07:23
          0
          • V Vlad02
            11 Apr 2023, 07:18

            @JonB cef7fee5-4d4c-49c5-8cad-88b0c6c1fda7-image.png
            Okay, I will wait your example. Thanks!

            J Offline
            J Offline
            JonB
            wrote on 11 Apr 2023, 07:23 last edited by JonB 4 Nov 2023, 07:26
            #8

            @Vlad02 said in QTableWidget and edit mode:

            Okay, I will wait your example. Thanks!

            LOL, you are optimistic! :) OK, I will start playing with it now....

            One question. Your code shows you making every cell in every row and column editable, but the picture only shows one row having been made editable/showing selected contents. Is that really the case?? (Because if so something else is going on.) Or does the picture come from code really making just one row editable?

            And one further thing. The normal behaviour is to mark all cells as being editable, not switch that on and off, but only to go into edit mode on one cell at a time, when you click into that cell to edit. At that point it changes into a QTextEdit, and changes back again to plain text when finished editing. But you want an interface where a whole row goes into edit mode at a time, is that the requirement?

            V 2 Replies Last reply 11 Apr 2023, 07:32
            0
            • J JonB
              11 Apr 2023, 07:23

              @Vlad02 said in QTableWidget and edit mode:

              Okay, I will wait your example. Thanks!

              LOL, you are optimistic! :) OK, I will start playing with it now....

              One question. Your code shows you making every cell in every row and column editable, but the picture only shows one row having been made editable/showing selected contents. Is that really the case?? (Because if so something else is going on.) Or does the picture come from code really making just one row editable?

              And one further thing. The normal behaviour is to mark all cells as being editable, not switch that on and off, but only to go into edit mode on one cell at a time, when you click into that cell to edit. At that point it changes into a QTextEdit, and changes back again to plain text when finished editing. But you want an interface where a whole row goes into edit mode at a time, is that the requirement?

              V Offline
              V Offline
              Vlad02
              wrote on 11 Apr 2023, 07:32 last edited by Vlad02 4 Nov 2023, 07:36
              #9

              @JonB I check flag

              if(!secFunc->getIsBigMemory())
              

              If condition is true, other rows not editable.
              Regarding editing. The matter is that I have 2 modes: in one all lines are available, in another only one. And yes, I need to change the values ​​of each cell in a row. The bottom line is that I need to be able to open the editor of all cells by pressing a key from the keyboard, and not by double clicking the mouse

              It may be clearer if I say that this is conditionally the memory of a certain processor, into which I write commands to perform tasks (I write an emulator)

              1 Reply Last reply
              1
              • J JonB
                11 Apr 2023, 07:23

                @Vlad02 said in QTableWidget and edit mode:

                Okay, I will wait your example. Thanks!

                LOL, you are optimistic! :) OK, I will start playing with it now....

                One question. Your code shows you making every cell in every row and column editable, but the picture only shows one row having been made editable/showing selected contents. Is that really the case?? (Because if so something else is going on.) Or does the picture come from code really making just one row editable?

                And one further thing. The normal behaviour is to mark all cells as being editable, not switch that on and off, but only to go into edit mode on one cell at a time, when you click into that cell to edit. At that point it changes into a QTextEdit, and changes back again to plain text when finished editing. But you want an interface where a whole row goes into edit mode at a time, is that the requirement?

                V Offline
                V Offline
                Vlad02
                wrote on 11 Apr 2023, 07:42 last edited by Vlad02 4 Nov 2023, 07:43
                #10

                @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 :)

                J 1 Reply Last reply 11 Apr 2023, 07:59
                0
                • V Vlad02
                  11 Apr 2023, 07:42

                  @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 :)

                  J Offline
                  J Offline
                  JonB
                  wrote on 11 Apr 2023, 07:59 last edited by JonB 4 Nov 2023, 08:27
                  #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 11 Apr 2023, 08:50
                  0
                  • J JonB
                    11 Apr 2023, 07:59

                    @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 11 Apr 2023, 08:50 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?

                    J 1 Reply Last reply 11 Apr 2023, 08:51
                    0
                    • V Vlad02
                      11 Apr 2023, 08:50

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

                      J Offline
                      J Offline
                      JonB
                      wrote on 11 Apr 2023, 08:51 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 11 Apr 2023, 09:02
                      0
                      • J JonB
                        11 Apr 2023, 08:51

                        @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 11 Apr 2023, 09:02 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?

                        J 1 Reply Last reply 11 Apr 2023, 09:15
                        0
                        • V Vlad02
                          11 Apr 2023, 09:02

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

                          J Offline
                          J Offline
                          JonB
                          wrote on 11 Apr 2023, 09:15 last edited by JonB 4 Nov 2023, 09:19
                          #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 11 Apr 2023, 09:23
                          0
                          • J JonB
                            11 Apr 2023, 09: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 Offline
                            V Offline
                            Vlad02
                            wrote on 11 Apr 2023, 09:23 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

                            J 1 Reply Last reply 11 Apr 2023, 09:31
                            0
                            • V Vlad02
                              11 Apr 2023, 09:23

                              @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

                              J Offline
                              J Offline
                              JonB
                              wrote on 11 Apr 2023, 09:31 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 11 Apr 2023, 09:36
                              0
                              • J JonB
                                11 Apr 2023, 09:31

                                @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 11 Apr 2023, 09:36 last edited by
                                #18

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

                                J 1 Reply Last reply 11 Apr 2023, 09:40
                                0
                                • V Vlad02
                                  11 Apr 2023, 09:36

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

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 11 Apr 2023, 09:40 last edited by JonB 4 Nov 2023, 09:41
                                  #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 11 Apr 2023, 09:48
                                  0
                                  • J JonB
                                    11 Apr 2023, 09:40

                                    @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 11 Apr 2023, 09:48 last edited by Vlad02 4 Nov 2023, 09:50
                                    #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 :)

                                    J 1 Reply Last reply 11 Apr 2023, 10:01
                                    0
                                    • V Vlad02
                                      11 Apr 2023, 09:48

                                      @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 :)

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 11 Apr 2023, 10:01 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 11 Apr 2023, 10:22
                                      0
                                      • J JonB
                                        11 Apr 2023, 10:01

                                        @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 11 Apr 2023, 10:22 last edited by
                                        #22

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

                                        1 Reply Last reply
                                        0

                                        13/22

                                        11 Apr 2023, 08:51

                                        • Login

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