PDA

View Full Version : Help me on creating a List Widget...



Bong.Da.City
9th September 2010, 21:33
First i want to say.. Do not give me any Class reference... I had looked many times THE class reference of listwidget and i cannot understand it...

So what i want to do is to create a list widget... Beside the listwidget i want to have four buttons... Add, Remove, Move up, Move down ....
So when i press add it will search for files and when you click open it will add the items you selected to the listwidget... When you click on an item on the listwidget and click remove the item be deleted... With move up go up an item and with move down go down an item...

But the most importand thing... What i realy want to do is the items that the listwidget have be written to a txt ( the path of this files.. and on every item a new line be created on the txt)...( Do not ask why.. I have a reason )
I now the command how to write something on a txt.. The only think i do not know is the hole thing with the listwidget..

The school is starting in 3 days so i would appresiate it if you created an example for me so i can understand it and make the app before school starts..

squidge
9th September 2010, 21:56
So basically you want us to do your work for you rather than you learning how to do it yourself?

Sorry, but that's not how we work here.

If you don't understand something, explain what you don't understand maybe we can help. Don't just say "Do this for me and don't ask why."

Urthas
9th September 2010, 22:01
First i want to say.. Do not give me any Class reference... I had looked many times THE class reference of listwidget and i cannot understand it...

Perhaps you could clarify what exactly is causing you grief in the documentation?


So what i want to do is to create a list widget... Beside the listwidget i want to have four buttons... Add, Remove, Move up, Move down ....
So when i press add it will search for files and when you click open it will add the items you selected to the listwidget... When you click on an item on the listwidget and click remove the item be deleted... With move up go up an item and with move down go down an item...

That is a lot of functionality, but most of it is concerned with the signals and slots paradigm (http://doc.trolltech.com/3.3/signalsandslots.html), I think. Specifically, you need to connect the clicked() signal of the various buttons to slots that manipulate the QListWidget. Most likely this will involve subclassing QListWidget.


The school is starting in 3 days so i would appresiate it if you created an example for me so i can understand it and make the app before school starts..

Well, now I'm curious...

Bong.Da.City
9th September 2010, 22:05
Do not ask why, is refer do not ask why i want to write the paths of the listwidget on a txt...

If you know how to do all of that you would created on maybe 10 minutes... And then i would take it and i would understand it... so simple..
But if you answer me with simple answers i would bomb you with many questions as it is sure i would not understand it... That's why i posted on the Newbie section...

This is a forum.. It is better not everytime i am asking a question you answer with a simple Class reference or complaing things...

Urthas
9th September 2010, 22:11
If you know how to do all of that you would created on maybe 10 minutes... And then i would take it and i would understand it... so simple..But if you answer me with simple answers i would bomb you with many questions as it is sure i would not understand it... That's why i posted on the Newbie section... This is a forum.. It is better not everytime i am asking a question you answer with a simple Class reference or complaing things...

I suggest that you read the following, very carefully: How to ask questions the smart way (http://www.catb.org/esr/faqs/smart-questions.html). Skip right to the introduction if you like.

squidge
9th September 2010, 22:13
What you are asking for is rather simple, and like already stated, is mostly just to throw some things on a form and connect them together with signals. To do a half-assed attempt would take maybe 15 minutes, but to do it properly as a reusable component maybe 20 - 30.

But thats not the point. You don't learn by asking other people to do what you want to do. You learn by doing it yourself.

This is a forum, true. It is idea to communicate. So please communicate what is your problem with creating the above. Where are you stuck?

wysota
9th September 2010, 22:19
The school is starting in 3 days so i would appresiate it if you created an example for me so i can understand it and make the app before school starts..

Sorry, we don't do homeworks here. If anyone gives you a solution to your homework, I will personally ban them off the forum, so I wouldn't expect more than hints that will help you solve your school assignments.

BTW. It would take me 5 minutes to do what you ask, as I already have a component (or maybe two) that does most of it (apart saving to a file but that's really simple). I can even tell you the component is publically available as part of a widget set I have written. But I don't think your supervisor would be happy if you used it.

Bong.Da.City
9th September 2010, 22:39
wysota why are you always so bad with me?

Me and i think every newbie would understand much easier something if you maked an example... See you can make me the example in 5 minutes... Why not to send it to me? Is this something so boring for you? Come on... And why, why if you made the example and sent it to me.. Why do you think i would not learn tehn how to use it... I wouln't just copy it but i would try to understand everything... And if i didn't understand a part of the code i would ask.....

Urthas
9th September 2010, 22:45
wysota why are you always so bad with me?

Me and i think every newbie would understand much easier something if you maked an example... See you can make me the example in 5 minutes... Why not to send it to me? Is this something so boring for you? Come on... And why, why if you made the example and sent it to me.. Why do you think i would not learn tehn how to use it... I wouln't just copy it but i would try to understand everything... And if i didn't understand a part of the code i would ask.....

I'm going to completely ignore the fact that you continue to not care about working on the problem in any way yourself. Instead, I'll focus on something constructive: *you still haven't asked a concrete question*. We still have no idea what about the QListWidget you don't understand. Until you can formulate a decent question, how can you possibly expect a useful answer?

I would like to add that browsing this forum makes me think of avocado. Serious discussion may now resume. :p

squidge
9th September 2010, 22:48
I think you need to re-read his message. It would take him 5 minutes because he has already done it in the past and made it a reusable component. Otherwise it would probably take 20 - 30 minutes.

But, the question still stands, for the amount of time you spend on this forum pleading that someone do your work for you, you could have done it yourself, or at least be a large portion into having it completed.

If you can't understand the first thing you must do, you are not going to understand a fully working example.

wysota
9th September 2010, 22:54
wysota why are you always so bad with me?
My job here is to enforce some rules which have kept this site running for almost five years now. Don't take it personally.


Why not to send it to me?
I would have to ban myself then and I don't think I would want to do that. I don't even know if this is technically possible.


I wouln't just copy it but i would try to understand everything... And if i didn't understand a part of the code i would ask.....
So far you have not done anything yourself to solve your problem. I suggest you write an example and then we help you fix it, not the other way round.

Talei
9th September 2010, 22:57
I really love this thread :).
Almost whole solution for the problem has already been posted in this thread (granted using non standard widgets but he never stated that he need to use them), but he still don't get it.

Sorry for being sarcastic but Bong.Da.City I find Your attitude very rude, not only in this thread but in other as well, and did You ever thanked Anyone who gave You help? NO.

ChrisW67
9th September 2010, 23:09
The list and button plumbing has been addressed several times.

The "write to file" requirement would just require accessing the items in the widget, in order, and writing them to file at the appropriate time (you don't tell us when that should be). Hints: QListWidget::item(), QListWidget::count() and QTextStream

When you have written something to attempt this you should be able to ask targeted questions.

Bong.Da.City
9th September 2010, 23:48
I really love this thread :).
Almost whole solution for the problem has already been posted in this thread (granted using non standard widgets but he never stated that he need to use them), but he still don't get it.

Sorry for being sarcastic but Bong.Da.City I find Your attitude very rude, not only in this thread but in other as well, and did You ever thanked Anyone who gave You help? NO.

I do not say thanks because on this particular forum i here everytime complains and never good answers...
Anyway right now i am trying to make the AddButton.. I will post questions when i have problems...

Talei
10th September 2010, 00:43
I do not say thanks because on this particular forum i here everytime complains and never good answers...
Anyway right now i am trying to make the AddButton.. I will post questions when i have problems...

You are mistaken, in this very thread You have:
src code for custom listWidget (You can easily figure out how to do what You want to do simply analysing the code),
ChrisW67 kindly posted needed classes to complete Your task (just read description of the classes/methods),
and few others ppl are willing to help You if You ask specific question (just post some code/actual problem).

And You are wrong with first statment, You should say that You don't say thanks because no one wrote finished program for YOU.
And answer to Your question is also in one of the threads here on the forum.

Bong.Da.City
10th September 2010, 01:03
I do not want ready programms.. ok?
List widget is just a small part that i need for my application... Not the hole app...

I say thanks only when i solve the problem i have :) ..

ChrisW67
10th September 2010, 02:22
Actually, looking back, the add button functionality is addressed the last time you asked this question:
http://www.qtcentre.org/threads/33716-How-to-make-a-ListBox

wysota
10th September 2010, 02:34
Actually, looking back, the add button functionality is addressed the last time you asked this question:
http://www.qtcentre.org/threads/33716-How-to-make-a-ListBox

Wow... two weeks just to create an editable list of files (seems like a wallpaper management app)... I think I'll go to bed now :p

Lykurg
10th September 2010, 06:57
There are nice examples in the docs which cover your problem but you forbid to give doc references. It's a pity but I respect this.

((small side node:

Me and i
Ehm, is that an alarm signal of schizophrenia? Considering the red eyes of your avatar I am a little scared right now.))

@squidge: You caused me a stroke with your renaming! I thought that it couldn't be that there is suddenly a guy with more than 1500 post I never have heard of. Then finally I saw your footer. Thank heaven, I am relieved. Please, don't change your name too often!

squidge
10th September 2010, 07:50
@Lykurg: I don't plan on changing it again, don't worry :) The first name was a temporary name of which I've no idea how I thought about it. I thought it looked quite strange now so I change to match some of my other forums and looks slightly less weird :)

@Bong.Da.City: If you were not so rude when people are trying to help you, you might get better replies when you ask questions. You may also consider reading the "How to ask questions the smart way" that was pointed out above.

SixDegrees
10th September 2010, 08:28
First i want to say.. Do not give me any Class reference... I had looked many times THE class reference of listwidget and i cannot understand it...



Then you need to overcome your inability to understand simple explanations of how things work. You could start by reading through the numerous tutorials and examples, including the source code, already available for list views, table views, the model-view architecture and related material. There's a lot of it, but you should be able to work your way from end to end in a few hours. If you focus your attention sufficiently, that should be more than enough information to let you implement your task.

As others have said, though: don't expect other people to do the work you're unwilling to do yourself. If you want someone to code to your specifications, I occasionally take on consulting jobs. I normally charge $150 to $200 per hour for such work, with a 20 hour minimum. Let me know if you're interested, although my time is pretty much spoken for over the next month.

wysota
10th September 2010, 08:59
@squidge: You caused me a stroke with your renaming! I thought that it couldn't be that there is suddenly a guy with more than 1500 post I never have heard of. Then finally I saw your footer. Thank heaven, I am relieved. Please, don't change your name too often!
Ha ha.... I was right when asking for the signature :cool:

Bong.Da.City
10th September 2010, 09:00
Wow... two weeks just to create an editable list of files (seems like a wallpaper management app)... I think I'll go to bed now :p
Actually i am not trying to make the listwisget 2 weeks... Lol..
I do other thinks on the app...

Bong.Da.City
10th September 2010, 09:16
I have some problems on creating the List Widget.. For once please answer me...

I had created a List Widget.. There are five buttons.. Add,remove,remove all,move up,mode down... The add,remove and remove all buttons work perfect and do their job.. But i do not have any idea on how to make the move up button to move up the currentItem and the movedown button to move down the currentItem.. Any ideas?

wysota
10th September 2010, 09:19
Use QListWidget::takeItem() to remove the item from the list and reinsert it again in the new position.

Bong.Da.City
10th September 2010, 09:33
I am using this..

ui->listWidget->takeItem(int row);

But i do not know what exactly to replace with the int row in order the current item to move up?

tbscope
10th September 2010, 09:42
+----------+ +----------+ +----------+
| | | | | |
| Item 1 | | Item 1 | | Item 1 |
| Item 2 | | Item 3 | | Item 3 |
| Item 3 |--> item = takeItem(2) -->| Item 4 |--> insertItem(3, item) -->| Item 2 |
| Item 4 | | | | Item 4 |
| | | | | |
+----------+ +----------+ +----------+

Bong.Da.City
10th September 2010, 09:48
tbscope thanks for the reply.... But look what i have done:


void MainWindow::on_moveupButton_clicked()
{
ui->listWidget->takeItem(ui->listWidget->currentRow());
ui->listWidget->insertItem(ui->listWidget->currentRow() + 1,ui->listWidget->currentRow());
}

With errors:
error: invalid conversion from ‘int’ to ‘const char*’
error: initializing argument 1 of ‘QString::QString(const char*)’

wysota
10th September 2010, 10:24
If you remove an item from the list, it is no longer there so asking for currentRow() after removing the item will certainly not give you what you want.


With errors:
error: invalid conversion from ‘int’ to ‘const char*’
error: initializing argument 1 of ‘QString::QString(const char*)’
Please, please take a look at documentation of the methods you are using prior to using them and check if signatures of them match.

Lykurg
10th September 2010, 10:28
after taking the item, ui->listWidget->currentRow() is not longer what you expect! Create local variables with the taken item (as in tbscops anwser!) and the row. Then handle with them.

EDIT: refresh a page before answering could be helpful...

Bong.Da.City
10th September 2010, 10:39
Look what it is saying..

QListWidgetItem * QListWidget::takeItem ( int row )

Removes and returns the item from the given row in the list widget; otherwise returns 0.
Items removed from a list widget will not be managed by Qt, and will need to be deleted manually.

It doesn't say anything about taking the item and inserting it one item up... :(:(:(:(
So how can i remove an item and then insert it without deleting it from the list?

Bong.Da.City
10th September 2010, 11:02
after taking the item, ui->listWidget->currentRow() is not longer what you expect! Create local variables with the taken item (as in tbscops anwser!) and the row. Then handle with them.

EDIT: refresh a page before answering could be helpful...

I had understand whay tbscops says... But how to create local variables with the taken item and the row? What do you mean?

wysota
10th September 2010, 11:15
But how to create local variables with the taken item and the row?
Please do yourself (and us) a favour and learn at least a tiny bit of C++ before trying yourself against Qt. We will not teach you how to program in C++.

Bong.Da.City
10th September 2010, 11:27
wysota i had done the add,remove, and remove all button to work alone....
But i really have no idea on how to make the move down and move up buttons to work...
I had looked every line of the class reference ... searched on google and nothing... try it my own and nothing..

I am sure that you probably know... So why not tell it to me? It is just 2 lines ( probably ) .. And if you hade done it another time it is just a copy paste for you...
Finally i founded a widget that have move up and move down ( http://qt-apps.org/content/show.php/Item+Selection+Widget?content=125518 )

P.s if it is a problem to post link to qt-apps tell me and i will delete the link....

wysota
10th September 2010, 12:17
I had looked every line of the class reference ... searched on google and nothing... try it my own and nothing..
It's not about the reference, it's not about google. It's about you not knowing how to do software development. You have been given all the help required, you know which methods to use but you don't know how to use them. You are asking how to create local variables and stuff like that. You can't even match types of return values of methods to argument types of other methods. Do you really consider yourself a skilled developer? You should take a piece of paper and a pencil and think about an algorithm for solving your problem - without any classes and methods, just a concept. Only then come back to a computer and implement the solution you came up with.


So why not tell it to me? It is just 2 lines ( probably ) .. And if you hade done it another time it is just a copy paste for you...
The more I think about it the more I come to a conclusion that you should fail your programming class for your professor to see how lousy programming teacher he is.

Bong.Da.City
10th September 2010, 12:36
Just to know that i am a 16 year old kid... I am learning programming only for 1.5 months and i already know many things..

I am sure that you was an absolute noob on your first 1.5 months... :mad:

I think that the newbie section is not as it should be.. When we say newbie we say newbie... Tell me when somebody says that he is a beginner how is he supposed to
understand something when you give him a class reference.. ? He will understood with an example or a good answer..

And the worst thing... How a beginner is supposed to try to do things when you say phrases like:

Do you really consider yourself a skilled developer? You should take a piece of paper and a pencil and think about an algorithm for solving your problem - without any classes and methods, just a concept. Only then come back to a computer and implement the solution you came up with.

The more I think about it the more I come to a conclusion that you should fail your programming class for your professor to see how lousy programming teacher he is.

:mad:

It suppose that you encourage beginners... ( But as i can see it is like you say that your are the best programmer and I suck... Nice :mad::mad: )

squidge
10th September 2010, 13:02
When we say "Newbie" we mean someone who has not used Qt before and are taking there first steps into using it. We don't mean someone whose previous programming language was BASIC or whatever (or even just standard C, for that matter).

To learn Qt, there is a prerequisite of a good knowledge of C++, so instead of whinging on this forum you should learn C++ first, and then come back and attempt Qt.

wysota
10th September 2010, 13:06
I am sure that you was an absolute noob on your first 1.5 months... :mad:
Actually no (my first language was BASIC and I learned it quite fast myself apart from some quirks related to reading stored data using READ and DATA directives) but that's not the point. The point is you are trying to prove us that you do know how to use C++ while in reality you don't and you should learn it first before using Qt because Qt development requires you to posess some knowledge about object oriented programming and C++ in particular. Otherwise you will keep having problems such as the ones you have now.


I think that the newbie section is not as it should be.. When we say newbie we say newbie...
Newbie in Qt, not newbie in programming. Please take a break from Qt and spend two days studying C++. You will see how much faster you will be solving your problems.


He will understood with an example or a good answer..
If an example is beyond your comprehension you will not understand it nor learn from it. You could only blindly copy it but it would get you nowhere.


It suppose that you encourage beginners... ( But as i can see it is like you say that your are the best programmer and I suck... Nice :mad::mad: )
I don't consider myself a "best" programmer but I consider myself a decent Qt programming teacher and somehow my students don't have problems with grasping Qt when I explain it to them. But they do have a background in C++ or some other OO language. This is something your programming teacher should have taught you before letting you work with Qt.

If you want to become a good developer, you have to do things in the right order. You can't build a roof of a house before laying down foundations for the house. And currently you are lacking those foundations when it comes to programming.

Bong.Da.City
10th September 2010, 13:25
Ok you have right in that point wysota...
I know that i must have knowledge of C++.. i have bought a book and it is quite well.. Because of the school i do not have any time to read it... And if i read it i will forget it as i will start programming again in 2 years because i have to focus on studying on my 2 last classes on high school... Anyway i do not have any teacher for now ( As I am 16 :P )...

I have still 4 days to sit all day in my laptop and try to do things with my app... After this 4 days i will not use my laptop again for 2 years ( Maybe on christmas or on summer a bit) because i want to pass on a programming school here in greece... The point is that on this 4 days i would like to finish my app so i wouldn't think when i study," ohh i have so many things to do in my app "... Finishing the app includes have a List Widget to it...

Anyway i had post this topic and i thought someone would make me a simple example, take it and understand, put it on my app and finish my app...:/

Bong.Da.City
10th September 2010, 13:43
Here is the answer for move up...


void MainWindow::on_moveupButton_clicked()
{
if( ui->listWidget->selectionModel()->hasSelection() ){
if( ui->listWidget->currentIndex().row() != 0 ){
int row = ui->listWidget->currentIndex().row();
QModelIndex currentIndex = ui->listWidget->currentIndex();
QModelIndex indexAbove = ui->listWidget->model()->index( row-1, 0, QModelIndex() );

QString selectedString = ui->listWidget->currentIndex().data().toString();
ui->listWidget->model()->setData( currentIndex, indexAbove.data().toString() );
ui->listWidget->model()->setData( indexAbove, selectedString );
ui->listWidget->setCurrentIndex( indexAbove );
}
}
}

Here is the answer for move down...


void MainWindow::on_movedownButton_clicked()
{
if( ui->listWidget->selectionModel()->hasSelection() ){
if( ui->listWidget->currentIndex().row() != ui->listWidget->model()->rowCount()-1 ){
int row = ui->listWidget->currentIndex().row();
QModelIndex currentIndex = ui->listWidget->currentIndex();
QModelIndex indexBelow = ui->listWidget->model()->index( row+1, 0, QModelIndex() );

QString selectedString = ui->listWidget->currentIndex().data().toString();
ui->listWidget->model()->setData( currentIndex, indexBelow.data().toString() );
ui->listWidget->model()->setData( indexBelow, selectedString );
ui->listWidget->setCurrentIndex( indexBelow );
}
}
}

Lykurg
10th September 2010, 14:08
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.

tbscope
10th September 2010, 14:13
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/how-can-i-make-the-move-up-and-move-down-button-to-work-on-my-list-widget.html

Bong.Da.City
10th September 2010, 14:22
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...

tbscope
10th September 2010, 14:23
and now i understand the hole thing...

Then you don't mind explaining it?

wysota
10th September 2010, 14:23
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".

marcvanriet
10th September 2010, 14:50
And just think of all the other people one could have helped in the time spend on this thread... ;)

Bong.Da.City
10th September 2010, 14:53
Then you don't mind explaining it?

Ok i do not mind...


if( ui->listWidget->selectionModel()->hasSelection() ){

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


if( ui->listWidget->currentIndex().row() != 0 ){
int row = ui->listWidget->currentIndex().row();

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


QModelIndex currentIndex = ui->listWidget->currentIndex();
currentIndex will be the item that you selected


QModelIndex indexAbove = ui->listWidget->model()->index( row-1, 0, QModelIndex() );
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 )


QString selectedString = ui->listWidget->currentIndex().data().toString();
Here we convert the currentindex to string


ui->listWidget->model()->setData( currentIndex, indexAbove.data().toString() );
Here we say to replace the current index with the indexAbove


ui->listWidget->model()->setData( indexAbove, selectedString );
And here to replace the indexAbove with the current index


ui->listWidget->setCurrentIndex( indexAbove );
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..

wysota
10th September 2010, 15:02
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).

Bong.Da.City
10th September 2010, 19:57
1. why are you using selectionModel() and currentIndex()?

Because selectionModel() provides the QModelIndexes() that are selected from the ListView.



2. what does QModelIndex() mean?
Look here.. QModelIndex() (http://doc.trolltech.com/4.6/qmodelindex.html#details)


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.


Or the two questions gathered into one - how are selectionModel() and currentIndex() related to QListWidget?
QListView inherts QAbstractItemView with contains these methods.


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

ChrisW67
10th September 2010, 23:34
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 (http://www.qtcentre.org/threads/34067-Help-me-on-creating-a-List-Widget...?p=157606#post157606) 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 (http://www.qtcentre.org/threads/34067-Help-me-on-creating-a-List-Widget...?p=157620#post157620) 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:


listWidget -> +-- QListWidget ------+
| |
| 0 QListWidgetItem * | -> QListWidgetItem A
| |
| 1 QListWidgetItem * | -> QListWidgetItem B
| |
| 2 QListWidgetItem * | -> QListWidgetItem C
| |
| 3 QListWidgetItem * | -> QListWidgetItem D
+---------------------+

QListWidget::currentRow() returns 2 and corresponds to item C


Remove the item with:


QListWidgetItem *item = listWidget->takeItem(2);


listWidget -> +-- QListWidget ------+
| |
| 0 QListWidgetItem * | -> QListWidgetItem A
| |
| 1 QListWidgetItem * | -> QListWidgetItem B
| |
| | QListWidgetItem C <- item
| |
| 2 QListWidgetItem * | -> QListWidgetItem D
+---------------------+


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:


listWidget->insertItem(1, item);


listWidget -> +-- QListWidget ------+
| |
| 0 QListWidgetItem * | -> QListWidgetItem A
| |
| 1 QListWidgetItem * | -> QListWidgetItem C
| |
| 2 QListWidgetItem * | -> QListWidgetItem B
| |
| 3 QListWidgetItem * | -> QListWidgetItem D
+---------------------+


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

Bong.Da.City
11th September 2010, 14:51
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 ...