PDA

View Full Version : Phonon::MediaSource



Lendamand
8th September 2010, 04:52
It is my first time to post a question here, and I have a simple problem about Phonon::MediaSource class. Codes like this:


QString string = "It's ok.mp3";
Phonon::MediaSource source1(string);
Phonon::MediaSource source2(string);
if(source1 == source2)
{
qDebug("equal");
}
else
{
qDebug("not equal");
}

The output is "not equal".
why above codes will return false? And how to get true when I compare two MediaSources?

By the way, I am using the QList<Phonon::MediaSource> to store the MediaSources in my project, but I don't want to stroe the same source in it, I know that it should use the method QList::contains(MediaSource source) to decide whether the source has existed. of course, I fail. It will store the same mediasource more than once. I think it may have a relation to the 'MediaSource::operator== ( const MediaSource & other )'


QList<Phonon::MediaSource> sources;
Phonon::source1("it's ok.mp3");
Phonon::source2("it's ok.mp3");
if(!sources.contains(source1))
{
sources.append(source1);
}
if(!sources.contains(source2))
{
sources.append(source2);
}

Look forward to any suggestions or help. Thank you!

tbscope
8th September 2010, 05:55
This is the code for Mediasource:


MediaSource::MediaSource(const MediaSource &rhs)
: d(rhs.d)
{
}

MediaSource &MediaSource::operator=(const MediaSource &rhs)
{
d = rhs.d;
return *this;
}

bool MediaSource::operator==(const MediaSource &rhs) const
{
return d == rhs.d;
}

This means that the operator == only returns true when the pointer to the private structure is the same.
This in turn means you can only get true when you use the = operator or the MediaSource(otherMediaSource) constructor.

In your case, the private structures are different and will always return false

Suggestion:

if (source1.url() == source2.url()) // maybe also check the type

Lendamand
8th September 2010, 13:34
Thank you for tbscope's help, but I still have a problem. How can I make sure that QList just store the only one mediasoure, while I must construct my source using 'MediaSource(QString fileName)'? The following is my QT codes which may be similar to the QT's demo:music player.


void MainWindow::addfile()
{ QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Music Files"), QDesktopServices::storageLocation(QDesktopServices ::MusicLocation));
if (files.isEmpty())
return;
int index = sources.size();
foreach (QString string, files)
{
Phonon::MediaSource source(string);
if(!sources.contains(source)) //how can I make sure sources just store the same mediasource one time?
{
sources.append(source);
}
}
if (!sources.isEmpty() && sources.size()>index)
metaInformationResolver->setCurrentSource(sources.at(index));
}