- 论坛徽章:
- 0
|
关于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 容器在内存中的布局类似链表, 使用不连续的内存, 因此不能用[]操作符随机访问, 但是添加删除元素的效率较高.
它的访问使用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()
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 > 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()是隐藏窗体, 用{}可以析构掉窗体;
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 text() text() 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 text() text() 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();
leaderssetStringList(leaders);
listView->setModel(model);
listView->setEditTriggers(NULL);
state = 0;
tcpSocket0 = tcpSocket;
QTextStream out(tcpSocket0);
out 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; irowCount()!=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; iindex(0);
model->setData(index, temp);
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() != "")
outtext()text() != "")
outtext()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 setText(msgBuffer);
textBrowser->moveCursor(QTextCursor::End);
if (4 == state && "admin" == orgname)
out setText(lbl_Show);
if ("admin" == orgname)
{
btn_kickoff->show();
btn_silent->show();
}
out 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 currentIndex().row();
index = model->index(row);
QString silent_name = model->data(index, 0).toString();
QTextStream out(tcpSocket0);
out 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()
#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() 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() 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() addItem("1970");
for (int i=2007; i addItem(QString::number (i,10));
// 1~9 of date
for (int i=1; i 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 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 addItem(QString::number (i,10));
//day
for (int i=10; i addItem(QString::number (i,10));
//hour
for (int i=10; i addItem(QString::number (i,10));
hou_Box->setCurrentIndex(12);
//miniute
for (int i=10; i addItem(QString::number (i,10));
//second
for (int i=10; i addItem(QString::number (i,10));
// for (int i=1; i addItem(QString::number (i,10));
QStringList rate;
rate addItems(rate);
set_comboBox->setCurrentIndex(5);
}
#endif
#if 1
void VoltForm :: update()
{
int temp_id;
qDebug() = 8)
temp_id = id -8;
else
temp_id = 0;
qDebug() setSort(0, Qt::AscendingOrder);
model->setFilter("ID > " + QString::number(temp_id));
// model->setFilter("scorea select();
tableView->resizeColumnsToContents();
tableView->setColumnHidden(0, true);
}
}
// display the real_time sample
void VoltForm :: rt_display()
{
qDebug() 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 select();
tableView->setColumnHidden(0, true);
}
else
QMessageBox::critical(this,QString::fromUtf8("警告") ,QString::fromUtf8("您要的记录不存在!!!"));
qDebug()start(count); // set the time interval
qDebug() text()clear();
outcanReadLine())
return;
qDebug()canReadLine());
qDebug()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
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/53151/showart_421307.html |
|