Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [solved] qtablewidget selectedItems gives empty list
QtWS25 Last Chance

[solved] qtablewidget selectedItems gives empty list

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.7k Views
  • 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 Offline
    A Offline
    Abin
    wrote on last edited by
    #1

    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
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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
      0
      • A Offline
        A Offline
        Abin
        wrote on last edited by
        #3

        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
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved