《Qt Creator笔记》 QListview

文章目录

  • 代码
  • 看图

代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = nullptr);
    QLineEdit* _Edit;
    QPushButton* _Button;
    QListView* _View;

    QStringListModel* _StrMod;
    QStringList* _StrList;
signals:

public slots:
    void SlotAddData();
};


#include "MyWidget.h"
#include 
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
     QHBoxLayout* L_Layout(new QHBoxLayout);
    L_Layout->addWidget(this->_Edit=new QLineEdit);
    L_Layout->addWidget(this->_Button=new QPushButton("Add"));

    QVBoxLayout* V_Layout(new QVBoxLayout);
    V_Layout->addLayout(L_Layout);
    V_Layout->addWidget(this->_View=new QListView);

    this->setLayout(V_Layout);

    创建数据模型
        this->_StrMod=new QStringListModel;
        this->_StrList=new QStringList;

    connect(this->_Button,SIGNAL(pressed()),this,SLOT(SlotAddData()));
    connect(this->_Edit,SIGNAL(returnPressed()),this,SLOT(SlotAddData()));
}

void MyWidget::SlotAddData()
{
    添加数据
    this->_StrList->append(this->_Edit->text());
    this->_StrMod->setStringList(*this->_StrList);
    this->_View->setModel(this->_StrMod);
    this->_Edit->clear();
}

QComboBox也可以设置QStringListModel

看图

《Qt Creator笔记》 QListview_第1张图片

你可能感兴趣的:(Qt,Creator笔记,QtCreator,QListView)