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. Problem in copying data from One QListWidget into another QListWidget
Forum Updated to NodeBB v4.3 + New Features

Problem in copying data from One QListWidget into another QListWidget

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.7k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    M_31
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      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.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • M Offline
        M Offline
        M_31
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

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

          http://www.catb.org/~esr/faqs/smart-questions.html

          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