Search code examples
c++qtqlistwidgetqlistwidgetitem

Initialising a pointer to QListWidgetItem Qt 5.8


In my program I have a series of tabs and on each tab there is a combobox and a QListWidget. Based on the selection on the combobox the QListWidget will be populated.

Now what I am trying to achieve is that one the user presses a "APPLY" button after selecting a series of items on the "checkable" list widget for a given selection in combobox, I will read the bool status of each item on the list widget by using a pointer QListWidgetItem pointer to the list widget

This is part of my code;

void MainWindow::on_applyButton_clicked()
{
  //Reset list
  MainWindow::revenueList.clear();

  //Processing income statement
  //Select the first item on inc_st_combo
  ui->inc_st_comb->setCurrentText("Revenue Growth");

  //Create an iterator
  QStringListIterator iter(MainWindow::revenue_labels);

  //Loop through the list widget and read bool status
  while(iter.hasNext())
  {
      //Index for the QList
      int index = 0;
      //Create a QListWidgetItem pointer
      QListWidgetItem *listItem = new     QListWidgetItem(iter.next(),Ui_MainWindow::inc_st_list);

      bool status = listItem->checkState();
      qDebug() << "Status: " << status << endl;

      MainWindow::revenueList.append(status);
   }

  qDebug() << "List: " << MainWindow::revenueList << endl;
}

My problem is that when I try to initialise the QLsitWidgetItem on the following line;

   QListWidgetItem *listItem = new QListWidgetItem(iter.next(),Ui_MainWindow::inc_st_list);

Qt return the following error;

/Users/Vino/Documents/My Stuff/Qt Projects/Fundemental Analysis/FundementalAnalysis/mainwindow.cpp:389: error: invalid use of non-static data member 'inc_st_list' QListWidgetItem *listItem = new QListWidgetItem(iter.next(),Ui_MainWindow::inc_st_list); ~~~~~~~~~~~~~~~^~~~~~~~~~~

How do I initialise the QListWidgetItem pointer to point at a particular listWidget on the form?


Solution

  • If you want a pointer to an already existing object you won't use new, you need to assign it the address of the existing object:

    int pos = 0; //determine the right position
    QListWidgetItem *listItem = ui->inc_st_list->item(pos);