Labels

Thursday, September 29, 2011

How to redirect to online webpage when click a link in Whatsthis rich text

If the text is rich text and the user clicks on a link, the widget also receives a QWhatsThisClickedEvent with the link's reference as QWhatsThisClickedEvent::href().
If a QWhatsThisClickedEvent is handled (i.e. QWidget::event() returns true), the help window remains visible. Call QWhatsThis::hideText() to hide it explicitly.
Reimplement QWidget::event() and catch QEvent::WhatsThisClicked. It can be done more or less in this way:

bool CSDQueuesWindow::eventFilter(QObject *obj, QEvent *ev)
{
  if(ev->type()==QEvent::WhatsThisClicked)
  {
   QWhatsThisClickedEvent* whatsclicked = static_cast<QWhatsThisClickedEvent*>(ev);
   QDesktopServices::openUrl(QUrl(whatsclicked->href())); // redirect to the linked bomgar webpages
   return true;
  }
  return QWidget::eventFilter(obj,ev); // call base class!
 
}

QAction class

Qt simplifies the programming of menus and toolbars through its action concept. An action is an item that can be added to any number of menus and toolbars. Creating menus and toolbars in Qt involves these steps:

• Create and set up the actions.

• Create menus and populate them with the actions.

• Create toolbars and populate them with the actions.



In the Spreadsheet application, actions are created in createActions():

void MainWindow::createActions()

{

newAction = new QAction(tr("&New"), this);

newAction->setIcon(QIcon(":/images/new.png"));

newAction->setShortcut(tr("Ctrl+N"));

newAction->setStatusTip(tr("Create a new spreadsheet file"));

connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));

}

The New action has an accelerator (New), a parent (the main window), an

icon (new.png), a shortcut key (Ctrl+N), and a status tip.We connect the action’s

triggered() signal to the main window’s private newFile() slot, which we will

implement in the next section. This connection ensures that when the user

chooses the File|New menu item, clicks the New toolbar button, or presses Ctrl+N,

the newFile() slot is called.

Monday, September 26, 2011

what's the difference between executing a program by simply typing it's name, vs. executing a program by typing a . (dot) ?

source, . (dot command)

This command, when invoked from the command-line, executes a script. Within a script, a source file-name loads the file file-name. Sourcing a file (dot-command) imports code into the script, appending to the script (same effect as the #include directive in a C program). The net result is the same as if the "sourced" lines of code were physically present in the body of the script. This is useful in situations when multiple scripts use a common data file or function library.
Running "runme" will create a new process which will go on its merry little way and not affect your shell.
Running ". runme" will allow the script "runme" to change your environment variables, change directories, and all sorts of other things that you might want it to do for you. It can only do this because it's being interpreted by the shell process that's already running for you. As a consequence, if you're running bash as your login shell, you can only use the "." notation with a bash script, not (for example) a binary on C shell script.

Friday, September 23, 2011

Design问题的学习总结

转:http://siyangxie.wordpress.com/2010/02/05/design%e9%97%ae%e9%a2%98%e7%9a%84%e5%ad%a6%e4%b9%a0%e6%80%bb%e7%bb%93/

Design问题的学习总结

1. 系统设计步骤:
首先是需求分析。这包括,理解系统是要做什么,有哪些用户,用户希望系统提供什么服务/功能。这些是最基本的,进一步的,可以分析一下系统的规模及相应对性能的要求,输入输出是什么(或者说怎么跟用户交互)。
其次就是分解系统为若干子系统,并且找系统中出涉及到的各个对象。在甄别对象的时候,需要牢记,对象要封装用一组共同的数据和特性。然后就是分析思考各个对象,对内应该封装哪些状态和性质,对外提供什么接口和行为。与此同时要考虑各个对象之间的关系和interaction,把每个对象拟人化。关系有composition, association, aggregation等等,在设计对象关系的时候,要多注意考虑code reuse和future extention. Interaction是麻烦的地方,因为一个interaction中,涉及到两个或者多个的对象,很难分清楚,到底谁是哪个对象提供的这个行为:"file.print(printer) or printer.print(file)?” 怎么办?一个是从语义行为的常理上分析,谁应该提供这个行为,一个是从软件设计的原则出发,例如力求避免紧耦合而使用一个manager.print(file, printer),我也觉得这个是很难说清楚的,只有靠多分析了。
接下来就是考虑有哪些可能变化的东西(现在或者未来),尽量把会变化的部分分离出来加以封装,并且时刻考虑,如何使得design更flexible,如何在未来添加新的code或者feature而不需要改动现在的code。这个时候,可以开始回忆熟悉的设计模式,考察有哪些设计模式可以应用,常用到的如decorator, bridge, strategy, factory, singleton, state, observer等等。
现在把类的框架搭好后,开始考虑动态的关系,即sequence diagram。我个人感觉就是系统提供的服务流程的描述。这里一个困扰我很久的问题是,如何把人机交互的GUI部分,跟GUI背后的各个类的工作,分离开设计。我现在就只能先假设不考虑具体的系统是怎么通过人机交互来提供服务的。
接下来就是考虑实现。一个重要且经常遇到的问题是,怎么管理数据和信息,信息本身可以会涉及到一个或者多个对象类。普遍的,系统都会提供信息搜索查询的服务,那么是用数据库?还是普通的数据结构(数组,map等)?哪些信息应该交给对象自己保存而不是让manager来保存?例如,在停车场设计的题目中,需要保存下“某Customer的车子Car停在位子Space上”的信息,这时候也要考虑,是否应该在space obj中记录停车的客户信息,是否应该在customer object中记录车位的信息?这时,如果考虑到,customer的代码,可能会被用到别的application中,因此不应该在customer对象中保存车位的信息。
2. 设计原则:
基本的OOD概念:封装,抽象,继承,多态
OOD五大原则:OCP(open closed), LSP(substitution), ISP(interface segregation), DIP(dependency inversion), SRP(single responsibility)
GOF设计三大原则:Program to interface; Favor composition over inheritance; Encapsulate what varies;
总体的目标是:promote flexibility and weak coupling; easy to maintain, easy to extend;

OO design--parking lot

转: http://www.mitbbs.com/article_t1/JobHunting/31956527_0_2.html
这种问题都起始于OO Design,那些Object,怎么关系啥的。帮人帮到底,送佛送到西,
讲讲通常情况下,这种design问题的答题方法(对于多数面试官适用,当然也有例外)



这种问题首先明确要干啥,也就是需求分析,不要上来就开搞。比如parking lot,设计
是parking lot的管理?还是charge system?还是啥其他相关问题。这个很重要,决定
了你的设计。

其次,要求几个具体user case。比如,parking lot,可能要求用户能walk-in and pa
rk;要求用户能提前预订;能cancel reservation。user caess也决定了你的design,
也体现了你良好的沟通能力。

然后就是Logical Model或者physical model的design。需要啥Objects(比如 Garage,
Lot, Customer, Car)。

然后他们的关系,比如Garage to Lot (1 - m)。在兽一下OO的继承关系(比如car有tru
ck, sedan, SUV啥的;Lot有大lot,小lot,残帕啥的)

然后根据user case,说明如何用你的model实现。比如reserve,从garage (比如singl
eton)找合适的 Lots,然后根据时间段,看能不能reserve。能的话,系统状态如何up
date;不能得话,如何给customer feedback。

这个可以延伸到很多问题,比如可以考察synchronization,如果只有一个lot,几个用
户同同时request,如何保障正确的行为,如何保障公平,等等。

也可以考算法,根据你的设计,随意挑一个component,比如选择合适的lot,如何高效
优质的实现你的算法。比如你可以用List一个一个找;也可以用其他数据结构,提高算
法质量。

也可以考Coding,从你的任意描述,抽象一个方法,让你写Code。比如如何实现方法Re
verse。定义API,写code。

也可以考business sense,比如在资源有限情况下,如何取舍(功能上,用户体验上,
等等)。

基本上,这种Open Question,想考啥,都可以考。所以你应该从基本OO Design开始,
把面试管引导到你擅长的方向。比如,你熟悉多线程,就多强调一下如何做同步控制;
如果你Design Pattern比较熟,就多丢一丢,比如Garage可以是个Singleton Pattern,
啥的。当然,要是你碰着傻X的面试官,一根经的非要你按照他的想法去设计,那就是你
的rp问题了。

Wednesday, September 14, 2011

ipad/iphone开发入门

Tutorials:



The tool you use to create Mac OS X applications is Xcode—Apple’s IDE (integrated development environment). “Creating Your Project” shows you how to create a new project and describes how an application launches.

Whereas Xcode is the application you use to create and manage the project, Cocoa is the name given to the collection of APIs that you use to develop programs for Mac OS X. You should typically use the highest level of abstraction available to accomplish whatever task you want to perform. To create a desktop application, you should use the Objective-C-based frameworks that provide the infrastructure you need to implement graphical, event-driven applications.

To use the Objective-C frameworks effectively, you need to follow a number of conventions and design patterns. Following the Model-View-Controller (or “MVC”) design pattern, the application uses a controller object to mediate between the user interface (view objects) and the underlying representation of a track (a model object).

·        “Understanding Fundamental Design Patterns” provides an overview of the design patterns you’ll use.

·        “Adding a View Controller” shows you how to customize a view controller class and create an instance of it.

You design the user interface graphically, using Xcode, rather than by writing code. The interface objects are stored in an archive called a nib file.



开发免费,到developer.apple.com请个账户就可以了.但只能在simulator上测试,搞到iphone/ipad上就要花$99了,另外两种方法:
1.
找一个有$99账户的人,让他加你team member
2. jailbreak

developer.apple.com
videosample codes是最快入门的.要说书吗,我推荐两本:
stephen kochan: programming in objective-c 2.0
erica sadun: cookbook



Thursday, September 8, 2011

Finding a duplicated integer in a given array, O(n) time and O(1) space

Finding a duplicated integer. Given a read-only array of n integers between
1 and n-1, design an O(n) time algorithm to find a duplicated integer. Use
only O(1) space. Hint: equivalent to finding a loop in a singly linked
structure.

http://en.wikipedia.org/wiki/Floyd%27s_cycle-finding_algorithm#Tortoise_and_hare


http://www.mitbbs.com/article_t1/JobHunting/31942529_0_1.html
IANGLE PUZZLE
By starting at the top of the triangle and moving to adjacent numbers on the
row below, the maximum total from top to bottom is 27..
5

9 6

4 6 8

0 7 1 5

I.e. 5 + 9 + 6 + 7 = 27.
Write a program in a language of your choice to find the maximum total from
top to bottom in triangle.txt, a text file containing a triangle with 100
rows.
download traiangle.txt in http://www.yodle.com/downloads/puzzles/triangle.txt

who can fix this problem? It is very difficult. Please post your answer

Project Euler Problem 18 & 67

work bottom-up, greedy

从倒数第二行开始。每个数字和下一行的两个数字相加。选较大的值存。然后再上一行
5
9 6
4 6 8
0 7 1 5

倒数第二行的4和下一行它能reach的两个值0和7相加。取较大的4+7=11。
倒数第二行的6和下一行它能reach的两个值7和1相加。取较大的6+7=13。

三角形变为

5
9 6
11 13 13

再变为

5
22 19

再变为

27

最大值

Model/View Programming

MVC(model-view-controller): The Model is the application object, the View is its screen presentation, and the Controller defines the way the user interface reacts to user input.

Model/View: View and the controller objects are combined to become a simpler framework. This separation makes it possible to display the same data in several different views without changing the underlying data structures. To allow flexible handling of user input, we introduce the delegate which allows the way items of data are rendered and edited to be customized.

1.    The model/view architecture


 
The model communicates with a source of data, providing an interface for the other components in the architecture. The nature of the communication depends on the type of data source, and the way the model is implemented.
The view obtains model indexes from the model; these are references to items of data. By supplying model indexes to the model, the view can retrieve items of data from the data source.
In standard views, a delegate renders the items of data. When an item is edited, the delegate communicates with the model directly using model indexes.

Models, views, and delegates communicate with each other using signals and slots:

  •  Signals from the model inform the view about changes to the data held by the data source.
  • Signals from the view provide information about the user's interaction with the items being displayed.
  • Signals from the delegate are used during editing to tell the model and view about the state of the editor.





Wednesday, September 7, 2011

The syntax rules

1.    The syntax rules were:


  • XML documents must have a root element
  • XML elements must have a closing tag
  • XML tags are case sensitive
  • XML elements must be properly nested
  • XML attribute values must be quoted. Metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.





XML Attributes for Metadata

Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements in much the same way as the id attribute in HTML. This example demonstrates this:

<messages>
<note id="501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note id="502">
<to>Jani</to>
<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not</body>
</note>
< /messages>

The id attributes above are for identifying the different notes. It is not a part of the note itself.

Note: metadata (data about data) should be stored as attributes, and the data itself should be stored as elements.



2.    XML Parser


An XML parser converts an XML document into an XML DOM object - which can then be manipulated with a JavaScript.



3.    XML DOM


A DOM (Document Object Model) defines a standard way for accessing and manipulating documents.

The XML DOM

 The XML DOM defines a standard way for accessing and manipulating XML documents.  

The XML DOM views an XML document as a tree-structure.

 All elements can be accessed through the DOM tree. Their content (text and attributes) can be modified or deleted, and new elements can be created. The elements, their text, and their attributes are all known as nodes.

4.    XML Encoding


XML documents can contain non ASCII characters, like Norwegian æ ø å , or French ê è é. To avoid errors, specify the XML encoding, or save XML files as Unicode.

  • Always use the encoding attribute
  • Use an editor that supports encoding
  • Make sure you know what encoding the editor uses
  • Use the same encoding in your encoding attribute