Author Archives: nanzt

revised lineEdit

目的:当第一次鼠标点到lineEdit的时候,自动将全部的text选上,而不是默认的将光标置于某个位置
解决方法:重载focusInEvent
难点:click时产生QFocusEvent和MousePressEvent,然后MousePressEvent的处理在focusInEvent之后,所以即便将focuseInEvent重载后,mouse event还是会将光标置到点击的位置,而不是选定全部。这时首先判断是否因为mouse focus造成的,如是的话,由重新发送一个由tab引发的focus in(默认的tab引起的focus是全部选定)。这样就ok了。
 
Purpose: when the first time of clicking the line edit widget, the whole text will be selected, other than the default action, which would insert the cursor in a certain position in the line edit.
Solution: override the focusInEvent virtual method.
Points to be specially addressed:  In a normal way, when the focus changed by the mouse clicking, the default action includes (1) invoking focusIn event which in turn calls the focusInEvent, and (2) normal mouse event which inserts the cursor into a proper position. This will bring a problem, whatever you set the focusInEvent event (for ex., call selectionAll to select all the text) , the normal mouse event will move the cursor to a certain position. A workaround is listing as following:  first check the reason causing the focus change, if it’s due to mouse click, then post in the event queue a tab focus event which by defaut will select all the text in a line edit widget.  
 
 
 
代码:
void PmFocusHandledLineEdit::focusInEvent( QFocusEvent * event )
{
  if (event->reason()==Qt::MouseFocusReason)
  {
    QFocusEvent *fe=new QFocusEvent(QEvent::FocusIn, Qt::TabFocusReason);
    QApplication::postEvent(this, fe);
  }
  else
  {
    QLineEdit::focusInEvent(event);
  }
}

一个Switzerland的博后机会

水文模型方向,做海河流域
有兴趣的朋友请与他们联系。详细见附件

点击这里

声明一下,我只帮他们做了个广告,我也不认识对方是什么人,所以我也无能为力,帮不上。大家可以直接通过附件里的方式跟对方联系。

about W’s graduate

尽管说兰州不好,很多方面比不上北京,但毕竟这里有熟悉的同事和领导,和自己熟悉的业务。很是希望他能留下,大家相补有无,相信还是能做出东西来的。不一定要是什么大成果,至少踏实干活了也无愧于自己。

看到一个对比中美文化的文章,很精彩

摘录几段过来,我们难道不要好好思索一下吗?如何学习国外先进经验不是靠叫靠嚷就出来了,要认识到自己的不足,踏实虚心地向人家学。下面提到的是个别现象,我不感觉。当然政府在改,制度也在完善(尽管还有很多不足)。晕,说教开了,就此打住,请读文!


回国后首当其冲就是车文化冲突了。在美国天天开车,可从来就没想过车还
有什么文化,坐进去开就是了。只要不违反交通规则,别叫警车跟上,就平安无
事。交通规则已经“溶化在血液中,落实在行动上”,一旦坐在方向盘后面,遵
守交通规则已经成为下意识的反应,根本不经过思考。与其它车同开在路上,虽
说做不到礼让,却也能彼此尊重,配合默契。……
回国到深圳以后,天天看着满街杂乱无序、横冲直撞的汽车,突然觉得车是
有文化的。光有车,会开车, 道路设施齐备不等于有车文化。车文化实际上就是
人的文化,思维方式和行为举止的延伸,通过车表现出来。要定义车文化,首先
要给车做个定义。也许有人会说,谁不知道车是什么,要你废话。不过我觉得,
车的定义在中国和美国是不同的。我刚到美国时,有一个老太太曾经对我说:
“what is a car? A car is a machine with four wheels can take you to
wherever you want to go.”(车是什么?车是一个有四个轱辘的机器,可以带
你去你想去的地方)。可以说,这就是车在美国的定义。在中国,这个定义上是
绝对不被认同的,车是财富、权利、地位、新潮的象征,是骄傲和炫耀的资本。
至少是舒服、享受和寻找刺激所必须的条件。开车的对没车的就有优越感,开好
车的对开经济型车的就更霸道。我在深圳就经常看到报道说,开奔驰、宝马的对
挡了他去路的行人车辆大打出手,而且无人敢拦。
在美国开车,除了遵守交通规则,还靠驾驶人彼此的默契和相互尊重。如果
我在安全距离内准备换车道,给出信号后,在绝大多数情况下,我敢肯定,我可
安全换车道,不必担心后车突然加速堵住我的路。这就需要对方对我的意图的理
解,配合。更主要的是,交通法规对换车道有严格规定,只要我按法定程序和要
求操作,没有人愿意冒险当过失方冲撞我。可是,在国内,只要给出换道信号,
后面车反而加速超前,堵住我换道后就减速并行。其目的并非要加速行驶,就是
不让我开到他前面去。所以我看多数人换车道不给信号,而是突然插入别人车道。
停下来,确认把对方逼停了,才向前开。
……
……感觉最深的就是这里的商人不讲什么规矩,也不懂什么规矩,
只要能赚钱,胆子足够大,那真是“人有多大胆,地有多大产”。深圳阔人常挂
在嘴边的一句话是:“英雄莫问出处。”说白了就是,我有钱,你别管怎么来
的。在这里,衡量成功、地位、能力的标准就一个字,钱。什么社会贡献,道德
标准,知识学问(如果和赚钱无关)在深圳人眼里是统统没有价值的。我觉得,
最能代表深圳文化的是“深圳热线”上4月23号刊载的一篇文章,《富人没必要
讲道德,穷人没资格谈道德》,作者署名“曹碧霜”,我都用不着引用其内容,
光看题目和作者名字的谐音,就知道他要说什么。这篇文章,代表了绝大多数先
富起来的深圳人的心态。
习惯了美国人权平等,劳工保护的现代资本主义体制,到深圳就好象回到狄
更斯在《艰难时世》里描写的,贪婪和肮脏的资本原始积累阶段,和现代生产对
工人盘剥压迫的卓别林《摩登时代》。在港资、台资的工厂里,工人就像包身工
一样失去人身自由,星期天也不许出厂门,出去就算旷工,扣三天工资。像加班
不给加班工资,超时加班,病假、迟到算旷工,肆意罚款,试用期解雇不发工资,
侮辱殴打雇员,性侵犯女工,是这些香港台湾老板司空见惯的家常便饭。可是在
美国,随便其中一条就可以让他们倾家荡产和官司缠身。……

(引自http://www.xys.org/xys/magazine/GB/2005/xys0508.txt

周末,与Prof zhang一起去玩

开了近2小时的车,先是参观了US Air Force Academy,兴趣不减,又跑去Garden of
God,看奇形怪状的rock。附两张照片。其实国内比如黄山、雁荡山远在Garden of
God之上,不过因为国内总是一些东西不是让游客玩的很高兴,不比在这里好象很自由,没人任何人可以约束你。看到很多攀岩高手。美国人喜欢
adventure。

Qs on OMS

1. introduce something about OMS
2. if there is a generic model interface, with which, any hydrological
models can be easily plugged in the system
3. integrate models together, such as hydrological, ecological and economic
models. How to implement?
4. any example on developing DSS on top of OMS?
5. any ideas on how to build a practical SDSS?
6. how to incorporate expert system into a DSS system? its presentation,
functions, etc.

7. does OMS support graphically model composition as MMS does?
yes, but implement in a tree organization. the graphic build is
underconstruction.
8. the OMS/JavaBean framework same as the JavaBean IDE?
no. the framework is based on the IDE, with a major extension.
9. is it too complicated as a DSS?
the purpose of OMS is not to be a DSS. It provides a flexible framework to
build a hydrological model.

10. 对于模型计算过程中的状态变化,可以采用程序调试时的debug模式,将重要变量
以列表方式表示出来。很不错。OMS是这样做的。

11. How about the efficiency compiling and running under Java virtual
machine?
it’s a little bit slower than MMS. But it is affordable for current
computers.
12. How to incorporate GIS functions?
13. Is the latest version available through Internet?
No.

顺便帖两张Colorado State Univ的校园照吧,很漂亮,跟国内的大学没法子比。

Qt中的事件

Qt中的事件
giscn[at]msn[dot]com
 
1.事件的来源
来源于a)windows系统的事件,经Qt的包装(如QMouseEvent);b)Qt内置的事件(如
QTimerEvent);c)应用程序自定义的事件
 
2.处理事件的位置
2.1 重载虚拟方法
比如一个按钮,要自定义左键click时的行为,可以这样做:
a. 从一个QPushButton上派生一个子类如MyPushButton
b. 重载void MyPushButton::mousePressEvent(QMouseEvent *event)
* 一般来讲,除非要重新改写这个事件的默认行为,否则如果仅是为了扩展其功能,最好在
函数的最后call默认的父类处理方法,如QPushButton::mousePressEvent(event);
* 在调用mousePressEvent这些特定的事件前,先调用通用事件处理函数,QObject::event()
。因此,对于一些特殊情况,我们必须要重定义event(),如我们要对tab键进行重定义。默
认的tab键是将focus移到下一个widget上去。比如在一个text edit widget里,我们可能希
望tab键是向右形成一个置表位置,这时需要在text edit widget的event()进行处理。
2.2 event filter(事件过滤器)方法
一个对象的event filter的处理优先级在 该对象的 event()方法调用之前。这时,filter对
象的eventFilter()方法被调用
* 使用QCoreApplication的事件过滤,可以将任何事件在进行具体对象之前,进行处理。但
需要注意,这可能会使得整个事件处理过程变慢。
  • Reimplementing QCoreApplication::notify. This is very powerful, providing complete control; but only one subclass can be active at a time.
  • Installing an event filter on QCoreApplication::instance(). Such an event filter is able to process all events for all widgets, so it’s just as powerful as reimplementing notify(); furthermore, it’s possible to have more than one application-global event filter. Global event filters even see mouse events for disabled widgets.
  • Reimplementing QObject::event() (as QWidget does). If you do this you get Tab key presses, and you get to see the events before any widget-specific event filters.
  • Installing an event filter on the object. Such an event filter gets all the events except Tab and Shift-Tab key presses.
  • Reimplementing paintEvent(), mousePressEvent() and so on. This is the commonest, easiest and least powerful way.
3. 自定义事件
3.1 自定义事件从QEvent派出,该event number必须大于QEvent::User
3.2 使用postEvent和sendEvent方法派遣自定义事件
* postEvent实质是将事件推进事件循环(event loop),马上返回。事件的实际发送可能在稍
后的一个事件处理时钟内。
* 在某种意义上,程序可以直接调用对象的对应事件函数,而绕过postEvent。但存在不足,
a)很多事件处理函数是protected,意味着你不能直接调用;b)这种直接调用将绕过event
filter;Qt的解决方法是sendEvent。比如repaint()实质是调用了sendEvent。
* sendEvent等待事件被发送成功或失败后才返回。
* postEvent的一个好处是多个posted的event可以被压缩成单一的event,从而提高了效率。
* 可以使用QCoreApplication::sendPostedEvents强迫Qt马上进入事件循环(event loop)派
遣事件。
 
4. 事件的传播(propogation)
如果事件在目标对象上得不到处理,事件向上一层进行传播,直到最顶层的widget为止。
如果得到事件的对象,调用了accept(),则事件停止继续传播;如果调用了ignore(),事件向上一级继续传播。
Qt对自定义事件处理函数的默认返回值是accept(),但默认的事件处理函数是ingore()。因此,如果要继续向上传播,调用QWidget的默认处理函数即可。到时为止的话,不必显式调用accept()。
但在event处理函数里,返回true表示accept,返回false表示向上级传播。
* 在closeEvent是个特殊情形,accept表示quit,ignore表示取消,所以最好在closeEvent显式调用accept和ignore。
 
5. tips
5.1 使用event->type得到事件的具体类型,对之进行判断,是否是需要的类型(比如
KeyPress),然后进行相应处理
5.2 使用static_cast<specific event type *>对通用事件转换成特定类型,比如将event转
成QKeyEvent *,然后调用QKeyEvent::key()取得按下什么键。
5.3 使用sendEvent时,参数 event,必须是局部变量,因为sendEvent不负责对event的删除
。而postEvent的event参数,则必须是 new分配的heap对象,post完了后会自动删除。