Results 1 to 3 of 3

Thread: QListWidgetItem Button to display text

  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default QListWidgetItem Button to display text

    So I figured out how I can put the text from the ListWidget into the textEdit but what I would like to do is after I select the item I then have to press a QPushButton before it will display the text into the textEdit box I tried using this:
    Qt Code:
    1. listWidget->addItem(new QListWidgetItem("Hello"));
    2. listWidget->addItem(new QListWidgetItem("World"));
    3. listWidget->addItem(new QListWidgetItem("Andrew"));
    4. listWidget->setSelectionMode(QAbstractItemView::MultiSelection);
    5. QModelIndex modelIndex = listWidget->rootIndex();
    6. listWidget->setCurrentIndex(modelIndex);
    7. textEdit->setReadOnly(true);
    8.  
    9.  
    10.  
    11. connect(listWidget,SIGNAL(itemActivated(QListWidgetItem*)),
    12. this,SLOT(listWidgetItemDoubleClicked(QListWidgetItem*)));
    13. connect(listWidget, SIGNAL(itemClicked(QListWidgetItem*)),
    14. this, SLOT(listWidgetItemClicked(QListWidgetItem*)));
    15. connect(pushButton,SIGNAL(clicked()),
    16. this,SLOT(button_pressed()));
    17. }
    18.  
    19.  
    20. void ListWidgetDialog::listWidgetItemClicked(QListWidgetItem *item)
    21. {
    22.  
    23. if(!textEdit->document()->isEmpty()){
    24. QTextCursor c = textEdit->textCursor();
    25. textEdit->setTextCursor(c);
    26. c.beginEditBlock();
    27. textEdit->insertPlainText(", "+item->text());
    28. c.endEditBlock();
    29. }
    30. if(textEdit->document()->isEmpty()){
    31. QTextCursor c = textEdit->textCursor();
    32. c.beginEditBlock();
    33. textEdit->insertPlainText(item->text());
    34. c.endEditBlock();
    35.  
    36. }
    37.  
    38. }
    39. void ListWidgetDialog::button_pressed(){
    40. void listWidgetItemClicked(QListWidgetItem*);
    41. textEdit->insertPlainText("HELLO I HAVE BEEN PRESSED");
    42. }
    To copy to clipboard, switch view to plain text mode 

    but it shows the items when I press them on the ListWidget. and then Shows the message "HELLO I HAVE BEEN PRESSED" when I press the button. I want it to display nothing when I press on the ListWidget, but when I press on the ListWidget then the Button it to display the item then the "hello" message.

  2. The following user says thank you to giblit for this useful post:

    fecub (18th November 2014)

  3. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QListWidgetItem Button to display text

    So what is the problem in doing so?

    Store the items selected in a QString instead of inserting in to QTextEdit, and when QPushButton is clicked insert that stored QString and "hello" message in the QTextEdit, and clear the QString.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  4. #3
    Join Date
    Feb 2013
    Posts
    71
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Platforms
    Windows

    Default Re: QListWidgetItem Button to display text

    Lol thanks idk why I didn't think of that
    EDIT: works perfectly now haha thanks again
    Last edited by giblit; 23rd March 2013 at 19:04.

Similar Threads

  1. QListWidgetItem text too long
    By jcr in forum Qt Programming
    Replies: 3
    Last Post: 20th October 2010, 00:19
  2. QListWidgetItem's icon and text orientation
    By circass in forum Qt Programming
    Replies: 5
    Last Post: 17th June 2010, 08:03
  3. Replies: 2
    Last Post: 21st May 2010, 14:32
  4. Rich Text Inside QListWidgetItem?
    By Jick in forum Newbie
    Replies: 1
    Last Post: 17th February 2010, 01:30
  5. QListWidgetItem Text Rect
    By js67257 in forum Qt Programming
    Replies: 2
    Last Post: 22nd August 2006, 12:02

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
  •  
Qt is a trademark of The Qt Company.