Problem in copying data from One QListWidget into another QListWidget



  • HI All,
    I have used QTabWidget with 2 Tabs ( each tab contains one QListWidget on it).

    i am able to copy & paste the selected from one QListWidget item data into another QListWidget,
    I used a QList to store the copied item data in my QDialog.

    my codes are:
    @
    void CTestDlg::copyButtonPressed()
    {
    QList<MyObj*> listObj;
    QList<QListWidgetItem*> list = ui->listWdgt->selectedItems();

    foreach( QListWidgetItem* item, list )
    {
        MyObj  *obj = (MyObj*)item->data(Qt::UserRole).value<void*>();
        
        if(obj)
            listObj.append( obj);
    
    }
      emit sendCopiedObject(listObj); // here i am sending this list object to store in m_listObj of my QDialog member variable
    

    }

    void CTestDlg::pasteButtonPressed()
    {

    QList<MyObj*> listObj;
    
    emit getStoredObjcte(listObj);  // here i am getting back the copied list obj from my QDialog
    foreach(  MyObj  *obj , listObj )
    {
            addObjToListWidget( examQuestion  );
            
    }
    

    }

    void CTestDlg::addObjToListWidget( MyObj *obj )
    {
    QListWidgetItem *item = new QListWidgetItem( obj->object_name );

    item->setData( Qt::UserRole, qVariantFromValue((void *)obj));
    item->setFlags(Qt::ItemFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable ));

    int nCurRow = ui->listWdgt->currentRow();
    if( nCurRow >= 0 )
    {
    ui->listWdgt->insertItem( nCurRow, item );
    }
    else
    ui->listWdgt->addItem(item);
    }

    }
    @

    But the problem is ...if i have selected any items while pressing the paste button on my QListWidget.
    then the selected item is getting repeated instead of copied one... i dont know know why...

    please let me know..whether i am doing any thing wrong on this copy & paste procedure..



  • You should not emit a signal in order to get data back from somewhere. Just call an ordinary method.

    Also, if you store the data in a member variable of the very same object, you should call a method directly, too.

    From the code snippets you showed here, we cannot see what's going on and/or wrong. Show us the implementations of the slots connected to your signals, etc.



  • Thanks for the reply.
    let me give you my full implementation.
    @

    //in MainDlg.cpp

    void MainDlg::addTab(MyObj *myobj )
    {
    CTestDlg *testDlg = new CTestDlg( myobj->m_strName );
    int nNoOfTabs = ui->tabWidget->count();

    connect( testDlg, SIGNAL(sendCopiedObject(QList<MyObj*>&)), this,
                            SLOT(storeCopiedObject(QList<MyObj*>&)));
    
    connect( testDlg, SIGNAL(getStoredObject()), this,
                            SLOT(sendStoredObject()));
    
    
    ui->tabWidget->addTab( testDlg , myobj->m_strName );
    ui->tabWidget->setTabData( nNoOfTabs, qVariantFromValue( (void*)myobj ));
    ui->tabWidget->setCurrentIndex( nNoOfTabs );
    ui->tabWidget->setTabToolTip( nNoOfTabs,  myobj->m_strName );
    

    }

    void MainDlg::sendCopiedObject(QList<MyObj*>& listObj)
    {
    m_listCopiedObj.clear(); //this is the member variable of MainDlg.cpp
    m_listCopiedObj = listObj;

    }

    void MainDlg::sendStoredObject()
    {
    MyObj *myobj =qobject_cast<MyObj *> (ui->tabWidget->currentWidget());
    if( !myobj )
    return;

    myobj->storePasteObject(m_listCopiedObj );
    

    }

    //in TestDlg.cpp

    void CTestDlg::storePasteObject(QList<MyObj*>& listObj)
    {
    m_listCopiedObj.clear(); //this is the member variable of CTestDlg.cpp
    m_listCopiedObj = listObj;

    foreach( MyObj *myobj, m_listCopiedObj )
    {
        addObjToListWidget( myobj );
        
    }
    

    }

    void CTestDlg::copyButtonPressed()
    {
    QList<MyObj*> listObj;
    QList<QListWidgetItem*> list = ui->listWdgt->selectedItems();
    foreach( QListWidgetItem* item, list )
    {
    MyObj obj = (MyObj)item->data(Qt::UserRole).value<void*>();
    if(obj)
    listObj.append( obj);
    }
    emit sendCopiedObject(listObj); // here i am sending this list object to store in m_listObj of my QDialog member variable
    }

    void CTestDlg::pasteButtonPressed()
    {
    QList<MyObj*> listObj;
    emit getStoredObjcte(listObj); // here i am getting back the copied list obj from my QDialog
    foreach( MyObj *obj , listObj )
    {
    addObjToListWidget( examQuestion );
    }
    }

    void CTestDlg::addObjToListWidget( MyObj *obj )
    {
    QListWidgetItem *item = new QListWidgetItem( obj->object_name );

    item->setData( Qt::UserRole, qVariantFromValue((void *)obj));
    item->setFlags(Qt::ItemFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable ));

    int nCurRow = ui->listWdgt->currentRow();
    if( nCurRow >= 0 )
    {
    ui->listWdgt->insertItem( nCurRow, item );
    }
    else
    ui->listWdgt->addItem(item); }

    }

    @

    Please let me know whether i am doing anything wrong on my implementation?



  • Your class MainDlg does not have slots storeCopiedObject adn sendStoredObject. I suspect the connects to fail.


Log in to reply
 

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