【Qt5开发及实例】34、Qt操作主/从视图和XML

前端之家收集整理的这篇文章主要介绍了【Qt5开发及实例】34、Qt操作主/从视图和XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

mainwindow.h

/**
* 书本:【Qt5开发及实例】
* 功能:Qt操作主/从视图和XML
* 文件:mainwindow.h
* 时间:2015年2月20日21:20:25
* 作者:cutter_point
*/
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QGroupBox>
#include <QTableView>
#include <QListWidget>
#include <QLabel>
#include <QDomDocument>
#include <QDomNode>
#include <QFile>
#include <QsqlRelationalTableModel>
#include <QsqlTableModel>
#include <QDebug>

class MainWindow : public QMainWindow
{
  Q_OBJECT

public:
  MainWindow(QWidget *parent = 0);
  //汽车制造商列表名,汽车表名,汽车详细信息对应XML文件指针
  MainWindow(const QString &factoryTable,const QString &carTable,QFile *carDetails,QWidget *parent = 0);   //构造函数
  ~MainWindow();

private:
  QGroupBox *createCarGroupBox();
  QGroupBox *createFactoryGroupBox();
  QGroupBox *createDetailsGroupBox();
  void createMenuBar();   //菜单

  QTableView *carView;
  QTableView *factoryView;

  QListWidget *attribList;

  QLabel *profileLabel;
  QLabel *titleLabel;

  QDomDocument carData;     //对应XML文件的源
  QFile *file;      //XML文件指针
  QsqlRelationalTableModel *carModel;     //对应的汽车模型表
  QsqlTableModel *factoryModel;     //工厂模型表

  void readCarData();   //读入XML文件爱你根目录

};

#endif // MAINWINDOW_H


mainwindow.cpp

/**
* 书本:【Qt5开发及实例】
* 功能:Qt操作主/从视图和XML
* 文件:mainwindow.cpp
* 时间:2015年2月20日21:20:25
* 作者:cutter_point
*/
#include "mainwindow.h"

#include <QGridLayout>
#include <QAbstractItemView>
#include <QHeaderView>
#include <QAction>
#include <QMenu>
#include <QMenuBar>

MainWindow::MainWindow(const QString &factoryTable,QWidget *parent)
    : QMainWindow(parent)
{
    file = carDetails;      //得到文件指针
    readCarData();      //得到XML根元素开始

    carModel = new QsqlRelationalTableModel(this);      //为汽车表创建一个模型
    carModel->setTable(carTable);     //为模型设定表
    carModel->setRelation(2,QsqlRelation(factoryTable,"id","manufactory"));    //按0, 1, 2,3顺序来的在carTable中第3列是外键连接在factoryTable表中的id,
    //用来展示的是manufactory来在car表中代替id号
    carModel->select();   //这个是为了直接显示效果出来,不用等你点他进行响应

    factoryModel = new QsqlTableModel(this);      //同上
    factoryModel->setTable(factoryTable);
    factoryModel->select();      //这个是为了直接显示效果出来,不用等你点他进行响应

    QGroupBox *factory = createFactoryGroupBox();
    QGroupBox *cars = createCarGroupBox();
    QGroupBox *details = createDetailsGroupBox();   //创建三块显示区域

//    uniqueCarId = carModel->rowCount();
//    uniqueFactoryId = factoryModel->rowCount();

    //布局
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(factory,0);
    layout->addWidget(cars,1,0);
    layout->addWidget(details,2,1);
    layout->setColumnStretch(1,1);
    layout->setColumnMinimumWidth(0,500);    //第一列的列宽

    QWidget *widget = new QWidget;
    widget->setLayout(layout);
    setCentralWidget(widget);
    createMenuBar();

    resize(850,400);
    setWindowTitle(tr("主从视图"));
}

//MainWindow::MainWindow(QWidget *parent)
//  : QMainWindow(parent)
//{
//  QGroupBox *factory = createFactoryGroupBox();
//  QGroupBox *cars = createCarGroupBox();
//  QGroupBox *details = createDetailsGroupBox();   //创建三块显示区域

//  //布局
//  QGridLayout *layout = new QGridLayout;
//  layout->addWidget(factory,0);
//  layout->addWidget(cars,0);
//  layout->addWidget(details,1);
//  layout->setColumnStretch(1,1);
//  layout->setColumnMinimumWidth(0,500);    //第一列的列宽

//  QWidget *widget = new QWidget;
//  widget->setLayout(layout);
//  this->setCentralWidget(widget);
//  createMenuBar();

//  resize(850,400);
//  setWindowTitle(tr("主从视图"));
//}

//QGroupBox *createCarGroupBox();
QGroupBox* MainWindow::createCarGroupBox()
{
  //汽车的区域
  QGroupBox *Box = new QGroupBox(tr("汽车"));

  carView = new QTableView;
  carView->setEditTriggers(QAbstractItemView::NoEditTriggers);    //不准用户编辑视图中的字段
  carView->setSortingEnabled(true);
  carView->setSelectionBehavior(QAbstractItemView::SelectRows);
  carView->setSelectionMode(QAbstractItemView::SingleSelection);
  carView->setShowGrid(false);        //不显示网格
  carView->verticalHeader()->hide();  //隐藏水平头部的计数1,3,4,。。。
  carView->setAlternatingRowColors(true);

  carView->setModel(carModel);

  QVBoxLayout *layout = new QVBoxLayout;
  layout->addWidget(carView,0);
  Box->setLayout(layout);

  return Box;

}

//QGroupBox *createFactoryGroupBox();
QGroupBox* MainWindow::createFactoryGroupBox()
{
  //汽车制造商
  factoryView = new QTableView;
  factoryView->setEditTriggers(QAbstractItemView::NoEditTriggers);  //不准编辑
  factoryView->setSortingEnabled(true);
  factoryView->setSelectionBehavior(QAbstractItemView::SelectRows);
  factoryView->setSelectionMode(QAbstractItemView::SingleSelection);
  factoryView->setShowGrid(false);        //不显示网格
  factoryView->verticalHeader()->hide();    //隐藏水平头部的计数1,4,。。。
  factoryView->setAlternatingRowColors(true);

  factoryView->setModel(factoryModel);      //设置模型显示出来


  QGroupBox *Box = new QGroupBox(tr("汽车制造商"));
  QGridLayout *layout = new QGridLayout;
  layout->addWidget(factoryView,0);
  Box->setLayout(layout);

  return Box;
}

//QGroupBox *createDetailsGroupBox();
QGroupBox* MainWindow::createDetailsGroupBox()
{
  QGroupBox *Box = new QGroupBox(tr("详细信息"));

  profileLabel = new QLabel;
  profileLabel->setWordWrap(true);
  profileLabel->setAlignment(Qt::AlignBottom);
  titleLabel = new QLabel;
  titleLabel->setWordWrap(true);
  titleLabel->setAlignment(Qt::AlignBottom);

  attribList = new QListWidget;

  QGridLayout *layout = new QGridLayout;
  layout->addWidget(profileLabel,2);
  layout->addWidget(titleLabel,2);
  layout->addWidget(attribList,2);
  layout->setRowStretch(2,1);
  Box->setLayout(layout);

  return Box;
}


//oid createMenuBar();   //菜单
void MainWindow::createMenuBar()
{
  QAction *addAction = new QAction(tr("添加"),this);
  QAction *deleteAction = new QAction(tr("删除"),this);
  QAction *quitAction = new QAction(tr("quit"),this);

  addAction->setShortcut(tr("Ctrl+A"));
  deleteAction->setShortcut(tr("Ctrl+D"));
  quitAction->setShortcut(tr("Ctrl+Q"));

  QMenu *fileMenu = menuBar()->addMenu(tr("操作菜单"));
  fileMenu->addAction(addAction);
  fileMenu->addAction(deleteAction);
  fileMenu->addSeparator();
  fileMenu->addAction(quitAction);
}

MainWindow::~MainWindow()
{

}

//void readCarData();   //读入XML文件爱你根目录
void MainWindow::readCarData()
{
  if(!file->open(QIODevice::ReadOnly))
    return;
//  else
//    {
//      qDebug()<<"..ok";
//    }
  if(!carData.setContent(file))   //设置XML源
    {
      qDebug()<<"..ok";
      file->close();
      return;
    }
  file->close();
}






conndlg.h

#ifndef CONNDLG_H
#define CONNDLG_H

#include <QDialog>
#include <QMessageBox>

namespace Ui
{
  class ConnDlg;
}

class QsqlError;

class ConnDlg : public QDialog
{
  Q_OBJECT

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

  QString driverName() const;     //驱动名
  QString databaseName() const;   //数据库
  QString userName() const;   //用户
  QString password() const;   //密码
  QString hostName() const;   //主机
  int port() const;       //端口
  QsqlError addConnection(const QString &driver,const QString &dbName,const QString &host,const QString &user,const QString &passwd,int port = -1);    //打开数据库连接,并判断是否打开
  //创建一个连接
  void createDB();
  void addsqliteConnection();

private slots:
  void on_okButton_clicked();

  void on_cancelButton_clicked() { reject(); }
  void driverChanged(const QString &);

private:
  Ui::ConnDlg *ui;
};

#endif // CONNDLG_H

conndlg.cpp

#include "conndlg.h"
#include "ui_conndlg.h"

#include <QsqlDatabase>
#include <Qtsql>

ConnDlg::ConnDlg(QWidget *parent) :
  QDialog(parent),ui(new Ui::ConnDlg)
{
  ui->setupUi(this);
  QStringList drivers = QsqlDatabase::drivers();
  ui->comboDriver->addItems(drivers);   //显示可以用的所有驱动
  connect(ui->comboDriver,SIGNAL(currentIndexChanged(QString)),this,SLOT(driverChanged(QString)));
  ui->strtus_label->setText(tr("准备连接数据库!"));

}

//void driverChanged(const QString &);
void ConnDlg::driverChanged(const QString &text)
{
  if(text =="QsqlITE")
      {
          ui->editDatabase->setEnabled(false);
          ui->editUsername->setEnabled(false);
          ui->editPassword->setEnabled(false);
          ui->editHostname->setEnabled(false);
          ui->portSpinBox->setEnabled(false);
      }
      else
      {
          ui->editDatabase->setEnabled(true);
          ui->editUsername->setEnabled(true);
          ui->editPassword->setEnabled(true);
          ui->editHostname->setEnabled(true);
          ui->portSpinBox->setEnabled(true);
      }
}

QString ConnDlg::driverName() const
{
  return ui->comboDriver->currentText();    //得到当前选择的驱动
}

QString ConnDlg::databaseName() const
{
    return ui->editDatabase->text();    //得到数据库名
}

QString ConnDlg::userName() const
{
    return ui->editUsername->text();      //用户名
}

QString ConnDlg::password() const
{
    return ui->editPassword->text();      //密码
}

QString ConnDlg::hostName() const
{
    return ui->editHostname->text();        //主机
}

int ConnDlg::port() const
{
    return ui->portSpinBox->value();    //端口号
}

ConnDlg::~ConnDlg()
{
  delete ui;
}

void ConnDlg::on_okButton_clicked()
{
  if (ui->comboDriver->currentText().isEmpty())
      {
          ui->strtus_label->setText(tr("请选择一个数据库驱动!"));
          ui->comboDriver->setFocus();      //显示选中
      }
      else if(ui->comboDriver->currentText() =="QsqlITE")
      {
          addsqliteConnection();        //添加连接
          //创建数据库表,如已存在则无须执行
          createDB();
          accept();   //接受事件,免得返回父类
      }
      else
      {
          QsqlError err = addConnection(driverName(),databaseName(),hostName(),userName(),password(),port());
          if (err.type() != QsqlError::NoError)
              ui->strtus_label->setText(err.text());
          else
              ui->strtus_label->setText(tr("连接数据库成功!"));
          //创建数据库表,如已存在则无须执行
          accept();
          }
}

//QsqlError addConnection(const QString &driver,//                                            const QString &passwd,int port = -1);
QsqlError ConnDlg::addConnection(const QString &driver,int port)
{
    QsqlError err;
    QsqlDatabase db = QsqlDatabase::addDatabase(driver);
    db.setDatabaseName(dbName);
    db.setHostName(host);
    db.setPort(port);
    if (!db.open(user,passwd))
    {
        err = db.lastError();
    }
    return err;
}

void ConnDlg::addsqliteConnection()
{
    QsqlDatabase db = QsqlDatabase::addDatabase("QsqlITE");
    db.setDatabaseName("databasefile");
    if (!db.open())
    {
        ui->strtus_label->setText(db.lastError().text());
        return;
    }
    ui->strtus_label->setText(tr("创建sqlite数据库成功!"));
}

void ConnDlg::createDB()
{
  QsqlQuery query;
  query.exec("create table factory (id int primary key,manufactory varchar(40),address varchar(40))");
  query.exec(QObject::tr("insert into factory values(1,'一汽大众','长春')"));
  query.exec(QObject::tr("insert into factory values(2,'二汽神龙','武汉')"));
  query.exec(QObject::tr("insert into factory values(3,'上海大众','上海')"));
//  query.exec(QObject::tr("insert into factory values(4,'长春')"));

  query.exec("create table cars (carid int primary key,name varchar(50),factoryid int,year int,foreign key(factoryid) references factory)");
  query.exec(QObject::tr("insert into cars values(1,'奥迪A6',2005)"));
  query.exec(QObject::tr("insert into cars values(2,'捷达',1993)"));
  query.exec(QObject::tr("insert into cars values(3,'宝来',2000)"));
  query.exec(QObject::tr("insert into cars values(4,'毕加索',1999)"));
  query.exec(QObject::tr("insert into cars values(5,'富康',2004)"));
  query.exec(QObject::tr("insert into cars values(6,'标致307',2001)"));
  query.exec(QObject::tr("insert into cars values(7,'桑塔纳',1995)"));
  query.exec(QObject::tr("insert into cars values(8,'帕萨特',2000)"));
}



main.cpp

#include "mainwindow.h"
#include "conndlg.h"

#include <QApplication>
#include <QDebug>

int main(int argc,char *argv[])
{
  QApplication a(argc,argv);

  ConnDlg dialog;
  if (dialog.exec() != QDialog::Accepted)
      return -1;
//  dialog.show();
  QFile *carDetails = new QFile("attribs.xml");
//  if(carDetails->isOpen())
//    {
//      qDebug()<<"??ok";
//    }
  MainWindow window("factory","cars",carDetails);
  window.show();

//  MainWindow w;
//  w.show();

  return a.exec();
}

截图:

原文链接:https://www.f2er.com/xml/297086.html

猜你在找的XML相关文章