zoukankan      html  css  js  c++  java
  • 5.listview(QStringList QStringListModel)

    • UI
    • mainwindow.h
       1 #ifndef MAINWINDOW_H
       2 #define MAINWINDOW_H
       3 
       4 #include <QMainWindow>
       5 #include <QStringListModel>
       6 
       7 namespace Ui {
       8 class MainWindow;
       9 }
      10 
      11 class MainWindow : public QMainWindow
      12 {
      13     Q_OBJECT
      14 
      15 public:
      16     explicit MainWindow(QWidget *parent = 0);
      17     ~MainWindow();
      18 
      19 private slots:
      20     //按下按钮
      21     void on_pushButton_clicked();
      22   //combox改变
      23     void on_comboBox_currentIndexChanged(int index);
      24 
      25 private:
      26     Ui::MainWindow *ui;
      27     //设置列表
      28     QStringList *sl;
      29     //设置模式,显示列表
      30     QStringListModel *slm;
      31 };
      32 
      33 #endif // MAINWINDOW_H
    • mainwindow.cpp
       1 #include "mainwindow.h"
       2 #include "ui_mainwindow.h"
       3 #include <QStringListModel>
       4 
       5 MainWindow::MainWindow(QWidget *parent) :
       6     QMainWindow(parent),
       7     ui(new Ui::MainWindow)
       8 {
       9     ui->setupUi(this);
      10     sl = new QStringList();
      11     sl->append("hello1");
      12     sl->append("hello2");
      13     sl->append("hello3");
      14     sl->append("hello4");
      15     //创建字符串模式
      16     slm = new QStringListModel(this);
      17     slm->setStringList(*sl);
      18     //显示
      19     ui->listView->setModel(slm);
      20 
      21     //设置combox选项
      22     ui->comboBox->insertItem(0,"hello1","hello1");
      23     ui->comboBox->insertItem(1,"hello2","hello2");
      24     ui->comboBox->insertItem(2,"hello3","hello3");
      25     ui->comboBox->insertItem(3,"hello4","hello4");
      26 
      27 }
      28 
      29 MainWindow::~MainWindow()
      30 {
      31     delete ui;
      32 }
      33 
      34 void MainWindow::on_pushButton_clicked()
      35 {
      36 
      37 
      38     QString qstr;
      39     qstr = ui->lineEdit->text();
      40     sl->append(qstr);
      41     //模式设置字符串
      42     slm->setStringList(*sl);
      43     //显示
      44     ui->listView->setModel(slm);
      45 }
      46 
      47 void MainWindow::on_comboBox_currentIndexChanged(int index)
      48 {
      49     //获取当前数据
      50     QString myqstr=ui->comboBox->currentText();
      51     ui->lineEdit->setText(myqstr);
      52 }
    • main.cpp
       1 #include "mainwindow.h"
       2 #include <QApplication>
       3 
       4 int main(int argc, char *argv[])
       5 {
       6     QApplication a(argc, argv);
       7     MainWindow w;
       8     w.show();
       9 
      10     return a.exec();
      11 }
  • 相关阅读:
    magento 去掉index.php
    TCP三次握手与四次挥手
    <Jper和Iperf>的安装及使用
    【Python】安装方法小结
    【ubuntu】日常网络配置信息的查看及修改
    DNS解析
    【Dig工具】
    【ping/tracert】的简单使用
    【VMvare】yum在线源
    配置文件加载
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8735327.html
Copyright © 2011-2022 走看看