PDA

View Full Version : How to make a ListBox?



Bong.Da.City
29th August 2010, 11:00
I am making a program.. In that program i had an browze button which you can select some files from an directory.. But what a really want is a List which will have some buttons ( add file,remove file,move up,move down) so the user can select files from multiply directoories... So i searched on Qt Designer for the keyword "list" and what i have found is KEditLIstBox.... But i do not know how to use it.. Can you tell me how? Is KEditListBox the best choice?

Please do not answer me with just a link.. That's why i asked at the Newbie section....

squidge
29th August 2010, 13:08
KEditListBox is for KDE. Unless you want to write a KDE-application, I'd advise you to use QListWidget or its base class QListView instead.

Bong.Da.City
29th August 2010, 14:29
I do not have QListWidget but i have QListView...
Did you see the screenshot before? That's the exact list i want...
I do not think that QListWidget or the QListView are what i want..

squidge
29th August 2010, 15:22
What do you mean, you do not have QListWidget ? It's part of Qt...

Both QListWidget and QListView will do what you want - they will provide a list of items (ListBox), which is what you ask in your topic title.

You will of course have to add the buttons yourself, but thats the easy part :)

Bong.Da.City
29th August 2010, 15:42
yes ok.. but how with the press of the button an item be added on the listbox? ( the same with remove,move up,move down)

Astronomy
29th August 2010, 16:28
You mean/asking?
right click the Button and select "Go TO Slot"? there you can code..

myListbox->appenditem(..) or ->add(..) or whatever the help for the Class QListWidget says.. :)

greetz A.

Bong.Da.City
29th August 2010, 17:05
Yes i know how to go to slot :P I am not so noob..
Anyway what are you telling me is strange.. Look, an idea is with the click of the button "ADD" search for files and then add them to the list..
i know the first part it is simple..


path = QFileDialog::getOpenFileNames(this, tr("Files"), QDir::currentPath(), tr("*.jpg *.png *.gif *.bmp"));

But then how to add them to the ListWidget?

Talei
29th August 2010, 17:30
From Your posts I can only assume that You are lacking in understanding what MVC (Model View Controller) is.
Basically QListView is a View for the model. QListView don't store Your data, it's only a way to display it to the user in list manner. (thats why this class name is *View, QTableView will display Your data in table manner, QComboBox will add combobox). The data is stored in the so called model, and simply saying it is independent (from the View) way of storing Your information/data. Idea behind this is that You can represent the same model in different way (depending on the View/Delegates).
Examples:
QListView:
51145115
QTableView:
5116
QComboBox:
5117
Ass You can see I used the same model to display my "Data" in different way's. (depending on the needs)

QListWidget is different then QListView, because it actually store that data that You want to display. For Your needs probably QListWidget is simplest way to go.

Place QListWidget on the main form and use this code to add entries to the QListWidget:


QStringList path = QFileDialog::getOpenFileNames(this, tr("Files"), QDir::currentPath(), tr("*.jpg *.png *.gif *.bmp"));

if( !path.isEmpty())
{
ui->listWidget->addItems( path );
}
This is the simplest way to add items to the QListWidget.

For the rest, You need to study MVC, or look as was pointed out earlier, into qt docs.

Bong.Da.City
29th August 2010, 18:02
Talei thanx for your time... What i want to do isn't select some files and then copy on a path.. what i want to do is :
When i press the add button a qfiledialog been shown ( searching for jpg,png,gif,bmp - and maybe on that filedialog when i click a picture the preview of this been show)

5118

and then the pictures that will be selected added to the listview... not copy them somewhere just add them to the listview... .

P.s I prefer the QListView...

squidge
29th August 2010, 18:26
then you need to subclass QFileDialog first and modify to your needs, and then once you have done that create a model that the QListView will use to obtain the data. It is upto you how to handle this data, and thus you decide on the implementation for adding and moving files in the list, as you will be holding that list.

Bong.Da.City
29th August 2010, 20:00
You know we are on the newbie section... Can you describe it more?

ChrisW67
30th August 2010, 03:23
In the slot connected to the Add button you open your file dialog and, if the user does not cancel, add the selectedFiles() to the model that underlies the QListView (or add it to a QListWidget). You should look at QStringListModel if what you want to display in your list box is the paths to the selected files with (perhaps) a small icon version of the picture. You could derive from QStringListModel or QAbstractListModel to create your own model if you want to do something more advanced.

The reference to subclassing QFileDialog above is to support your request for the file selection dialog to preview images.

You're probably going to continue getting broad descriptions like this until you read the documentation, try the examples, and try to build your solution. Then you should be able to ask specific questions with specific answers.

squidge
31st August 2010, 18:36
You know we are on the newbie section... Can you describe it more?

If we describe more you say "I'm not a noob :P", if we describe less you say "we are on the newbie section". We can't guess what you know or don't know, so we give you general advice and it's upto you to RTFM and come back with specific questions. If you don't know how to subclass an object, a good guide would be a C++ manual. If you don't know about models, then the Qt documentation talks about them plenty, and comes with lots of examples.