Witam, mam funkcję, która czyta wyrazy z pliku tekstowego i je wyświetla. Chciałbym ją zamienić, tak żeby nie czytała całego pliku, a losowo wybierała 5 wyrazów.
DragWidget::DragWidget(QWidget *parent)
: QWidget(parent)
{
QFile dictionaryFile(":/dictionary/words.txt");
dictionaryFile.open(QIODevice::ReadOnly);
QTextStream inputStream(&dictionaryFile);
int x = 5;
int y = 5;
while (!inputStream.atEnd()) {
QString word;
inputStream >> word;
if (!word.isEmpty()) {
DragLabel *wordLabel = new DragLabel(word, this);
wordLabel->move(x, y);
wordLabel->show();
wordLabel->setAttribute(Qt::WA_DeleteOnClose);
x += wordLabel->width() + 2;
if (x >= 195) {
x = 5;
y += wordLabel->height() + 2;
}
}
}
QPalette newPalette = palette();
newPalette.setColor(QPalette::Window, Qt::white);
setPalette(newPalette);
setAcceptDrops(true);
setMinimumSize(400, qMax(200, y));
setWindowTitle(tr("Draggable Text"));
}
Próbowałem zmienić instrukcję while na jakąś pętle for, ale nie udawało mi się. Proszę o pomoc, dzięki z góry