Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    [solved] qtablewidget selectedItems gives empty list

    General and Desktop
    2
    3
    2378
    Loading More Posts
    • 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.
    • A
      Abin last edited by

      Hi,

      I have a qtablewidget with one column with a widget and others with data. The only column with widget is shown and all other columns are hidden.

      @foreach (BillHeader *billHeader, billHeaderList)
      {
      m_pBillTable->insertRow(i);

          itemWidget = new LookupItem;
          itemWidget->setImage(1);
          ...
          m_pBillTable->setCellWidget(i, 0, itemWidget);
      
          tableItem = new QTableWidgetItem(billHeader->billNumber);
          tableItem->setTextAlignment(Qt::AlignCenter);
          m_pBillTable->setItem(i, 1, tableItem);
          ...
      
          m_pBillTable->hideColumn(1);
          ...@
      

      when ok button click i try to get the selected item and get data from the widget set to it

      @void OrderLookup::handleOkClick()
      {
      qDebug()<<Q_FUNC_INFO<<"Invoked";

      QList<QTableWidgetItem*> itemList = m_pBillTable->selectedItems();
      qDebug()<<Q_FUNC_INFO<<itemList.count();
      if (!itemList.isEmpty())
      {
          int row = itemList.at(0)->row();
          qDebug()<<Q_FUNC_INFO<<row;
          LookupItem *item = (LookupItem*)m_pBillTable->cellWidget(row, 0);
      
          if (NULL != item)
          {
              QString billNumber = item->getBillNumber();
              emit orderLookupComplete(billNumber);
              accept();
          }
      }
      
      qDebug()<<Q_FUNC_INFO<<"Exits";
      

      }
      @

      But i am getting the list count as zero.

      The row is getting selected and gets highlighted.
      I've set some properties to table widget as below:

      @ m_pBillTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
      m_pBillTable->setSelectionBehavior(QAbstractItemView::SelectRows);
      m_pBillTable->setSelectionMode(QAbstractItemView::SingleSelection);
      m_pBillTable->setFocusPolicy(Qt::NoFocus);
      @

      Please can someone help me to know why list count is empty..

      1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators last edited by

        Hi,

        Use QAbstractItemView::MultiSelection or QAbstractItemView::ExtendedSelection instead of QAbstractItemView::SingleSelection

        Check "selectionModel":http://qt-project.org/doc/qt-4.8/qabstractitemview.html#selectionModel and "selectedIndexes()":http://qt-project.org/doc/qt-4.8/qitemselectionmodel.html#selectedIndexes and then iterate over the selected Indexes.

        157

        1 Reply Last reply Reply Quote 0
        • A
          Abin last edited by

          Thanks..

          Solved the issue ..

          QItemSelectionModel *itemModel = m_pBillTable->selectionModel();
          QModelIndexList indexList = itemModel->selectedRows();
          qDebug()<<Q_FUNC_INFO<<"IndexList Count"<<indexList.count();

          if (!indexList.isEmpty())
          {
          int row = indexList.at(0).row();

          1 Reply Last reply Reply Quote 0
          • First post
            Last post