Results 1 to 7 of 7

Thread: Make QHeaderView Editable

  1. #1
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Default Make QHeaderView Editable

    I have been searching around the fourm for a way to make the QHeadView editable when double clicked. The best link I found had only had and example file and the link was broken. I have been searching on and off for two weeks or so.

    Anyway as I stated before I am trying to make a QHeaderView that is editable when double clicked. The best I have gotten it to create a line edit box and un hide it when a section is clicked. However none of the close edit signals are triggered when editting is done so I have no good way to pass the info of what was changed back to the dataset. I know that I need to use a delegate I just don't know how. I am writting in Python but at this point I will take help in any programming language.

    Below is a sample of what I have come up with

    Qt Code:
    1. class MyHeaderView(QtGui.QHeaderView):
    2.  
    3. def __init__(self,orientation,dataset,parent=None):
    4. super(MyHeaderView, self).__init__(orientation,parent)
    5. self.data = dataset
    6. # This block sets up the edit line by making setting the parent
    7. # to the Headers Viewport.
    8. self.line = QtGui.QLineEdit(parent=self.viewport()) #Create
    9. self.line.setAlignment(QtCore.Qt.AlignTop) # Set the Alignmnet
    10. self.line.setHidden(True) # Hide it till its needed
    11.  
    12. self.sectionedit = 0
    13.  
    14. # Connects to double click
    15. self.sectionDoubleClicked.connect(self.editHeader)
    16. self.line.editingFinished.connect(self.setHeaderData)
    17.  
    18. def editHeader(self,section):
    19.  
    20. # This block sets up the geometry for the line edit
    21. edit_geometry = self.line.geometry()
    22. edit_geometry.setWidth(self.sectionSize(section))
    23. edit_geometry.moveLeft(self.sectionViewportPosition(section))
    24. self.line.setGeometry(edit_geometry)
    25.  
    26. self.line.setHidden(False) # Make it visiable
    27. self.line.setFocus()
    28. self.sectionedit = section
    29.  
    30. def setHeaderData(self, section, orientation, value,
    31. role = QtCore.Qt.EditRole):
    32. # Update dataset
    33. pass
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Make QHeaderView Editable

    A delegate won't help you for QHeaderView. It doesn't have a full implementation of the model-view scheme and it doesn't use delegates. The only way to get an inline editor is what you're already trying -- put a line edit over the header section and intercept proper signals or reimplement event handlers. I would probably make the line edit a Qt::Popup and I would probably react to its close event as well as its returnPressed signal.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Make QHeaderView Editable

    Ok thanks for the help. I finally had time to get back to this project and tried making the QLineEdit a popup by setting its window flag I ran into problems however. This causes the line edit to show up at 0,0 absolute position, the upper left hand corner of my screen. On top of that it grabs focus when it shows up but then will not release it unless I click another window down on my applications bar(Windows), at which point it disappears it does reappear when I double click a section header.
    Below is the updated code, just one line. I am going to continue to work on this just though I should reply
    Qt Code:
    1. class MyHeaderView(QtGui.QHeaderView):
    2.  
    3. def __init__(self,orientation,dataset,parent=None):
    4. super(MyHeaderView, self).__init__(orientation,parent)
    5. self.data = dataset
    6. # This block sets up the edit line by making setting the parent
    7. # to the Headers Viewport.
    8. self.line = QtGui.QLineEdit(parent=self.viewport()) #Create
    9. self.line.setAlignment(QtCore.Qt.AlignTop) # Set the Alignmnet
    10. self.line.setHidden(True) # Hide it till its needed
    11. self.line.setWindowFlags(QtCore.Qt.Popup)
    12.  
    13. self.sectionedit = 0
    14.  
    15. # Connects to double click
    16. self.sectionDoubleClicked.connect(self.editHeader)
    17. self.line.editingFinished.connect(self.setHeaderData)
    18.  
    19. def editHeader(self,section):
    20.  
    21. # This block sets up the geometry for the line edit
    22. edit_geometry = self.line.geometry()
    23. edit_geometry.setWidth(self.sectionSize(section))
    24. edit_geometry.moveLeft(self.sectionViewportPosition(section))
    25. self.line.setGeometry(edit_geometry)
    26.  
    27. self.line.setHidden(False) # Make it visiable
    28. self.line.setFocus()
    29. self.sectionedit = section
    30.  
    31. def setHeaderData(self, section, orientation, value,
    32. role = QtCore.Qt.EditRole):
    33. # Update dataset
    34. pass
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Make QHeaderView Editable

    Most of what you describe is caused by the popup flag. If you reposition the element to its proper place it should behave correctly. If that's not what you want, you can resign from using the popup flag however you'll have hard time determining when to hide the widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Make QHeaderView Editable

    I tried using the popup flag but placing the line edit became troublesome. It seems that when I made it a popup it changes it positioning coordinates from being with reference to the widget to being global. This made it difficult to place the line edit in the right place. Also this made the line edit slightly larger than the header. So this got me thinking really all I need is to be able to connect the editingFinished signal to be connected to the setHeaderData slot. The initial problem of course is setHeaderData is expecting section orientation and value arguments from its caller. Is there a way to add those into my connecting declaration or is there some way to redefine the editingFinished signal to output these.

    I guess what I am asking is, is there a way to overload the editingFinshed signal or create a new signal that will output the proper arguments?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Make QHeaderView Editable

    Quote Originally Posted by mechsin View Post
    I tried using the popup flag but placing the line edit became troublesome. It seems that when I made it a popup it changes it positioning coordinates from being with reference to the widget to being global. This made it difficult to place the line edit in the right place.
    QWidget::mapToGlobal()
    So this got me thinking really all I need is to be able to connect the editingFinished signal to be connected to the setHeaderData slot.
    I don't think that's enough.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Make QHeaderView Editable

    I finally came up with a solution for this and wanted to post it here it seemed to be a common problem.

    Qt Code:
    1. class MetaHeaderView(QtGui.QHeaderView):
    2.  
    3. def __init__(self,orientation,parent=None):
    4. super(MetaHeaderView, self).__init__(orientation,parent)
    5. self.setMovable(True)
    6. self.setClickable(True)
    7. # This block sets up the edit line by making setting the parent
    8. # to the Headers Viewport.
    9. self.line = QtGui.QLineEdit(parent=self.viewport()) #Create
    10. self.line.setAlignment(QtCore.Qt.AlignTop) # Set the Alignmnet
    11. self.line.setHidden(True) # Hide it till its needed
    12. # This is needed because I am having a werid issue that I believe has
    13. # to do with it losing focus after editing is done.
    14. self.line.blockSignals(True)
    15. self.sectionedit = 0
    16. # Connects to double click
    17. self.sectionDoubleClicked.connect(self.editHeader)
    18. self.line.editingFinished.connect(self.doneEditing)
    19.  
    20. def doneEditing(self):
    21. # This block signals needs to happen first otherwise I have lose focus
    22. # problems again when there are no rows
    23. self.line.blockSignals(True)
    24. self.line.setHidden(True)
    25. oldname = self.model().dataset.field(self.sectionedit)
    26. newname = str(self.line.text())
    27. self.model().dataset.changeFieldName(oldname, newname)
    28. self.line.setText('')
    29. self.setCurrentIndex(QtCore.QModelIndex())
    30.  
    31. def editHeader(self,section):
    32. # This block sets up the geometry for the line edit
    33. edit_geometry = self.line.geometry()
    34. edit_geometry.setWidth(self.sectionSize(section))
    35. edit_geometry.moveLeft(self.sectionViewportPosition(section))
    36. self.line.setGeometry(edit_geometry)
    37.  
    38. self.line.setText(self.model().dataset.field(section).name)
    39. self.line.setHidden(False) # Make it visiable
    40. self.line.blockSignals(False) # Let it send signals
    41. self.line.setFocus()
    42. self.line.selectAll()
    43. self.sectionedit = section
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to make webView editable?
    By isamert in forum Qt Programming
    Replies: 2
    Last Post: 14th July 2010, 09:14
  2. how can I make a balloon to wrap the editable text?
    By learning_qt in forum Qt Programming
    Replies: 9
    Last Post: 20th January 2009, 15:09
  3. How to make QListWidget items editable?
    By montylee in forum Qt Programming
    Replies: 14
    Last Post: 3rd October 2008, 09:57
  4. how to set up a QHeaderView editable?
    By iguanna in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2008, 14:37
  5. make each row of table widget not editable??
    By darpan in forum Qt Programming
    Replies: 4
    Last Post: 16th October 2006, 10:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.