首页 文章

Qt - 如何将QListView项目转移到另一个QListView?

提问于
浏览
1

我有 QLineEdit2 QPush buttons (添加和删除按钮)和 QListView . QListViewQListView QLineEdit will gets added 中输入的文字 . 如果我选择 Item from the QListView 中的任何一个并单击 Remove Button ,则将删除所选项目 . 我完成了这些事情并且工作正常 . Now i want to add a another QListView and if am Double clicking the QListView Items (QListView 1) the items should be transfered (QListView 1中的项目应该完全删除) to the new QListView (QListView 2) and vice versa. plz帮我提出建议 . 提前致谢 .

2 回答

  • 0

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

    更可扩展的方法是创建自定义模型(如果符合您的需要,可能继承 QStringListModel ),然后实现 moveRows 和/或拖放工具 .

相关问题