2017-02-23 9 views
-1

私のプログラムでは、一連のタブがあり、各タブにはコンボボックスとQListWidgetがあります。私はQListWidgetItemのポインタを使ってQListWidgetの項目のステータスを読み込もうとしています。プログラムのこの時点でクラッシュします。私はブレークポイントでダブルチェックしているので、プログラムがここでクラッシュすることを確かめてください。QListWidgetItemポインタによってプログラムがクラッシュする

これは私のコードです。

void MainWindow::on_applyButton_clicked() 
{ 
//Reset list 
MainWindow::revenueList.clear(); 
QStringList itemList; 
itemList <<"Revenue growth" << "Cost of revenue growth" << "Operating income growth" 
     << "Net income growth" << "Total operating expense growth" << "Gross profit" 
     << "Operating profit" << "Net profit"; 

//Processing income statement 
//Loop through all itemsin ComboBox 
int items = ui->inc_st_comb->count(); 

for(int currentItem = 0; currentItem < items; currentItem++) 
{ 
    //Set to current index 
    ui->inc_st_comb->setCurrentText(itemList.at(currentItem)); 

    //Point to QListWidget Item and read checkbox 
    QListWidgetItem *listItem = ui->inc_st_list->item(currentItem); 

    if(listItem->checkState() == Qt::Checked) 
    { 
     MainWindow::revenueList.append(true); 
    } 
    else if (listItem->checkState() == Qt::Unchecked) 
    { 
     MainWindow::revenueList.append(false); 
    } 
} 

    qDebug() << "U: " << MainWindow::revenueList; 
} 

このブロックでプログラムがクラッシュします。

if(listItem->checkState() == Qt::Checked) 
{ 
     MainWindow::revenueList.append(true); 
} 
else if (listItem->checkState() == Qt::Unchecked) 
{ 
     MainWindow::revenueList.append(false); 
} 

これはおそらくポインタlistItem無効な場所にポイントまたはNULLです。この問題をどうやって解決するのですか?私は何か間違ったコーディングですか?

+0

"おそらく、ポインタlistItemが無効な場所またはNULLを指しているためです。"多分? SOに投稿する前にデバッガでこれを確認してみませんか?最初に_exact_問題を解く。 – MrEricSir

+0

デバッガでこれを行う方法を教えていただけますか?私はQtプログラミングの初心者です。自分自身を学ぶ。アドバイスは参考になりますか? – Vino

+0

コードが不完全です。特に、main()関数と少なくとも1つの '#include'が欠落しているようです。コードを問題の[mcve]にするようにコードを編集してください。そうすれば、それを再現して解決することができます。 [ask]も読んでください。 –

答えて

0

私のエラーは修正されました。 私が間違っていた部分は、QComboBox::count()機能によって返された値を使ってQListWidgetの項目にアクセスしようとしていたことでした。 コンボボックス内の項目数は8でした。このQListWidget上の数字項目は、QComboBoxの選択は3でした。私は​​3210を使用してループ数を制限することによってQListWidgetの項目をループするためにfor forループを追加することで解決しました。

ここに私の作業コードです。

void MainWindow::on_applyButton_clicked() 
{ 
//Reset list 
MainWindow::revenueList.clear(); 
QStringList itemList; 
itemList <<"Revenue growth" << "Cost of revenue growth" << "Operating income growth" 
     << "Net income growth" << "Total operating expense growth" << "Gross profit" 
     << "Operating profit" << "Net profit"; 

//Processing income statement 
//Loop through all itemsin ComboBox 
int items = ui->inc_st_comb->count(); 

for(int currentItem = 0; currentItem < items; currentItem++) 
{ 
    //Set to current index 
    ui->inc_st_comb->setCurrentText(itemList.at(currentItem)); 

    for(int index = 0; index < ui->inc_st_list->count(); index++) 
    { 
     //Point to QListWidget Item and read checkbox 
     QListWidgetItem *listItem = ui->inc_st_list->item(index); 


     if(listItem->checkState() == Qt::Checked) 
     { 
      MainWindow::revenueList.append(true); 
     } 
     else if (listItem->checkState() == Qt::Unchecked) 
     { 
      MainWindow::revenueList.append(false); 
     } 
    } 
} 

    qDebug() << "U: " << MainWindow::revenueList; 
} 
関連する問題