Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get text value of custom QListWidgetItem ?
Forum Updated to NodeBB v4.3 + New Features

How to get text value of custom QListWidgetItem ?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    alt text

    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);
    }
    

    https://github.com/sonichy

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        I found the solution:
        QString path=((Form*)(ui->listWidget->itemWidget(ui->listWidget->item(ui->listWidget->currentRow()))))->ui->labelPath->text();

        https://github.com/sonichy

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved