ich sitze derzeit unter Zeitdruck an einem Prog, in dem ich an einer Stelle mit mehreren QList's hantieren muss.
Meine Hauptklasse enthält eine QList vom Typ "song", eine Struktur:
Code: Alles auswählen
struct song
{
QLabel *title;
int position;
int counter;
bool jumped;
};
Code: Alles auswählen
QList<song> *songlist;
Code: Alles auswählen
songlist = new QList<song>();
Code: Alles auswählen
void favs::initPlayList(int count)
{
for(int i=0;i<count;i++)
{
song tmpSong;
QString tmpString = "Song " + QString::number(i);
tmpSong.title = new QLabel(tmpString);
tmpSong.position = i;
tmpSong.jumped = false;
tmpSong.counter = rand()%100;
songlist->append(tmpSong);
}
}
Code: Alles auswählen
void favs::fillGrid()
{
playlistLayout->addWidget(new QLabel("<b>Nr</b>"),0,0);
playlistLayout->addWidget(new QLabel("<b>Song</b>"),0,1);
playlistLayout->addWidget(new QLabel("<b>Counter</b>"),0,2);
playlistLayout->addWidget(new QLabel("<b>Jumped?</b>"),0,3);
for(int i=0;i<songlist->size();i++)
{
playlistLayout->addWidget(new QLabel(QString::number(i)),i+1,0);
playlistLayout->addWidget(songlist->at(i).title,i+1,1);
playlistLayout->addWidget(new QLabel(QString::number(songlist->at(i).counter)),i+1,2);
QString tmpString = "";
if(songlist->at(i).jumped)
{
tmpString = tr("jumped");
}
else
{
tmpString = tr("played");
}
playlistLayout->addWidget(new QLabel(tmpString),i+1,3);
}
}
So, und dann habe ich eine Funktion, die nach einem bestimmten Muster aus diese QList eine neue erzeugt:
Code: Alles auswählen
void favs::generateList()
{
QList<song> *tmpList = new QList<song>; //temporäre Arbeitskopie
QList<song> *tmpList2 = new QList<song>; //2. temporäre Arbeitskopie
QList<song> *first20 = new QList<song>; //Liste der ersten 20 Titel
tmpList = songlist;
int haufigste[10] = {0,0,0,0,0,0,0,0,0,0}; //die häufigsten 10 Songs
int favorites[5] = {0,0,0,0,0}; //die Favorites-First-Einträge
int act_haufigkeit = -1; //aktuelle Häufigkeit
int act_position = 0; //aktuelle Position in Liste
int last_haufigkeit = usageCountSpin->value()+1; //letzte Häufigkeit
int positionen[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int apos = 0;
//10 meistgehörte Stücke ermitteln
for(int j=0;j<10;j++)
{
for(int i=0;i<tmpList->size();i++)
{
if((tmpList->at(i).counter > act_haufigkeit)&&(tmpList->at(i).counter < last_haufigkeit))
{
act_position = i;
act_haufigkeit = tmpList->at(i).counter;
}
}
haufigste[j]=act_position;
last_haufigkeit = act_haufigkeit;
act_haufigkeit = 0;
act_position = 0;
}
//5 zufällige Titel daraus auswählen
bool done[10] = {false,false,false,false,false,false,false,false,false,false};
bool found = false;
for(int i=0;i<5;i++)
{
while(!found)
{
int rnd = rand()%9;
if(!done[rnd])
{
favorites[i]=haufigste[rnd];
found = true;
done[rnd] = true;
}
}
found = false;
}
//15 Titel zufällig in Playlist ordnen
tmpList2 = tmpList;
for(int i=0;i<15;i++)
{
int rnd = rand()%tmpList2->size();
first20->append(tmpList2->at(rnd));
tmpList2->erase(tmpList2->begin()+rnd);
}
//Die 5 Favorites per Zufall dazwischen schieben
for(int i=0;i<5;i++)
{
int rnd = rand()%(14+i);
first20->insert(rnd,tmpList->at(favorites[i]));
}
//Liste speichern
songlist = new QList<song>(*first20);
fillGrid();
}
Jetzt zum Problem:
1. Die generierte Liste verweist manchmal auf ungültige Speicherpositionen vom Typ 0x00000000
2. Beim Einfügen der neuen QList in das Layout mit "fillGrid()" bleiben immer 5,6 oder 7 Elemente leer.
Wenn jemand ne Idee hat, woran das liegen könte, wäre das klasse.
Danke im Voraus,
Prof. MAAD