Results 1 to 13 of 13

Thread: How to obtain string from TextCursor whenever it is double clicked

  1. #1
    Join Date
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Question How to obtain string from TextCursor whenever it is double clicked

    Hi there, when user double clicks on QTextCursor in QTextEdit, I am looking for a way to get a string from that QTextCursor.

    Qt Code:
    1. class ContactsView(QtGui.QTextEdit):
    2. def __init__(self, parent=None):
    3. QtGui.QTextEdit.__init__(self, parent)
    4. #some initialization
    5.  
    6. def insert(self, name):
    7. cursor = self.textCursor()
    8. path = "image/icons/Messenger_blue32.png"
    9. icon = QtGui.QPixmap(path)
    10. image = icon.toImage()
    11. cursor.insertImage(image)
    12. cursor.insertText(name)
    13. self.setTextCursor(cursor)
    14. self.ensureCursorVisible()
    15. end = "<br />"
    16. fragment = QtGui.QTextDocumentFragment.fromHtml(end)
    17. self.textCursor().insertFragment(fragment)
    18.  
    19. def mouseDoubleClickEvent(self,event):
    20. cursor = self.cursorForPosition(event.pos())
    21. BlockUnderCursor = 2
    22. cursor.select(BlockUnderCursor)
    23. name = cursor.selectedText()
    24. print name
    To copy to clipboard, switch view to plain text mode 

    and when I run this program, I am getting this run time error;

    ContactsView.py", line 24, in mouseDoubleClickEvent
    print name
    UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffc' in position 0: ordinal not in range(128)
    I believe I am getting this error because there are more than strings in cursor.
    all I am interested is that I want to get a string from TextCursor whenever it is double clicked.
    Could anyone give me an advise? any inputs would be very appreciated.
    thank you for consideration.

    regards
    Naoya

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

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    User is not clicking on QTextCursor. QTextCursor doesn't have a visual representation. In general your code seems correct. The problem is probably that you can't print the QString, you need to do a proper conversion to ascii (try name.toByteArray().constData())
    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
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    Hi wysota, thank you so much for an input. I tried the conversion that you advised. but i got a following error;

    ContactsView.py", line 91, in mouseDoubleClickEvent
    print str(name).toByteArray().constData()
    UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffc' in position 0: ordinal not in range(128)
    Do you know why? again, thank you for your time and very appreciate if you could give me an input.

    regards
    Naoya

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

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    Hmm... did it even compile? There is no such thing as toByteArray() for QString

    Should be:
    Qt Code:
    1. print name.toAscii().constData()
    To copy to clipboard, switch view to plain text mode 
    And don't surround name with str(), it will again try to do the conversion on QString which will fail.
    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
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    Hi wysota, yes it now gives me a string
    Qt Code:
    1. print name.toAscii()
    To copy to clipboard, switch view to plain text mode 
    and the output was:
    ?name@email.com??name2@email.org??name3@email.com? ?name4@email.com?
    these are a list of ALL contacts in TextEdit. but what I wanted was I only wanted to get one name which is just double clicked.
    I thought
    Qt Code:
    1. cursor = self.cursorForPosition(event.pos())
    To copy to clipboard, switch view to plain text mode 
    gives me a cursor which is right under the pos()...


    here is my main window where I call ContactsView
    Qt Code:
    1. self.contactList = ContactsView.ContactsView(self,self)
    2. #some initialization...
    3. for name in buddyList:#buddyList is a list of contacts
    4. self.contactList.insert(name)#here inserts and displays contacts in contactList
    To copy to clipboard, switch view to plain text mode 

    in this list, I want to double click a name and get a string which name is clicked.
    Do you have any idea?
    thank you so much for your advises and always appreciate all.

    best regards
    Naoya
    Last edited by naoyamakino; 22nd July 2009 at 23:10.

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

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    My idea would be to use QListView instead of QTextEdit

    And following your idea - you're selecting the whole block under cursor (in human terms - the whole paragraph of the text) whereas you'd probably want to select WordUnderCursor or do parsing of the text yourself if your contacts contain word separator characters.
    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. The following user says thank you to wysota for this useful post:

    naoyamakino (23rd July 2009)

  8. #7
    Join Date
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    Hi wysota, thank you for the idea. and I am very close!!I switched QTextEdit to QListView and now I can select an item, display with icons;

    Qt Code:
    1. class ContactsView(QtGui.QListView):
    2. def __init__(self,buddyList, parent=None):
    3. QtGui.QListView.__init__(self, parent)
    4. self.buddyList = buddyList
    5. self.iStandardModel =QtGui.QStandardItemModel(self)
    6. def insert(self, name):
    7. path = "image/icons/Messenger_blue32.png"
    8. icon = QtGui.QIcon()
    9. icon.addPixmap(QtGui.QPixmap(path), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    10. item = QtGui.QStandardItem(icon,name)
    11. item.setEditable(False)
    12. self.iStandardModel.appendRow(item)
    13. self.setModel(self.iStandardModel)
    To copy to clipboard, switch view to plain text mode 

    but I'm facing a same issue; I can't still find a way to get a selected QStandardItem...
    there is a isSelectable function but I could not find things like isSelected().

    I tried something like this:
    Qt Code:
    1. self.contactList = ContactsView.ContactsView(self,self)
    2. if(buddyList is not None):
    3. for name in buddyList:
    4. self.contactList.insert(name)
    5. self.connect(self.contactList.selectionModel(),
    6. QtCore.SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),
    7. self.slot)
    8. ...
    9. def slot(self, selected, deselected):
    10. print str(selected), "items selected"
    To copy to clipboard, switch view to plain text mode 
    but this dose not print any useful information:
    <PyQt4.QtGui.QItemSelection object at 0x03A77810> items selected
    this prints out which ever I choose an item.

    Could you please give me an advise again?
    I very much appreciate your kindness and support.

    regards
    Naoya

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

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    You are abusing print and str(). Don't do that, Qt objects are incomatible with them. Look at documentation of QItemSelection and extract needed data using appropriate methods from its API. QItemSelection::indexes() might be a good place to start.
    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.


  10. #9
    Join Date
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    Hi wysota, thank your for a reply. but I think I am stucked. I have been looking at API but I still cannot figure out how to determine which QStandardItem is selected on QListView. it seems like many people are having the same problem but noone provides a solution to this.

    Thank you so much for your help.
    Last edited by naoyamakino; 24th July 2009 at 01:17.

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

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    I have already given you the solution. QItemSelection::indexes() provides a list of indexes which can then be converted into items using QStandardItemModel::itemFromIndex()
    Please direct those "many people" to our forum, they will find their answers here
    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.


  12. #11
    Join Date
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    YES!!!!! it now works!!!
    Qt Code:
    1. def slot(self, selected, deselected):
    2. indexes = selected.indexes()
    3. for index in indexes:
    4. item = self.contactList.iStandardModel.itemFromIndex(index)
    5. print item.text()
    To copy to clipboard, switch view to plain text mode 

    you made my day! thank you so much!! I very much appreciate your support.
    Best regards

    Naoya

  13. #12
    Join Date
    Jul 2009
    Posts
    14
    Thanks
    1
    Qt products
    Platforms
    MacOS X Windows

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    Hi wysota, its me again. thank you for the help. and I am facing a new issue
    before I used to send a signal to determine if a selection is changed.
    Qt Code:
    1. self.connect(self.contactList.selectionModel(),
    2. QtCore.SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),
    3. self.slot)
    To copy to clipboard, switch view to plain text mode 

    but when I try something like;
    Qt Code:
    1. self.connect(self.contactList.selectionModel(),
    2. QtCore.SIGNAL("itemDoubleClicked(QItemSelection)"),
    3. self.usernameClick)
    4.  
    5. self.connect(self.contactList.selectionModel(),
    6. QtCore.SIGNAL("itemClicked(QItemSelection)"),
    7. self.usernameSelect)
    8.  
    9. def usernameClick(self, contact):
    10. print 'usernameclick'
    11. indexes = contact.indexes()
    12. for index in indexes:
    13. item = self.contactList.iStandardModel.itemFromIndex(index)
    14. self.usernameClickCallback(item.text())
    15.  
    16. def usernameSelect(self,contact):
    17. print 'select'
    To copy to clipboard, switch view to plain text mode 

    it does not send a signal. I need to send two signals; one for double clicking the item(to pop up a chat window) and one for selection(to delete a contact and so on).
    it this possible?

    best regards

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

    Default Re: How to obtain string from TextCursor whenever it is double clicked

    You can't emit signals that don't exist... double clicking on an item is handled by the QAbstractItemView subclass - see QAbstractItemView::doubleClicked().
    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.


Similar Threads

  1. Replies: 1
    Last Post: 17th July 2008, 15:42

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.