Results 1 to 20 of 51

Thread: Help me on creating a List Widget...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me on creating a List Widget...

    Quote Originally Posted by wysota View Post
    1. why are you using selectionModel() and currentIndex()?
    Because selectionModel() provides the QModelIndexes() that are selected from the ListView.

    Quote Originally Posted by wysota View Post
    2. what does QModelIndex() mean?
    Look here.. QModelIndex()

    Quote Originally Posted by wysota View Post
    3. what will happen if you try to move down the last item in your list?
    Nothing. The condition in the down() function stops from executing code based on the position from the selected index compared to the bottom of the list.

    Quote Originally Posted by wysota View Post
    Or the two questions gathered into one - how are selectionModel() and currentIndex() related to QListWidget?
    QListView inherts QAbstractItemView with contains these methods.

    Quote Originally Posted by wysota View Post
    By the way, your code is very likely going to misbehave once you will want to do something more than just exchange strings from items (because you are not moving any items here).
    You are correct, but it does exactly what i want from it to do

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Help me on creating a List Widget...

    Bong.Da.City.

    You have since found a convoluted method to achieve the goal. I think it would be far more useful to you in the long run to work out why the forum crowd think it is more convoluted and/or not quite what you wanted. I appreciate that you are a newbie so I will take some time to walk through this.

    Back here tbscope gave the conceptual picture of what was needed. That picture contains strong hints as to the exact code required to achieve your aim. You had a go at it but missed a crucial point that you identified yourself a few posts later. As others have pointed out, there is significance to the return value of takeItem() and it is central to the solution the forum has in mind. You are on the right track here.

    In an homage to tbscope's ASCII art abilities I have prepared a few of my own. In these diagrams "->" means "points at".

    At start with four items in the list:
    Qt Code:
    1. listWidget -> +-- QListWidget ------+
    2. | |
    3. | |
    4. | |
    5. | |
    6. +---------------------+
    To copy to clipboard, switch view to plain text mode 
    QListWidget::currentRow() returns 2 and corresponds to item C


    Remove the item with:
    Qt Code:
    1. QListWidgetItem *item = listWidget->takeItem(2);
    2.  
    3.  
    4. listWidget -> +-- QListWidget ------+
    5. | |
    6. | |
    7. | |
    8. | | QListWidgetItem C <- item
    9. | |
    10. +---------------------+
    To copy to clipboard, switch view to plain text mode 
    Item now contains a pointer to the removed item. The item still exists in memory but does not exist in the list. You still have access to the removed item through this pointer.

    QListWidget::currentRow() returns 2 (still) but points at a different item D. This is why people were telling you that currentRow() was not what you thought it was in your attempt.

    Put the item back into the list with:
    Qt Code:
    1. listWidget->insertItem(1, item);
    2.  
    3.  
    4. listWidget -> +-- QListWidget ------+
    5. | |
    6. | |
    7. | |
    8. | |
    9. +---------------------+
    To copy to clipboard, switch view to plain text mode 

    There are still open issues with the approach above. What happens if there is only one item in the list, or you try to move the first/last item up/down? How do you work out where to insertItem()? Where do you want currentRow() to be after the move?

    Programming tools like Qt give you a palette of building blocks that can be assembled into a multitude of shapes, just like a bucket of Lego blocks. These blocks as well designed to give you a lot of flexibility without forcing you to reinvent everything from scratch every time. You still need to provide the big picture of what you want to build and work out how the little blocks go together to make the bigger pieces. Much of this comes with practice, as does designing your own intermediate assemblies in a way that can be reused (this orderable list widget combination could be something you reuse).
    Last edited by ChrisW67; 10th September 2010 at 23:45. Reason: reformatted to look better

  3. #3
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Help me on creating a List Widget...

    Thanks ChrisW67 for taking the time and answer me... But i had created my List widget example with add,remove,remove all,move up and move down work!! Thanks anyway ...

Similar Threads

  1. How can I create a widget list as Qt Designer?
    By ricardo in forum Qt Programming
    Replies: 5
    Last Post: 3rd May 2009, 12:54
  2. List Widget need help!
    By patrick772goh in forum Newbie
    Replies: 1
    Last Post: 15th August 2007, 10:25
  3. Creating a widget on a QGraphicsView
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 9th August 2007, 17:54
  4. Stupid list widget won't clear
    By scwizard in forum Qt Programming
    Replies: 14
    Last Post: 5th July 2007, 16:03
  5. Custom widget in list view
    By fusoin23 in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2006, 14:09

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