How to get text value of custom QListWidgetItem ?
-
void MainWindow::addnew() { QDateTime time = QDateTime::currentDateTime(); QString stime = time.toString("yyyy-MM-dd hh:mm:ss"); QString surl = dialognew->ui->lineEditURL->text(); QString sfn=dialognew->ui->lineEditFilename->text(); QString spath=dialognew->ui->lineEditPath->text(); Form *form=new Form; form->ui->labelFilename->setText(sfn); form->ui->labelURL->setText(surl); form->ui->labelURL->adjustSize(); form->ui->labelPath->setText(spath); form->ui->labelTimeCreate->setText(stime); QListWidgetItem *LWI = new QListWidgetItem(ui->listWidget); ui->listWidget->addItem(LWI); ui->listWidget->setItemWidget(LWI,form); LWI->setSizeHint(QSize(1200,30)); form->download(surl); } void MainWindow::on_actionPause_triggered() { // need: form->reply->abort(); } void MainWindow::on_actionDelete_triggered() { //need: form->reply->abort(); ui->listWidget->takeItem(ui->listWidget->currentRow()); } void MainWindow::on_actionDirectory_triggered() { //need: QString path=form->ui->labelPath.text(); QString path=ui->listWidget->item(ui->listWidget->currentRow())->text(); qDebug() << path; // get "" QDesktopServices::openUrl(QUrl::fromLocalFile(path)); } void Form::download(QString surl) { timer.start(); QUrl url(surl); QNetworkAccessManager manager; QEventLoop loop; reply = manager.get(QNetworkRequest(url)); connect(reply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(updateProgress(qint64,qint64))); connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); QString filepath=ui->labelPath->text()+"/"+ui->labelFilename->text(); qDebug() << filepath; QFile file(filepath); file.open(QIODevice::WriteOnly); file.write(reply->readAll()); file.close(); } void Form::updateProgress(qint64 bytesReceived, qint64 bytesTotal) { int elapse = timer.elapsed(); QTime t(0,0,0); t=t.addMSecs(elapse); QString selapse=t.toString("hh:mm:ss"); int speed=bytesReceived*1000/elapse; ui->labelSize->setText(QString("%1 / %2").arg(sbytes(bytesReceived)).arg(sbytes(bytesTotal))); totalBytes=bytesTotal; ui->progressBar->setMaximum(bytesTotal); ui->progressBar->setValue(bytesReceived); ui->labelSpeed->setText(QString("%1").arg(sspeed(speed))); ui->labelElapse->setText(selapse); }
-
Hi,
Since you are using a custom widget in place of the item, you first have get that widget and then grab the string you want from it.
-
Don't use c-style cast like that, when dealing with QObject derived classes you should use qobject_cast.
Also, even if understandable, that line is particularly unreadable you should break it in smaller chunk to show clearly what happens.
One last thing, accessing directly the ui variable from another designer based widget is a bad idea. What happens if you change that widget e.g. you change the QLabel for a QLineEdit? You'll have to go everywhere in your code and change the related calls. Your "Form" widget should rather have an accessor that returns the the content of that label so if you happen to change the widget, you'll only have to change the code in the accessor.