PDA

View Full Version : UI to preview pictures and music



Akira
15th December 2010, 12:21
Hi,

I just found out Qt not long ago and really like its interface. But it's kinda complicated when most of the examples are pure coding. I'm trying to do it with the design part...
Right now I'm making a file viewer. So I have a treeView, a graphicsView, a listView and buttons.

http://img816.imageshack.us/img816/654/42269357.jpg

I worked out how to put a directory browser in the treeView, and when a file clicked, the path goes to the lineEdit. Now I want the graphicsView to show the picture if one is clicked in the treeList. And the path of the music will go to the listView when button Add is pressed. Any advice how to do that?

I'd appreciate if you actually explain it so I can understand, I don't like "copy and paste" the code, it makes me feel very bad. :)

franz
15th December 2010, 15:23
You've reached the point where you need to start coding. It is a good sign that you feel bad just copy/pasting the code, but that shouldn't hold you from it when learning. If you are going to copy/paste, find out what the piece of code does. In your case it would be good to start learning about signals and slots (http://doc.trolltech.com/latest/signalsandslots.html). Then, since you are using views, learn about model/view programming (http://doc.trolltech.com/latest/model-view-programming.html).

We can say a lot about what you should do, but we can only be really helpful if we know what exactly you do not understand.

In any case:
Write a slot that copies the current string value from your line edit to the list view.
Write a slot that loads a picture from a file and passes it to the graphics view (look at the drop-site example for similar behavior).

Akira
15th December 2010, 15:54
Let's see, I have achieved getting the path when select an item. But GraphicsView widget is very confusing at the moment.


void FileBrowser::on_treeView_clicked(QModelIndex index)
{
QString path;
if(index.isValid())
{
ui->lineEdit->setText(model.filePath(index));
path = lineEdit.Text();
QGraphicsScene scene;
scene.addPixmap(path);
QGraphicsView view(&scene);
view.show();
}
}

So, I tried to display the item when it is clicked, but I can't get the path from lineEdit.

Added after 18 minutes:

I also got the path added to listView using this:


void FileBrowser::on_addButton_clicked()
{
QString musicPath;
path = QFileDialog::getOpenFileName(this,
"Choose music:",
QString::null,
QString::null);

ui->listWidget->addItem(musicPath);;
}

But it's not actually what I wanted, because I have to browse the file. But all the time Qt said "lineEdit is not declared in this scope". Can I use QLineEdit::copy() or something?

Akira
16th December 2010, 15:51
it has been solved!
basically I need to "access" the widget, not "connect" them. :D

It's even much much easier to use label to display the image, not GraphicsView so I'll take a note on that.

Last thing, now I have the path, can anyone tell me the quickest way to play the music with an event pushButton_clicked()?

I tried Phonon::MediaObject and music->play but it doesn't work... (connect2: ld has 1 exit status or something along that line)

Akira
16th December 2010, 18:10
QString path = ui->listWidget->currentItem()->text();
Phonon::MediaObject *music = Phonon::createPlayer(Phonon::MusicCategory,
Phonon::MediaSource(path));
music->play();
Error: collect2: ld returned 1 exit status

Akira
16th December 2010, 20:38
Solved.
Have to put QT += phonon in .pro file

Thank you very much.