Results 1 to 2 of 2

Thread: Question_QListWidget

  1. #1

    Default Question_QListWidget

    I have a question about using the QlistWidget Function where to use it to check if it is listed before and if not it should written into the listwdget.
    I have prepared some code but it doesnt work properly. Can someone look and correct if possible ?

    Thanks in advance,

    The part of the code (also included as txt file)
    -----------------------------------------------------------------------------
    //der Slot für das Laden der Seite
    void MainWindow:n_webView_loadFinished(bool ok)
    {
    int n;
    //für die HistoryListe
    QWebHistory *meineHistory = ui->webView->history();
    QListWidget *meineListe = new QListWidget(this);
    n=ui->listWidget->count();

    //war das Laden erfolgreich?
    if (ok)
    {

    {

    if ((ui->webView->url().toString()).isEmpty()==false)
    {
    for (int i=0; i<= n;i++)
    // Prüfen ob die geladene Seite schon vorhanden ist
    ui->listWidget->findItems(ui->webView->url().toString(),Qt::MatchFixedString);
    // wenn die Seite noch nicht vorhanden ist
    if (Qt::MatchFixedString ==8)
    // die URL in das Listenfeld schreiben
    ui->listWidget->addItem(ui->webView->url().toString());
    }

    else
    {
    //und auch in das Eingabefeld
    adressFeld->setText(ui->webView->url().toString());
    //den Titel setzen
    this->setWindowTitle("Minibrowser - " + ui->webView->title());
    if (meineHistory->canGoBack())
    ui->action_R_ckw_rts->setEnabled(true);
    else
    ui->action_R_ckw_rts->setEnabled(false);
    if (meineHistory->canGoForward())
    ui->action_Vorw_rts->setEnabled(true);
    else
    ui->action_Vorw_rts->setEnabled(false);
    }
    }
    }


    //die Fortschrittsanzeige ausblenden
    fortschrittBalken->setVisible(false);
    //den Text ändern
    fortschrittLabel->setText("Seite geladen");

    ---------------------------------------------------------
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Question_QListWidget

    Qt Code:
    1. // der Slot für das Laden der Seite
    2. void MainWindow : n_webView_loadFinished( bool ok )
    3. {
    4. int n;
    5. //für die HistoryListe
    6. QWebHistory *meineHistory = ui->webView->history();
    7. QListWidget *meineListe = new QListWidget( this );
    8. n = ui->listWidget->count();
    9.  
    10. //war das Laden erfolgreich?
    11. if ( ok )
    12. {
    13.  
    14. {
    15.  
    16. if ( ( ui->webView->url().toString() ).isEmpty() == false )
    17. {
    18. for ( int i = 0; i <= n; i++ )
    19. // Prüfen ob die geladene Seite schon vorhanden ist
    20. ui->listWidget->findItems( ui->webView->url().toString(), Qt::MatchFixedString );
    21. // wenn die Seite noch nicht vorhanden ist
    22. if ( Qt::MatchFixedString == 8 )
    23. // die URL in das Listenfeld schreiben
    24. ui->listWidget->addItem( ui->webView->url().toString() );
    25. }
    26.  
    27. else
    28. {
    29. //und auch in das Eingabefeld
    30. adressFeld->setText( ui->webView->url().toString() );
    31. //den Titel setzen
    32. this->setWindowTitle( "Minibrowser - " + ui->webView->title() );
    33. if ( meineHistory->canGoBack() )
    34. ui->action_R_ckw_rts->setEnabled( true );
    35. else
    36. ui->action_R_ckw_rts->setEnabled( false );
    37. if ( meineHistory->canGoForward() )
    38. ui->action_Vorw_rts->setEnabled( true );
    39. else
    40. ui->action_Vorw_rts->setEnabled( false );
    41. }
    42. }
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

    In line 7 you create a new QListWidget. It is created -empty-. Why do you do this when you don't actually use it anywhere? It is a memory leak.

    Line 20: Why do you call this method if you do not do anything with the return value? Yes, you are checking to see if you have visited the site already, but then you do nothing with the answer.

    Line 22 makes no sense. Qt:: MatchFixedString is a constant with the value 8, so the if clause is always true. And this line is not inside the for() loop because there is no { }. The only statement inside the for() loop is line 20. So the result is that you always add the URL to the list whether it is there or not.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.