[solved] qtablewidget selectedItems gives empty list



  • 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..


  • Moderators

    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.



  • 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();


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.