Page 3 of 3 FirstFirst 123
Results 41 to 51 of 51

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

  1. #41
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

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

    See, you found a solution your own. Unfortunately it is not a good one since you change the content of two item and do not swap them. But since you are able to create local variables (row and currentIndex) you can now use the suggested functions and then you really have it.

  2. #42
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

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

    Quote Originally Posted by Lykurg View Post
    See, you found a solution your own.
    No way :-). A person who can't undestand takeItem and insertItem does not understand how to move model indexes which is conceptually more difficult.

    http://www.qtforum.org/article/34168...st-widget.html

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

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

    First i know the the takeItem and insertitem but i didn't know how to use it to move up a selected item.. Anyway yes i cannot claim that i founded my own.. Somebody ( not from here) had give me an example that he had made for somebody in the past and now i understand the hole thing...

  4. #44
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

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

    Quote Originally Posted by Bong.Da.City View Post
    and now i understand the hole thing...
    Then you don't mind explaining it?

  5. #45
    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: Help me on creating a List Widget...

    And that's what remains of "I will analyze it, understand it and will not copy & paste it". Sometimes I'm wondering if it makes any sense to actually help people instead of just putting out a sign "will solve Qt problems for Big Money. No questions asked".
    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.


  6. #46
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

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

    And just think of all the other people one could have helped in the time spend on this thread...

  7. #47
    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 tbscope View Post
    Then you don't mind explaining it?
    Ok i do not mind...
    Qt Code:
    1. if( ui->listWidget->selectionModel()->hasSelection() ){
    To copy to clipboard, switch view to plain text mode 

    If you had selected something on the listWidget then do the following...

    Qt Code:
    1. if( ui->listWidget->currentIndex().row() != 0 ){
    2. int row = ui->listWidget->currentIndex().row();
    To copy to clipboard, switch view to plain text mode 

    That means that if you have selected something then int row and row will be the currentIndex.row... ( When you select for example the second item of an listWidget the row will be 2... )

    Qt Code:
    1. QModelIndex currentIndex = ui->listWidget->currentIndex();
    To copy to clipboard, switch view to plain text mode 
    currentIndex will be the item that you selected

    Qt Code:
    1. QModelIndex indexAbove = ui->listWidget->model()->index( row-1, 0, QModelIndex() );
    To copy to clipboard, switch view to plain text mode 
    indexAbove will be the item above the one you selected as it is row-1 ( we declared before that the row is the row of the selected item )

    Qt Code:
    1. QString selectedString = ui->listWidget->currentIndex().data().toString();
    To copy to clipboard, switch view to plain text mode 
    Here we convert the currentindex to string

    Qt Code:
    1. ui->listWidget->model()->setData( currentIndex, indexAbove.data().toString() );
    To copy to clipboard, switch view to plain text mode 
    Here we say to replace the current index with the indexAbove

    Qt Code:
    1. ui->listWidget->model()->setData( indexAbove, selectedString );
    To copy to clipboard, switch view to plain text mode 
    And here to replace the indexAbove with the current index

    Qt Code:
    1. ui->listWidget->setCurrentIndex( indexAbove );
    To copy to clipboard, switch view to plain text mode 
    Finnally with this function we say the one that we have selected at the start once it has gone an indexabove to be selected again..

  8. #48
    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: Help me on creating a List Widget...

    Three simple questions:
    1. why are you using selectionModel() and currentIndex()?
    2. what does QModelIndex() mean?
    3. what will happen if you try to move down the last item in your list?

    Or the two questions gathered into one - how are selectionModel() and currentIndex() related to QListWidget?

    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).
    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.


  9. #49
    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

  10. #50
    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; 11th September 2010 at 00:45. Reason: reformatted to look better

  11. #51
    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, 13:54
  2. List Widget need help!
    By patrick772goh in forum Newbie
    Replies: 1
    Last Post: 15th August 2007, 11:25
  3. Creating a widget on a QGraphicsView
    By maverick_pol in forum Qt Programming
    Replies: 4
    Last Post: 9th August 2007, 18:54
  4. Stupid list widget won't clear
    By scwizard in forum Qt Programming
    Replies: 14
    Last Post: 5th July 2007, 17:03
  5. Custom widget in list view
    By fusoin23 in forum Qt Programming
    Replies: 1
    Last Post: 18th November 2006, 15: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.