异度部落格

学习是一种生活态度。

0%

这次要学习一下,参数和环境变量

#!/bin/sh
echo "The program's name is $0"
echo "The first parameter is $1"
echo "The second parameter is $2"
echo "The parameter list is $*"
echo "The user's home directory is $HOME"
exit 0

在终端中输入:./ex_02.sh hello killua

执行结果:

The program's name is ./ex_02.sh
The first parameter is hello
The second parameter is killua
The parameter list is hello killua
The user's home directory is /home/killua

PS:主要环境变量都是大写的,写$Home 程序不会报错,但是没有结果

虽然知道 shell 好久,今天才开始好好学习 shell,鄙视下自己,下面都是练习代码写得不好大牛不要鄙视阿....

#!/bin/sh
var="Hello World"
echo $var
echo "$var"
echo '$var'

运行结果:

Hello World
Hello World
$var

功能很简单就是显示变量,第一个是直接回显,第二个和第三个感觉很相似,还是有很大区别的。如果你把一个带有$字符的变量放在双引号中,就是把它作为变量使用,如果你是放在单引号中就是没有作用,单引号里面可以放一些不含变量的字符串

还有一个点就是权限,chmod +x ex_01.sh

ok

今天 csdn 的个人空间升级了,恭喜下...

firefox 的在线收藏问题,其实就是一个插件http://download.xmarks.com/download/all  貌似这个插件还支持,IE 和 Safari 什么时候支持下 chrome,等 Linux 版的 chrome 稳定的,我就换 Chrome 的说

多线程这个部分看得晕晕的....

随便搞了段代码...

===================================================================

部分代码如下:

threads.h

#ifndef THREADS_H
#define THREADS_H
#include <QThread>
class Threads : public QThread
{
Q_OBJECT
public:
Threads();
void setMessage(const QString &message);
void stop();
protected:
void run();
private:
QString messageStr;
volatile bool stopped;
};
#endif // THREADS_H

threads.cpp

#include <QtCore>
#include <iostream>
#include "threads.h"
Threads::Threads()
{
stopped = false;
}
void Threads::setMessage(const QString &message)
{
messageStr = message;
}
void Threads::run()
{
while(!stopped)
std::cout << qPrintable(messageStr) <<std::endl;
}
void Threads::stop()
{
stopped = true;
}

threadDialog.h

#ifndef THREADDIALOG_H
#define THREADDIALOG_H
#include <QDialog>
#include "threads.h"
class ThreadDialog : public QDialog
{
Q_OBJECT
public:
ThreadDialog(QWidget *parent = 0);
protected:
void closeEvent(QCloseEvent *event);
private slots:
void startOrStopThreadA();
void startOrStopThreadB();
private:
Threads threadA;
Threads threadB;
QPushButton *threadAButton;
QPushButton *threadBButton;
QPushButton *quitButton;
};
#endif // THREADDIALOG_H

threadDialog.cpp

#include <QtGui>
#include "threaddialog.h"
ThreadDialog::ThreadDialog(QWidget *parent) : QDialog(parent)
{
threadA.setMessage("Thread A is start");
threadB.setMessage("Thread B is start");
threadAButton = new QPushButton(tr("Start A"));
threadBButton = new QPushButton(tr("Start B"));
quitButton = new QPushButton(tr("Quit"));
quitButton->setDefault(true);
connect(threadAButton, SIGNAL(clicked()),this, SLOT(startOrStopThreadA()));
connect(threadBButton,SIGNAL(clicked()), this, SLOT(startOrStopThreadB()));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(threadAButton);
layout->addWidget(threadBButton);
layout->addWidget(quitButton);
setLayout(layout);
setWindowTitle(tr("Thread Test"));
}
void ThreadDialog::startOrStopThreadA()
{
if (threadA.isRunning()) {
threadA.stop();
threadAButton->setText(tr("Start A"));
} else {
threadA.start();
threadAButton->setText(tr("Stop A"));
}
}
void ThreadDialog::startOrStopThreadB()
{
if (threadB.isRunning()) {
threadB.stop();
threadBButton->setText(tr("Start B"));
} else {
threadB.start();
threadBButton->setText(tr("Stop B"));
}
}
void ThreadDialog::closeEvent(QCloseEvent *event)
{
threadA.stop();
threadB.stop();
threadA.wait();
threadB.wait();
event->accept();
}

这个程序没有优化,感觉有点站内存的说,看看就好哈

Multiget 安装

sudo apt-get install multiget

把 Multiget 和 Firefox 连接起来的方法很简单,通过 Flashgot 插件完成

  1. 打开 Flashgot 选项,点击"常规"标签页。

  2. 因为下载管理器里面是没有 Multiget 的,所以点击"新增",填入 Multiget。

  3. 选择程序/usr/bin/multiget

  4. 在参数模板中写入,把全角的括号改成半角的。 引用

[url=URL][refer=REFERER]

  1. 确定 OK。

thunderbird邮件提醒设置 如果你使用 Ubuntu 9.04,给 ThunderBird 安装Mozilla Notification Extensions就可以让它使用 LibNotify 新通知系统 下载地址:https://addons.mozilla.org/en-US/thunderbird/addon/11530

thunderbird最小化到托盘 由于 thunderbird 没有最小化到托盘的扩展,这里用Alltray来模拟: sudo apt-get install alltray

新建文件,thunderbirdStart.sh 内容如下:

#!/bin/bash
alltray thunderbird

保存退出,并使文件可执行

打开System->Preferences->Sessions 添加此文件使之开机时运行

同样的方法也可以添加其他程序

添加联系人侧边栏 安装Contacts Sidebar插件就可以了 下载地址:https://addons.mozilla.org/en-US/thunderbird/addon/70

方法一:添加命令到rc.local

方法二:系统->首选项->启动程序(会话)

Ubuntu 运行程序不用密码的方法,其实就是修改权限

sudo chmod 4577 XXX

显然 include 是没错的,可是怎么也找不到这个文件,程序编译不过.....

解决方案如下:

在 XXX.pro 文件中添加一行

QT += sql

就可以了,这个方法还可以用再其他场合...要是找不到什么头文件可以考虑试试

这里利用 Qt4+MySQL 做一个简单的例子

不会连接 MySQL 的,参考这里:http://blog.csdn.net/killua_hzl/archive/2009/07/29/4391956.aspx

代码如下:

studentmanage.h

#ifndef STUDENTMANAGE_H
#define STUDENTMANAGE_H
#include <QtGui/QWidget>
class QSqlTableModel;
class QTableView;
enum { Stu_Sno = 0,Stu_Name = 1,Stu_Age = 2,Stu_Math = 3,Stu_Chinese = 4,Stu_English = 5 };
class StudentManage : public QWidget
{
Q_OBJECT
public:
StudentManage(QWidget *parent = 0);
~StudentManage();
private:
QSqlTableModel *model;
QTableView *view;
};
#endif // STUDENTMANAGE_H

studentmanage.cpp

#include <QtGui>
#include <QtSql>
#include "studentmanage.h"
StudentManage::StudentManage(QWidget *parent)
: QWidget(parent)
{
model = new QSqlTableModel(this);
model->setTable("student");
model->setSort(Stu_Sno, Qt::AscendingOrder);
model->setHeaderData(Stu_Name, Qt::Horizontal, tr("Name"));
model->setHeaderData(Stu_Age, Qt::Horizontal, tr("Age"));
model->setHeaderData(Stu_Math, Qt::Horizontal, tr("Math"));
model->setHeaderData(Stu_Chinese, Qt::Horizontal, tr("Chinese"));
model->setHeaderData(Stu_English, Qt::Horizontal, tr("English"));
model->select();
view = new QTableView;
view->setModel(model);
view->setSelectionMode(QAbstractItemView::SingleSelection);
view->setSelectionBehavior(QAbstractItemView::SelectRows);
view->resizeColumnsToContents();
view->setEditTriggers(QAbstractItemView::NoEditTriggers);
QHeaderView *headerView = view->horizontalHeader();
headerView->setStretchLastSection(true);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(view);
setLayout(layout);
setWindowTitle(tr("Student Manage"));
}
StudentManage::~StudentManage()
{
}

main.cpp

#include <QtGui/QApplication>
#include <QtSql>
#include <QMessageBox>
#include "studentmanage.h"
/**
建立数据库链接
*/
bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("DB_Stu");
db.setUserName("root");
db.setPassword("123456");
if (!db.open()) {
QMessageBox::warning(0, QObject::tr("Database Error"),
db.lastError().text());
return false;
}
return true;
}
/**
创建数据
*/
void createData()
{
QSqlQuery query;
query.exec("Drop table student"); //如果表存在删除
//建表
query.exec("Create table student ("
"Sno int not null,"
"Name varchar(40) not null,"
"Age int not null,"
"Math int not null,"
"Chinese int not null,"
"English int not null)");
//插入数据
query.exec("Insert into student (Sno,Name,Age,Math,Chinese,English) values(1,'Dear_fox',1,100,100,100)");
query.exec("Insert into student (Sno,Name,Age,Math,Chinese,English) values(2,'Killua',2,99,98,98)");
}
/**
主函数
*/
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
bool isCreate = !QFile::exists("DB_Stu");
if (!createConnection())
return 1;
if(isCreate)
createData();
StudentManage studentManage;
studentManage.resize(400,300);
studentManage.show();
return a.exec();
}

结果截图: ed4fa4671bc2ab0697d9b2d1680f92d8.png

貌似好久没写了,最近比较忙的说,这个主要练习 Model 的设计,部分代码是参考别人的,大牛们不要鄙视啊....

源码最后附上。

部分核心代码如下:

booleanmodel.h

#ifndef BOOLEANMODEL_H
#define BOOLEANMODEL_H
#include <QAbstractItemModel>
class Node;
class BooleanModel : public QAbstractItemModel
{
Q_OBJECT
public:
BooleanModel(QObject *parent = 0);
~BooleanModel();
void setRootNode(Node *node);
QModelIndex index(int row, int column, const QModelIndex &parent) const;
QModelIndex parent(const QModelIndex &child) const;
int rowCount(const QModelIndex &parent) const;
int columnCount(const QModelIndex &parent) const;
QVariant data(const QModelIndex &index, int role) const;
QVariant headerData(int section,Qt::Orientation orientation, int role) const;
private:
Node *nodeFromIndex(const QModelIndex &index) const;
Node *rootNode;
};
#endif // BOOLEANMODEL_H

booleanmodel.cpp

#include <QtCore>
#include "booleanmodel.h"
#include "booleanparser.h"
/**
构造函数,初始化根结点为空
*/
BooleanModel::BooleanModel(QObject *parent) :QAbstractItemModel(parent)
{
rootNode = 0;
}
/**
析构函数,删除根结点
*/
BooleanModel::~BooleanModel()
{
delete rootNode;
}
/**
设置根结点,删除原来根结点,重新设置根结点
*/
void BooleanModel::setRootNode(Node *node)
{
delete rootNode;
rootNode = node;
reset(); //刷新表格数据
}
/**
返回索引,这个是virtual函数所以一定要重载
*/
QModelIndex BooleanModel::index(int row,int column,const QModelIndex &parent) const
{
if (!rootNode || row < 0 || column < 0)
return QModelIndex(); //非法情况
Node *parentNode = nodeFromIndex(parent);
Node *childNode = parentNode->children.value(row);
if (!childNode)
return QModelIndex();
return createIndex(row,column, childNode);
}
/**
返回父亲结点
*/
QModelIndex BooleanModel::parent(const QModelIndex &child) const
{
Node *node = nodeFromIndex(child);
if (!node)
return QModelIndex(); //结点无效
Node *parentNode = node->parent;
if (!parentNode)
return QModelIndex(); //父结点无效
Node *grandparentNode = parentNode->parent;
if(!grandparentNode)
return QModelIndex(); //父结点的父结点无效
int row = grandparentNode->children.indexOf(parentNode);
return createIndex(row,0,parentNode);
}
/**
返回行数,这个也是virtual函数,要重载
*/
int BooleanModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
return 0;
Node *parentNode = nodeFromIndex(parent);
if(!parentNode)
return 0;
return parentNode->children.count();
}
/**
返回列数,固定只有2,这个也是virtual函数,要重载
*/
int BooleanModel::columnCount(const QModelIndex & /*parent*/) const
{
return 2;
}
/**
返回数据,这个也是virtual函数,要重载
*/
QVariant BooleanModel::data(const QModelIndex &index, int role) const
{
if (role != Qt::DisplayRole)
return QVariant(); //不是显示角色,认为是无效数据
Node *node = nodeFromIndex(index);
if (!node)
return QVariant(); //结点无效
if(index.column() == 0) {
switch (node->type) {
case Node::Root:
return tr("Root");
case Node::OrExpression:
return tr("OR Expression");
case Node::AndExpression:
return tr("AND Expression");
case Node::NotExpression:
return tr("NOT Expression");
case Node::Atom:
return tr("Atom");
case Node::Identifier:
return tr("Identifier");
case Node::Operator:
return tr("Operator");
case Node::Punctuator:
return tr("Punctuator");
default:
return tr("Unknown"); //默认为未知
}
} else if(index.column() == 1) {
return node->str;
}
return QVariant();
}
/**
返回表头
*/
QVariant BooleanModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
if (section == 0) {
return tr("Node");
} else if (section == 1) {
return tr("Value");
}
}
return QVariant();
}
/**
从索引返回结点
*/
Node *BooleanModel::nodeFromIndex(const QModelIndex &index) const
{
if (index.isValid()) {
return static_cast<Node *>(index.internalPointer());
} else {
return rootNode;
}
}

booleanparser.h

#ifndef BOOLEANPARSER_H
#define BOOLEANPARSER_H
#include "node.h"
class BooleanParser
{
public:
BooleanParser();
Node *parse(const QString &expr);
private:
Node *parseOrExpression();
Node *parseAndExpression();
Node *parseNotExpression();
Node *parseAtom();
Node *parseIdentifier();
void addChild(Node *parent, Node *child);
void addToken(Node *parent, const QString &str, Node::Type type);
bool matchToken(const QString &str) const;
QString in;
int pos;
};
#endif // BOOLEANPARSER_H

booleanparser.cpp

#include <QtCore>
#include "booleanparser.h"
#include "node.h"
BooleanParser::BooleanParser()
{
}
Node *BooleanParser::parse(const QString &expr)
{
in = expr;
in.replace(" ", "");
pos = 0;
Node *node = new Node(Node::Root);
addChild(node, parseOrExpression());
return node;
}
/**
解析或表达式
*/
Node *BooleanParser::parseOrExpression()
{
Node *childNode = parseAndExpression();
if (matchToken("||")) {
Node *node = new Node(Node::OrExpression);
addChild(node,childNode);
while (matchToken("||")) {
addToken(node,"||", Node::Operator);
addChild(node, parseAndExpression());
}
return node;
} else {
return childNode;
}
}
/**
解析与表达式
*/
Node *BooleanParser::parseAndExpression()
{
Node *childNode = parseNotExpression();
if (matchToken("&&")) {
Node *node = new Node(Node::AndExpression);
addChild(node, childNode);
while (matchToken("&&")) {
addToken(node, "&&", Node::Operator);
addChild(node, parseNotExpression());
}
return node;
} else {
return childNode;
}
}
/**
解析非表达式
*/
Node *BooleanParser::parseNotExpression()
{
if (matchToken("!")) {
Node *node = new Node(Node::NotExpression);
while (matchToken("!"))
addToken(node, "!", Node::Operator);
addChild(node, parseAtom());
return node;
} else {
return parseAtom();
}
}
/**
解析一个项
*/
Node *BooleanParser::parseAtom()
{
if (matchToken("(")) {
Node *node = new Node(Node::Atom);
addToken(node, "(", Node::Punctuator);
addChild(node, parseOrExpression());
addToken(node, ")", Node::Punctuator);
return node;
} else {
return parseIdentifier();
}
}
/**
解析标识符
*/
Node *BooleanParser::parseIdentifier()
{
int startPos = pos;
while (pos < in.length() && in[pos].isLetterOrNumber())
++pos;
if (pos > startPos) {
return new Node(Node::Identifier,
in.mid(startPos, pos - startPos));
} else {
return 0;
}
}
/**
添加子结点
*/
void BooleanParser::addChild(Node *parent, Node *child)
{
if (child) {
parent->children += child;
parent->str += child->str;
child->parent = parent;
}
}
/**
添加token
*/
void BooleanParser::addToken(Node *parent, const QString &str,
Node::Type type)
{
if (in.mid(pos, str.length()) == str) {
addChild(parent, new Node(type, str));
pos += str.length();
}
}
/**
token匹配
*/
bool BooleanParser::matchToken(const QString &str) const
{
return in.mid(pos, str.length()) == str;
}

booleanwindow.h

#ifndef BOOLEANWINDOW_H
#define BOOLEANWINDOW_H
#include <QWidget>
class QLabel;
class QLineEdit;
class QTreeView;
class BooleanModel;
class BooleanWindow : public QWidget
{
Q_OBJECT
public:
BooleanWindow();
private slots:private slots:
void booleanExpressionChanged(const QString &expr);
private:
QLabel *label;
QLineEdit *lineEdit;
BooleanModel *booleanModel;
QTreeView *treeView;
};
#endif // BOOLEANWINDOW_H

booleanwindow.cpp

#include <QtGui>
#include "booleanwindow.h"
#include "booleanmodel.h"
#include "booleanparser.h"
BooleanWindow::BooleanWindow()
{
//Label
label = new QLabel(tr("Boolean expression:"));
//LineEdit
lineEdit = new QLineEdit;
//BooleanModel
booleanModel = new BooleanModel(this);
//TreeView
treeView = new QTreeView;
treeView->setModel(booleanModel);
//signal and slot
connect(lineEdit, SIGNAL(textChanged(QString)), this,SLOT(booleanExpressionChanged(QString)));
//Layout
QGridLayout *layout = new QGridLayout;
layout->addWidget(label, 0 ,0);
layout->addWidget(lineEdit, 0 , 1);
layout->addWidget(treeView, 1, 0, 1, 2);
setLayout(layout);
setWindowTitle(tr("Boolean Parser"));
}
void BooleanWindow::booleanExpressionChanged(const QString &expr)
{
BooleanParser parser;
Node *rootNode = parser.parse(expr);
booleanModel->setRootNode(rootNode);
}

其他的可以去下载

下载地址:http://download.csdn.net/source/1529790">http://download.csdn.net/source/1529790

软件截图:

image

PS:最近比较郁闷.....