目录
概述
演示
源码
这里用Spring Boot控制"/"和"index.html"请求,获取客户端http的信息;
Qt端使用QML和C++混合编程方式,C++控制业务,QML做页面展示;
首先来个动态图:
然后,然后就没有然后了,
差不多就是这种现象,可以识别谷歌,和火狐浏览器,
Windows和Linux操作系统,主要是根据HTTP包的user-agent这个属性。
Java端结构如下:
源码如下:
HelloController.java
package getsomeinfo.demo.controller;
import getsomeinfo.demo.judgment.Judge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
@Controller
public class HelloController {
@Autowired
JdbcTemplate jdbcTemplate;
@Autowired
HttpServletRequest httpServletRequest;
@ResponseBody
@GetMapping({"/", "/index.html"})
public String index(){
//List
Jude.java
package getsomeinfo.demo.judgment;
public class Judge {
//判断是啥操作系统
public static String whatOS(final String str){
String osStr = "未知";
//这里就简单判断下
if(str.contains("Windows")){
osStr = "Windows";
}
else if(str.contains("Linux")){
osStr = "Linux";
}
else{
}
return osStr;
}
//判断是啥浏览器-简单判断下
public static String whatBrower(final String str){
String browerStr = "未知";
if(str.contains("Chrome")){
browerStr = "Chrome";
}
else if(str.contains("Firefox")){
browerStr = "Firefox";
}
else{
}
return browerStr;
}
}
application.yml
spring:
datasource:
username: root
password : 123456
url: jdbc:mysql://192.168.164.141:3306/jdbc
driver-class-name: com.mysql.jdbc.Driver
server:
port: 8888
porn.xml
4.0.0
org.springframework.boot
spring-boot-starter-parent
1.5.19.RELEASE
GetSomeInfo
demo
0.0.1-SNAPSHOT
demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
C++的Qt端
结构如下:
源码如下:
mytableviewmodel.h
#ifndef MYTABLEVIEWMODEL_H
#define MYTABLEVIEWMODEL_H
#include
class MyTableViewModel: public QAbstractTableModel
{
Q_OBJECT
public:
MyTableViewModel();
int rowCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex &parent) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role) const Q_DECL_OVERRIDE;
QHash roleNames() const Q_DECL_OVERRIDE;
Q_INVOKABLE void addItem(QString one, QString two, QString three, QString four, QString five);
Q_INVOKABLE void Del();
Q_INVOKABLE void Refresh();
private:
QVector> m_aryData;
};
#endif // MYTABLEVIEWMODEL_H
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
QT_BEGIN_NAMESPACE
class QQuickView;
class MyTableViewModel;
class QSqlQuery;
class QTimer;
QT_END_NAMESPACE
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
protected:
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
bool connectMySQL();
QVector selectAppropriateData(const QString &cmd);
protected slots:
void flushText();
private:
Ui::Widget *ui;
QQuickView *m_quickView;
QWidget *m_quickWidget;
MyTableViewModel *m_model;
QSqlDatabase m_db;
QSqlQuery *m_query;
QTimer *m_timer;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include "mytableviewmodel.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
mytableviewmodel.cpp
#include "mytableviewmodel.h"
MyTableViewModel::MyTableViewModel(): QAbstractTableModel(NULL)
{
}
int MyTableViewModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return m_aryData.size();
}
int MyTableViewModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return 3;
}
QVariant MyTableViewModel::data(const QModelIndex &index, int role) const
{
return m_aryData[index.row()][role];
}
QHash MyTableViewModel::roleNames() const
{
QHash roles;
roles[0] = "1";
roles[1] = "2";
roles[2] = "3";
roles[3] = "4";
roles[4] = "5";
return roles;
}
void MyTableViewModel::addItem(QString one, QString two, QString three, QString four, QString five)
{
beginInsertRows(QModelIndex(), m_aryData.size(),m_aryData.size());
QVector list;
list << one << two << three << four << five;
m_aryData << list;
endInsertRows();
}
void MyTableViewModel::Del()
{
if(m_aryData.size() <= 0) return;
for(int i = 0; i < m_aryData.size(); i++){
beginRemoveRows(QModelIndex(), m_aryData.size() - 1, m_aryData.size() - 1);
}
m_aryData.clear();
endRemoveRows();
}
void MyTableViewModel::Refresh()
{
beginResetModel();
endResetModel();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "mytableviewmodel.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
connectMySQL();
m_quickView = new QQuickView;
m_timer = new QTimer(this);
m_model = new MyTableViewModel;
m_quickView->engine()->rootContext()->setContextProperty("theModel", m_model);
m_quickView->setResizeMode(QQuickView::SizeRootObjectToView);
m_quickView->setSource(QUrl("qrc:/main.qml"));
m_quickWidget = QWidget::createWindowContainer(m_quickView);
m_quickWidget->setParent(ui->widget);
connect(m_timer, SIGNAL(timeout()), this, SLOT(flushText()));
m_timer->start(500);
this->setWindowTitle("CSDN IT1995");
}
Widget::~Widget()
{
delete ui;
delete m_quickView;
delete m_model;
}
void Widget::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event);
m_quickWidget->setFixedSize(ui->widget->size());
QWidget::resizeEvent(event);
}
bool Widget::connectMySQL()
{
m_db = QSqlDatabase::addDatabase("QMYSQL");
m_query = new QSqlQuery(m_db);
m_db.setHostName("192.168.164.141");
m_db.setPort(3306);
m_db.setDatabaseName("mysql");
m_db.setUserName("root");
m_db.setPassword("123456");
if(!m_db.open()){
qDebug() << "error";
return false;
}
return true;
}
QVector Widget::selectAppropriateData(const QString &cmd)
{
QVectorlistVec;
if(!m_query->exec(cmd) || m_query->size() == 0){
return listVec;
}
while(m_query->next()){
QStringList list;
for(int i = 0; i < 5; i++){ //这里偷懒了,把5写死了!
if(i == 3){
list << m_query->value(i).toDateTime().toString("yyyy-MM-dd HH:mm:ss");
continue;
}
list << m_query->value(i).toString();
}
listVec.append(list);
}
return listVec;
}
void Widget::flushText()
{
m_model->Del();
QVector listVec = selectAppropriateData("select * from jdbc.someInfo");
for(int i = 0; i < listVec.size(); i++){
m_model->addItem(listVec[i][0].toUtf8(), listVec[i][1].toUtf8()
, listVec[i][2].toUtf8(), listVec[i][3].toUtf8(), listVec[i][4].toUtf8());
}
m_model->Refresh();
}
widget.ui
main.qml
import QtQuick 2.8
import QtQuick.Controls 2.1
Item {
visible: true
MyTableView{
id: myTableView
height: parent.height
width: parent.width
focus: false
tableView.itemDelegate:Rectangle {
TextField{
id: textField
height: 25
text: styleData.value
}
}
tableView.rowDelegate: Rectangle {
height: 25
}
}
}
MyTableView.qml
import QtQuick 2.8
import QtQuick.Controls 1.4
TableView {
property alias tableView: tableView
id: tableView
TableViewColumn {title: "id"; role: "1"; width: 120}
TableViewColumn {title: "操作系统"; role: "2"; width: 120}
TableViewColumn {title: "浏览器"; role: "3"; width: 120}
TableViewColumn {title: "访问时间"; role: "4"; width: 120}
TableViewColumn {title: "所有数据"; role: "5"; width: 120}
model: theModel
}
MySQL数据库