Frage

I have a QLineEdit, 2 QPush buttons (Add & Remove Buttons) and a QListView. The Text which i am entering in the QLineEdit will gets added in the QListView when the add button is clicked. And if I'm selecting any one of the Item from the QListView and click the Remove Button the selected item will be removed. I finished these things and it works fine. Now i want to add a another QListView and if am Double clicking the QListView Items (QListView 1) the items should be transfered (items in the QListView 1 should be removed completely) to the new QListView (QListView 2) and vice versa. plz help me with your suggestions. Thanks in Advance.

War es hilfreich?

Lösung

Example.h

class Example : public QWidget
{
    Q_OBJECT

public:
    explicit Example(QWidget *parent = 0);
    ~Example();

private slots:
     void on_listView_doubleClicked(const QModelIndex &index);
     void on_listView_2_doubleClicked(const QModelIndex &index);

private:
    QStandardItemModel *model;  // This model is used when the add button is clicked.
    QStandardItemModel *listViewModel;
};  

Example.cpp

void Example::on_listView_doubleClicked(const QModelIndex &index)
{  
    QStandardItem *Item1;
    Items1 = new QStandardItem();

    Items1->setData(ui->listView->currentIndex().data(), Qt::DisplayRole );
    Items1->setEditable( false );

    listViewModel->appendRow( Items1 );
    listViewModel->sort( 0, Qt::AscendingOrder );
    ui->listView_2->setModel( listViewModel );

    model->removeRow( ui->listView->currentIndex().row() );
}

void Example::on_listView_2_doubleClicked(const QModelIndex &index)
{
    QStandardItem *Items2;
    Items2 = new QStandardItem();
    Items2->setData( ui->listView_2->currentIndex().data(), Qt::DisplayRole );
    Items2->setEditable( false);

    model->appendRow( Items2 );
    model->sort( 0,Qt::AscendingOrder );

    ui->listView->setModel( model );
    model->removeRow( ui->listView_2->currentIndex().row() );

}

Andere Tipps

A more extensible way would be to create a custom model (possibly inheriting the QStringListModel if that suits your needs) and then implementing moveRows and/or the Drag&Drop facilities.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top