Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350760
  • 博文数量: 112
  • 博客积分: 5245
  • 博客等级: 大校
  • 技术积分: 1120
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-07 09:20
个人简介

静下来,定好方向,好好干。

文章分类
文章存档

2017年(1)

2012年(1)

2011年(5)

2010年(6)

2009年(16)

2008年(59)

2007年(24)

我的朋友

分类: LINUX

2007-11-13 16:11:39

关于qt:
qt是Trolltech奇趣科技的产品, 是一个 多平台的C++图形用户界面应用程序框架, 完全面向对象, 容易扩展. qt也是流行的linux桌面环境KDE的基础;
支持平台:   ms/windows-98, 2000, nt   unix/x11-linux   macintosh-mac os x   embedded-linux with framebuffer
 桌面系统 嵌入式系统 
 桌面环境
Gnome   KDE
 桌面环境
Qtopia
 Widget库
GTK, Qt
----------
X  Window
 图形库
Qtopia Core
 framebuffer
显示设备
framebuffer 

xwindow负责基本的画图操作, 如画点, 画线, 字体等;
桌面GTK应用更广泛, 因为完全开源, 免费;   嵌入式上Qt占优;

qt的工程管理工具qmake:
1. qmake -project   分析当前目录及子目录下的C++文件, 头文件, Qt的资源文件, 生成"当前目录名. pro".
2. 根据需要修改pro文件, 比如: QT += sql network等;
3. qmake   由pro文件生成Makefile文件.
4. make   编译整个项目. 生成的目标文件名是目录名;
若添加了新的代码文件或资源文件, 需回到第一步重来, 若代码文件与头文件依赖关系变化, 从第三步开始.

Qt的容器类:
QVector容器在内存中的布局类似于数组, 所有元素连续存放, 能用[]操作符随机访问, 可以维护自己的长度信息, 可以改变长度, 但在开头和中间添加元素效率较低;   如: QVector vect;   vect. append(1 . 0);   vect.append( 0. 534);   也可vect << 1.0 <<0.2;
遍历所有元素:  
double sun = 0.0;
for (int i = 0; i < vect. count(); ++i)
    sum += vect[i];

QLinkedList容器在内存中的布局类似链表, 使用不连续的内存, 因此不能用[]操作符随机访问, 但是添加删除元素的效率较高.  
它的访问使用iterator, 类似于访问数组的指针, 可以使用++, --运算符; 可以使用*来取得指向的元素;
begin()返回第一个iterator, end()则指向容器的最后一个元素的下一个元素位置( 访问该位置元素是非法的);
如果一个容器为空, begin() 和 end()相等;
QLinkedList: : iterator i = list.begin();
while (i ! = list.end()) {
    * i = qAbs(* i);
    ++i;
}

下例将ccc插到aaa, bbb之间:
QLinkedList list;
list.append("aaa");
list.append("bbb");
QLinkedList: : iterator i = list.find("bbb");
list.insert(i, "ccc");

配置文件类
QSettings settings( "setdir", "mysettings");   //设定文件目录和文件名

QString str( "abc" );
settings. setValue( "stringhaha", str);
settings. setValue( "section/dbnum", 10.1);
QString getstr = settings.value("stringhaha").toString();   //toStringList(), toBool()等
double getfloat = settings.value("section/dbnum").toDouble();

qDebug() << getstr << getfloat <程序会建立文件~/.config/setdir/mysettings.conf
[General]
stringhaha=abc
[section]
dbnum=10.1

Qt的输出:
1. 如果要用cout输出QString字符串, 需先把它转换成C++的const char *类型:
       cout << qPrintable ( str) <2. 也可以使用qDebug()输出;   qDebug() << str;   包含库

MVC数据模型:
各种GUI库都提供了model-view-controller, Model是数据模型, 负责读写数据源, 并把数据提供给view进行显示, 所有对数据源的操作都通过model来进行, controller负责view与用户的交互, 将用户的操作转换成请求来浏览和编辑数据, 然后view将用户对数据的操作传给model;
在Qt中有Model, View, Delegate, 其中Delegate相当于Controller.

Qt的I/O类:
QFile file("test.txt");
if (! file.open(QIODevice : : WriteOnly)) {
    cerr << "Cannot open file for writing: " << qPrintable(file. errorString()) <}

QTextStream out(&file);      //需要绑定到QFile, QString, QTcpSocket, QUdpSocket, QBuffer等
out << oct << 31 << endl;       //oct把后面的数转成8进制, file的内容为'37\n';
--------------------------
QFile file("test.txt");
if (! file.open(QIODevice : : ReadOnly)) {
    cerr << "Cannot open file for reading: " << qPrintable(file. errorString()) < }
QString str;
QTextStream in(&file);
in >> str;
--------------------------
一般写出去需要分割符, 如\n, 读的时候用readLine()可以读取一行;   也可一个一个字符读, QChar ch;   in >> ch;

中文功能读写socket的实现:
QTextStream in(tcpSocket);
in. setCodec(QTextcodec: : codecForName("UTF-8"));
response = in. readLine() + "\n";

QString通过socket发出字符串不带\0结尾, 所有server端不能对收到的字符直接用strlen判断, 需用\n判断;
或在末尾加\0, 然后再作其他处理;
reject()是隐藏窗体, 用{}可以析构掉窗体;

当多个客户端跟QTcpServer连接的时候,某个客户端跟服务器发送消息,我要怎么取得当前发送消息的QTcpSocket连接:
写这类程序的步骤
1、接收QTcpServer部件的newConnection()信号,将进入来的QTcpSocket的指针获取到,然后将 这个QTcpSocket部件的readyRead()、disconnect()等信号链接到另外的slot函数中,比如称作 onReadyRead(), onDisconnected()
2、在onReadyRead()或onDisconnected()函数中,用sender()函数获取到发送信号的QTcpSocket部件,比如:
QTcpSocket *socket = qobject_cast(sender());
获取到了与代表与某个客户端连接的QTcpSocket对象指针后,就可以对其进行其它操作了,比如要读数据、写数据、关闭等。
如果想主动发送一些信息,则需要你自己分类记忆索引记忆进入到的QTcpSocket对象指针列表,以方便操作,当然了,从QTcpServer对象的方法中可以获取到当前正在连接的所有QTcpSocket对象指针列表。

qt 的一些例程:
/******************************************************************
  * Filename : main.cpp
****************************************************************** */
 

#include

#include "log_reg.h"
#include "chat.h"

int main(int argc, char *argv[])
{
    char *myip = "127.0.0.1";
    int success = 0;
    if (argc > 1)
        myip = argv[1];

    QApplication app(argc, argv);

    QTcpSocket *tcpSocket = new QTcpSocket();
    tcpSocket->connectToHost(myip, 8000);
    {
    Log_reg lr(tcpSocket);
    lr.show();
    app.exec();
    success = lr.success;
    }
    if (success)
    {
    MyDialog m(tcpSocket);
    m.show();
    return app.exec();
    }
}

/******************************************************************
  * Filename : log_reg.h
 ******************************************************************* */
 

#ifndef DIALOG_LOG_REG   
#define DIALOG_LOG_REG

#include
#include

#include "ui_log_reg.h"

class Log_reg : public QDialog, public Ui_log_reg
{
    Q_OBJECT

public:
        Log_reg(QTcpSocket *mtcpSocket, QWidget *parent = 0);

private slots:
    void log_Deal();  //deal with login button;
    void reg_Deal();  //deal with regist button;
    void quit_Deal();  //deal with the quit button;
    void recvMsg();    //deal with recieve msg from net;
    void error();

private:
    QTcpSocket *tcpSocket;
    QString msgBuffer;
    int state;      //record the state if login or regist;
    int log_time;   //record the fault login times ,quit if > 3;
public:
    int success;  //reject if login success or in other reason;
};


#endif

/******************************************************************
 * Filename : log_reg.cpp
  ****************************************************************** */
 


#include
#include
#include
#include
#include

#include "log_reg.h"

Log_reg::Log_reg(QTcpSocket *mtcpSocket, QWidget *parent)
    :QDialog(parent)
{
    setupUi(this);
    tcpSocket = mtcpSocket;
    state = 0; //if 0 ,log else if 1,reg
    success = 0;
    log_time = 0;
   
    user->setMaxLength(10);
    pass->setMaxLength(10);
    pass->setEchoMode(QLineEdit::Password);
    connect(btn_log, SIGNAL(clicked()), this, SLOT(log_Deal()));
    connect(btn_reg, SIGNAL(clicked()), this, SLOT(reg_Deal()));
    connect(btn_quit, SIGNAL(clicked()), this, SLOT(quit_Deal()));

    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(recvMsg()));
    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
}

void Log_reg::log_Deal()
{
    if (1 == state)
    {
        btn_reg->resize(51,20);
        btn_log->resize(51,27);
        state = 0;
        dial_name->setText(QString::fromUtf8("登录!请输入名称与密码:"));
        user->clear();
        pass->clear();
    }
    else if (user->text() != "")
    {
        log_time++;
        if (log_time > 3)
        {
            QMessageBox::critical(this,
                    QString::fromUtf8("登录错误"),
                    QString::fromUtf8("登录次数超过3次!"));
            exit(1);
        }
        dial_name->setText(QString::fromUtf8("登录!请输入名称与密码:"));
        QTextStream out(tcpSocket);
        out << "0002" << ":" << user->text() << ":" << pass->text() << endl;
        user->clear();
        pass->clear();
    }
    else
    {
        dial_name->setText(QString::fromUtf8("登录!请输入名称与密码:"));
    }
}

void Log_reg::reg_Deal()
{
    if (0 == state)
    {
        btn_reg->resize(51,27);
        btn_log->resize(51,20);
        state = 1;
        dial_name->setText(QString::fromUtf8("注册!请输入名称与密码:"));
        user->clear();
        pass->clear();
    }
    else if (user->text() != "")
    {
        dial_name->setText(QString::fromUtf8("注册!请输入名称与密码:"));
        QTextStream out(tcpSocket);
        out << "0001" << ":" << user->text() << ":" << pass->text() << endl;
        user->clear();
        pass->clear();
    }
    else
    {
        dial_name->setText(QString::fromUtf8("注册!请输入名称与密码:"));
    }
}

void Log_reg::recvMsg()
{
    if(!tcpSocket->canReadLine())
        return;
    QTextStream in(tcpSocket);   //bind to socket
    in.setCodec(QTextCodec::codecForName("UTF-8"));  //change to standard code
    QString responseLine = in.readLine();
    if ("00021" == responseLine)
    {
        success = 1;
        reject();
    }
    else if ("00020" == responseLine)
        dial_name->setText(QString::fromUtf8("登录错误,请重新输入:"));
    else if ("00011" == responseLine)
    {
        btn_reg->resize(51,20);
        btn_log->resize(51,27);
        state = 0;
        dial_name->setText(QString::fromUtf8("登录!请输入名称与密码:"));
        user->clear();
        pass->clear();
    }
    else if ("00010" == responseLine)
        dial_name->setText(QString::fromUtf8("注册不成功,请重新注册:"));
}

void Log_reg::error()
{
    QMessageBox::critical(this, "socket error", tcpSocket->errorString());
    tcpSocket->close();
}

void Log_reg::quit_Deal()
{
    exit(0);
}

/******************************************************************
  * Filename : chat.h
 ****************************************************************** */
 

#ifndef CHAT01_H
#define CHAT01_H
#include
#include
#include "ui_chat.h"

class QStringListModel;

class MyDialog : public QDialog, public Ui_MyDialog
{
  Q_OBJECT
  public:
    MyDialog(QTcpSocket *tcpSocket, QWidget *parent = 0);
    int arg_Decode(QString &msgBuffer);
    void list_Refresh(QString &msgBuffer);  //to refresh the user list;
    void list_Clear();  //clear the user list;
  private slots:
    void sendMsg();
    void recvMsg();
    void kickoff();   // to kickoff a user;
    void silent();    // to keep a user silent;
    void getId(const QModelIndex&);   // to get the objname if doubleclick;
    void error();
  private :
    int state;    //record the the argument kind;
    QModelIndex index;   
    QStringList id;  
    QStringList leaders;    //listview contents
    QStringListModel *model; 

    QTcpSocket *tcpSocket0;
    QString msgBuffer;   //message received;
    QString orgname;  //record the sender's name;
    QString objname;  //record the reciever's name;
};

#endif

/******************************************************************
  * Filename : chat.cpp
  ****************************************************************** */
 


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

#include "chat.h"


MyDialog::MyDialog(QTcpSocket *tcpSocket, QWidget *parent)
  : QDialog(parent)
{
  setupUi(this);
  btn_kickoff->hide();
  btn_silent->hide();
  leaders<<"";
  id<< QString::fromUtf8("所有人");
  model = new QStringListModel(this);
  model->setStringList(leaders);
  listView->setModel(model);
  listView->setEditTriggers(NULL);
  state = 0;
  tcpSocket0 = tcpSocket;
  QTextStream out(tcpSocket0);
  out << "0015";
  lbl_chat->setText(QString::fromUtf8("我是,对所有人说:"));
  objname = QString::fromUtf8("所有人");
  lineEdit->setMaxLength(100);

  connect(btn_exit, SIGNAL(clicked()), this, SLOT(reject()));
  connect(btn_send, SIGNAL(clicked()), this, SLOT(sendMsg()));
  connect(btn_kickoff, SIGNAL(clicked()), this, SLOT(kickoff()));
  connect(btn_silent, SIGNAL(clicked()), this, SLOT(silent()));
  connect(listView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(getId(const QModelIndex&)));

  connect(tcpSocket0, SIGNAL(readyRead()), this, SLOT(recvMsg()));
  connect(tcpSocket0, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));
  //tcpSocket0->connectToHost(QHostAddress::LocalHost, 8000);
}

int MyDialog::arg_Decode(QString &lineBuffer)
{
  QStringList list, temp;
  list = lineBuffer.split(":");
  for(int i=1; i    temp<  lineBuffer = temp.join(":");
  if ("11111" == list[0])
  {
      return 1;
  }
  else if ("00151" == list[0])
  {
    return 2;
  }
  else if ("00141" == list[0])
    return 3;  //close the window
  else if ("00121" == list[0])
      return 4;   //someone is kicked off
  else
    return 0;
}

void MyDialog::list_Clear()
{
  while (model->rowCount()!=1)
  {
    model->removeRows(1,1);
  }
  index = model->index(0);
  model->setData(index, "");
}

void MyDialog::list_Refresh(QString &usernameList)
{
  QStringList temp;
  temp = usernameList.split(":");
  for (int i=0; i  {
    index = model->index(0);
    model->setData(index, temp[i]);
    model->insertRows(0, 1);
  }
//  model->insertRows(0, 1);
  index = model->index(0);
  model->setData(index, QString::fromUtf8("所有人"));
}

void MyDialog::sendMsg()
{
  QTextStream out(tcpSocket0);
  if(objname == QString::fromUtf8("所有人"))
  {
      if (lineEdit->text() != "")
          out<<"0010:"<text()<  }
  else
  {
      if (lineEdit->text() != "")
          out<<"0011:"<text()<  }
  lineEdit->clear();
}

void MyDialog::recvMsg()
{
  if (!tcpSocket0->canReadLine())
    return;
  QTextStream in(tcpSocket0);
  QTextStream out(tcpSocket0);
  in.setCodec(QTextCodec::codecForName("UTF-8"));
  QString responseLine;
  do {
    responseLine += in.readLine();
  } while (tcpSocket0->canReadLine());

  state = arg_Decode(responseLine);
  QStringList list = responseLine.split(":");
  if (0 == state || 4 == state)
  {
        QFile file("local_chat_record.txt");
        if (!file.open(QIODevice::Append))
        {
            //stderr << "Cannot open file for writing:" << endl;
            qDebug() << "open file error" << endl;
            return;
        }
        QDateTime qtime;
        responseLine = qtime.currentDateTime().toString("hh:mm:ss")
            + " " +  responseLine;
        QTextStream fout(&file);
        fout << responseLine << endl;
        file.close();
        msgBuffer += responseLine;
        msgBuffer += "\n";
        textBrowser->setText(msgBuffer);
        textBrowser->moveCursor(QTextCursor::End);
        if (4 == state && "admin" == orgname)
            out << "0016";   //a code to make sure i've recieved msg
  }
  else if (2 == state)
  {
          orgname = list[1];
        QString lbl_Show = QString::fromUtf8("我是") + orgname + QString::fromUtf8(",对") + objname + QString::fromUtf8("说:");
        lbl_chat->setText(lbl_Show);
        if ("admin" == orgname)
        {
            btn_kickoff->show();
            btn_silent->show();
        }
        out << "1111";
  }
  else if (3 == state)
      exit(0);
  else if (1 == state)
  {
    list_Clear();
    list_Refresh(responseLine);
  }
}


void MyDialog::getId(const QModelIndex& index)
{
  objname = model->data(index, 0).toString();
  QString lbl_Show = QString::fromUtf8("我是") + orgname + QString::fromUtf8(",对") + objname + QString::fromUtf8("说:");
  lbl_chat->setText(lbl_Show);
}

void MyDialog::kickoff()
{
  int row = listView->currentIndex().row();
  index = model->index(row);
  QString kickoff_name = model->data(index, 0).toString();
  QTextStream out(tcpSocket0);
  out << "0012:" << kickoff_name;
}

void MyDialog::silent()
{
  int row = listView->currentIndex().row();
  index = model->index(row);
  QString silent_name = model->data(index, 0).toString();
  QTextStream out(tcpSocket0);
  out << "0013:" << silent_name;
}

void MyDialog::error()
{
  QMessageBox::critical(this, "socket error",
      tcpSocket0->errorString());
  tcpSocket0->close();
}


/*a project with sql and socket function*/
/*main.cpp*/

#include
#include "client.h"

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

    if (!createConnection())
    {
        qDebug() << "in main error conn" << endl;
        return 1;
    }
        qDebug() << "in main wright conn" << endl;

    VoltForm  volt;

    volt.show();

    return app.exec();
}

/*client.h*/

#ifndef      CLIENT_H
#define      CLIENT_H

//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

//#include
#include
//#include
#include
#include

#include "ui_client.h"


void sendMsg(QTcpSocket *tcpSocket);
void sqlInsert(QString sqlinsert);
bool createConnection(void);

//QSqlDatabase db;
//int id;

class VoltForm : public QDialog ,public  Ui_Display
{
    Q_OBJECT

public:
    VoltForm(QWidget *parent = 0);

private slots:
    void rt_display(void);
    void history_quary(void);
    void set_rate(const QString &);
    void update(void);
    void recvMsg(void);
    void error(void);
    void finsh(void);

private:
    // if quaryButton press,set 1. rt_display() termited, then display quary result
    int rate_flag ;       
    int count;

    void init_com(void);

    QTimer *timer;
    QSqlTableModel *model;
    QTcpSocket *tcpSocket;
    QString msgBuffer;
};

#endif

/*client.cpp*/

#include
#include

#include "client.h"

QSqlDatabase db;
int id;

bool createConnection(void)
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("table.db");

    if (!db.open()){
            qDebug() << "open table.db false!!!" << endl;
            return false;
    }

    // set data table id for query and real time display
    QSqlQuery query;

    query.exec("create table data (ID int primary key,Time text,Data1 text,Data2 text);");
    query.exec("select * from data");
    if (query.last())
        id = query.value(0).toInt();
    else
        id = 0;

    qDebug() << "###### id ="<    return true;
}

VoltForm::VoltForm(QWidget *parent) : QDialog(parent)
{
    setupUi(this);

    rate_flag = 0;       
    count = 10000;


    tcpSocket = new QTcpSocket(this);
    tcpSocket->connectToHost("192.168.15.15",8000);

    model = new QSqlTableModel(this);
    model->setTable("data");             

    // set column header
    model->setHeaderData(1, Qt::Horizontal, (QString::fromUtf8("日           期")));
    model->setHeaderData(2, Qt::Horizontal, (QString::fromUtf8("电  压1(mv)")));
    model->setHeaderData(3, Qt::Horizontal, (QString::fromUtf8("电  压2(mv)")));

    tableView->setModel(model);
    tableView->setColumnHidden(0, true);
   
    //  set font in the tableView
    QFont font("Helvetica", 12, QFont::Bold);
     tableView->setFont(font);


    qDebug() << "initialize nishhinihsiji ******************" << endl;


    init_com();
    update();

    timer = new QTimer(this); // for set the reflash rate
    timer->start(count);      // set time interval

    connect(timer, SIGNAL(timeout()), this, SLOT(update()));
    connect (displayButton,SIGNAL(clicked()),this, SLOT(rt_display()));
    connect (quaryButton,SIGNAL(clicked()),this, SLOT(history_quary()));
    connect (set_comboBox,SIGNAL(currentIndexChanged(const QString & )),
             this, SLOT(set_rate(const QString & )));

    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(recvMsg()));
    connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error()));

    // for close the data batabase when reject the dialog
    connect(closeButton,SIGNAL(clicked()),this,SLOT(reject()));
    connect(this,SIGNAL(rejected()),this,SLOT(finsh()));

    qDebug() << "initialize 3 ******************" << endl;
}

#if 1
void VoltForm ::init_com(void)
{
    //year
        yea_Box->addItem("1970");
    for (int i=2007; i <= 2010;i++ )
        yea_Box->addItem(QString::number (i,10));

    // 1~9 of date
    for (int i=1; i <= 9;i++ )
    {
        mon_Box->addItem("0" + QString::number (i,10));
        day_Box->addItem("0" + QString::number (i,10));
    }
    // 0~9 of time
    min_Box->addItem("--");
    sec_Box->addItem("--");
    for (int i=0; i <= 9;i++ )
    {
        hou_Box->addItem("0" + QString::number (i,10));
        min_Box->addItem("0" + QString::number (i,10));
        sec_Box->addItem("0" + QString::number (i,10));
    }

    //month
    for (int i=10; i <= 12;i++ )
        mon_Box->addItem(QString::number (i,10));

    //day
    for (int i=10; i <= 31;i++ )
        day_Box->addItem(QString::number (i,10));

    //hour
    for (int i=10; i <= 23;i++ )
        hou_Box->addItem(QString::number (i,10));
    hou_Box->setCurrentIndex(12);

    //miniute
    for (int i=10; i <= 59;i++ )
        min_Box->addItem(QString::number (i,10));

    //second
    for (int i=10; i <= 59;i++ )
        sec_Box->addItem(QString::number (i,10));

//    for (int i=1; i < 10;i++ )
//        set_comboBox->addItem(QString::number (i,10));

    QStringList rate;
    rate <<"200ms" << "500ms" << "1s" << "2s" << "5s" << "10s" << "30s" ;
        set_comboBox->addItems(rate);
    set_comboBox->setCurrentIndex(5);
}
#endif

#if 1
void VoltForm :: update()
{
    int temp_id;

    qDebug() << "update out=====================" << endl;

    sendMsg(tcpSocket);

    if(!rate_flag)
    {
        if (id >= 8)
            temp_id  = id -8;
        else
            temp_id = 0;

        qDebug() << "update in=====================" << endl;
        model->setSort(0, Qt::AscendingOrder);
        model->setFilter("ID > " + QString::number(temp_id));
//        model->setFilter("scorea < 50");
        model->select();
        tableView->resizeColumnsToContents();
        tableView->setColumnHidden(0, true);
    }

}
// display the real_time sample
void VoltForm :: rt_display()
{
    qDebug() << "ir_display=====================" << endl;

    qDebug() << "rate_flag 1=" << rate_flag << endl;

    rate_flag = 0;  // enable update the real_time display
    qDebug() << "rate_flag 2=" << rate_flag << endl;
}
void VoltForm :: history_quary()
{

    rate_flag = 1;  //disable the rt_display

    qDebug() << "rate_flag 3=" << rate_flag << endl;

    // make up the date for query
    QString date;
   
    if (min_Box->currentText() == "--")
    date = yea_Box->currentText() + "." + mon_Box->currentText() + "."
            + day_Box->currentText() + "-" + hou_Box->currentText() + "%";
    else if (sec_Box->currentText() == "--")   
    date = yea_Box->currentText() + "." + mon_Box->currentText() + "."
            + day_Box->currentText() + "-" + hou_Box->currentText() + ":"
            + min_Box->currentText() + "%" ;
    else
    date = yea_Box->currentText() + "." + mon_Box->currentText() + "."
            + day_Box->currentText() + "-" + hou_Box->currentText() + ":"
            + min_Box->currentText() + ":" + sec_Box->currentText() + "%" ;

    // display the table

//    model->setSort(0, Qt::DescendingOrder);
    QSqlQuery  query;
    query.exec("select * from  data where Time like '" + date +"'");
    if(query.next())
    {
        model->setFilter("Time like '" + date +"'");
        //    model->setFilter("Time >= '" + date+".000' and Time <= '" + date + ".999'");
        model->select();
        tableView->setColumnHidden(0, true);
    }
    else
        QMessageBox::critical(this,QString::fromUtf8("警告") ,QString::fromUtf8("您要的记录不存在!!!"));

    qDebug()<< date << endl;

}
void VoltForm :: set_rate(const QString &rate)
{
    bool ok;
    QString ms = "ms",c;

    if(rate.count(ms))
    {
        c= rate.left(rate.length() - 2);
        count = c.toInt(&ok,10);
    }
    else
    {
        c= rate.left(rate.length() - 1);
        count = c.toInt(&ok,10) * 1000;
    }

    timer->start(count);   // set the time interval


    qDebug() << "set_rate=" << rate << endl;
    qDebug() << "count=" << count << endl;
}
#endif

#if 1
void sendMsg(QTcpSocket *tcpSocket)
{
    QTextStream out(tcpSocket);
    qDebug()<<"input ask........";
//    out<text()<//    lineEdit->clear();
    out<<"ask"<}



void VoltForm::recvMsg()
{
    if(!tcpSocket->canReadLine())
        return;
    qDebug()<<"-------------";
   
    QTextStream in(tcpSocket);
    QString responseLine;

    do{
       
    }
    while(tcpSocket->canReadLine());

    qDebug()<    msgBuffer = responseLine;

    sqlInsert(msgBuffer);

   
}

/* insert sqlite */
void sqlInsert(QString sqlinsert)
{
    QString time ;
    QString data1;
    QString data2;
    QDateTime qtime;

    id++;
    QStringList temp;
    temp = sqlinsert.split("#");
    data1 = temp[0];
    data2 = temp[1];
    qDebug()<<"data1:"<    qDebug()<<"data2:"<   
    /*computer time*/
    time = qtime.currentDateTime().toString("yyyy.MM.dd");
    time +="-";
    time += qtime.currentDateTime().toString("hh:mm:ss.zzz");
    qDebug()<<"time:"<   
    /*operate sqlite3*/
    qDebug()<<"Insert start......";
    QSqlQuery query(db);
//    id = query.size();
    query.prepare("INSERT INTO data(ID, Time, Data1, Data2) VALUES (?, ?, ?, ?);");
    query.addBindValue(id);
    query.addBindValue(time);
    query.addBindValue(data1);
    query.addBindValue(data2);

    qDebug()<<"ID:"<    qDebug()<<"time:"<    qDebug()<<"data1:"<    qDebug()<<"data2:"<
    query.exec();
}

void VoltForm ::finsh(void)
{
    qDebug() << "in finsh============" << endl;
    db.close();
}

void VoltForm::error()
{
    QMessageBox::critical(this, "socket error",tcpSocket->errorString());
    tcpSocket->close();
}
#endif


信息点滴:
alt + tab   可切换工作面
在一个xterm下,   ctrl + alt + t可新开一个tab;   
各tab可用alt + num切换, 或者ctrl + pageup/pagedown切换;
unix上换行\n, windows上换行是\r\n;
POSIX标准看文件apue.
C++看C++ primer;
man Xorg / man XFree86
/etc/X11/xorg.conf       /etc/X11/xinit/xinitrc
Xorg --configure让X探测硬件并产生配置文件
startx启动X界面;
useradd -c "mary smith" mary
passwd mary
mount查看可以挂载的分区情况;
nmap探测网络服务
vim的颜色设置:
在$VimRunTime/colors下找颜色方案, 拷贝到mkdir ~/.vim/colors下面, 进行修改:
term黑白终端   cterm彩色终端   gui GUI版本   ctermfg前景   ctermbg背景
: hilight Comment ctermfg=green guifg=green
测试常用颜色组合:
: edit $VIMRUNTIME/syntax/colortest vim
: source %
图形界面下双击运行server, 该进程可在后台运行, 所以看起来server关闭了, 仍然可以连接.
全局变量应是一些关键性的东西, 并且应该有注释;
GPL - C++-QT-KDE
LGPL-C-GTK-GNOME


阅读(2029) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~