PDA

View Full Version : Question_QListWidget



cprogcoder
17th September 2017, 16:27
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");

---------------------------------------------------------

d_stranz
17th September 2017, 18:47
// 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 );
}
}
}
}

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.