美文网首页
Qt ListView

Qt ListView

作者: 静候那一米阳光 | 来源:发表于2017-12-22 00:15 被阅读0次

    ListView 增加 修改 删除 实例

    【dialog.h】

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include <QStringListModel>
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
    
    private slots:
        void on_pushButton_clicked();
    
        void on_pushButton_2_clicked();
    
        void on_pushButton_3_clicked();
    
    private:
        Ui::Dialog *ui;
        QStringListModel *model;
    };
    
    #endif // DIALOG_H
    

    【dialog.cpp】

    #include "dialog.h"
    #include "ui_dialog.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        model=new QStringListModel(this);
        QStringList list;
        list <<"cats"<<"dogs"<<"birds";
        model->setStringList(list);
        ui->listView->setModel(model);
        ui->comboBox->setModel(model);
        ui->listView->setEditTriggers(QAbstractItemView::AnyKeyPressed|QAbstractItemView::DoubleClicked);
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::on_pushButton_clicked()
    {
        //add
        int row = model->rowCount();
        model->insertRows(row,1);
        QModelIndex index = model->index(row);
        ui->listView->setCurrentIndex(index);
        ui->listView->edit(index);
    }
    
    void Dialog::on_pushButton_2_clicked()
    {
        //insert
        int row = ui->listView->currentIndex().row();
        model->insertRows(row,1);
        QModelIndex index = model->index(row);
        ui->listView->setCurrentIndex(index);
        ui->listView->edit(index);
    }
    
    void Dialog::on_pushButton_3_clicked()
    {
        //delete
        model->removeRows(ui->listView->currentIndex().row(),1);
    }
    

    【界面】

    界面

    相关文章

      网友评论

          本文标题:Qt ListView

          本文链接:https://www.haomeiwen.com/subject/kcnzwxtx.html