Tag Archives: qt

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);
  }
}

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完了后会自动删除。

QString与printf的%s

qt 4里
QString str("hello");
printf("%s
", str.data()); //只打印出h
//这是由于在qt4里每个char存留unicode,第二字节为零
解决方法
1. printf("%s
", qPrintable(str));
//qPrintable在qt文档里没有出现,但可在其源码里发现
2. printf("%s
", str.toLatin1());
//toLatin1,toUtf8,toLocal8bit返回QByteArray,可以自动转成 char*

compile qt with msvc.net

1. unzip opensource version

//2. run configure with win32-g++ // obselete, a good methods here

3. cvs_qtmsvc_bcc.zip should merge with qt4msvc_bcc.zip, then overwrote by files from mkspecs-from-evaluation-version

4. set qmakespec to win32-msvc.net
4.1 rename the License.cpl to license.TROLL
4.2 copy qmake-from-eval to qmake dir, rename to qmake.exe
4.2 open the makefile under the qmake dir, comments the all: xxx line, and then add a line "all: "
4.3 run configure, it will configure for you.

5. use qmake -recursive to generate makefiles (in general, this step
will be done during the configure process. but, if you run with any
other makespec rather than msvc.net, please do it again. In a clean installation, it’s ok to skip this step)

6. nmake all files, it will cost much time. but, you can stop it at any
time, and then simply run nmake again to continue the process.

qt 4 installed with msvc.net and dev-c++

qt 4 installed with msvc.net and dev-c++

adipose from qt forum has summerized some points in how to compile qt 4 source open version with msvc.net. here is its url

this thread also address some ideas on how to make qt4 work with dev-c++. apart from this thread, there are some as following:
http://sourceforge.net/forum/forum.php?thread_id=1314521&forum_id=48211
http://www.hoppelkoppel.de/docs/InstallationQt.pdf
http://evilissimo-softdev.de/files/stuff/qt-devcpp-template.txt
http://www.c-plusplus.de/forum/viewtopic-var-t-is-114023-….html

hope to help.

compiling Qt 4 with msvc.net (obselete)

see this item for latest method

1. unpack qt-win-opensource-4.0.0.zip to c:qt4.0.0
2. open cmd, then locate to c:qt4.0.0

c:
cd qt4.0.0

3. unpack qt4msvc_bcc.zip to the Qt 4.0.0 directory
4. configure Qt 4 with MinGW. This step is necessary, otherwise it will prompt missing of files when do nmake.

configure -platform win32-g++

5. go to the qmake directory, then use Makefile.win32-msvc to recompile qmake.

cd qmake
nmake -f Makefile.win32-msvc
I
am also wondering if there is a makefile for win32-msvc.net.
Unfortunately, there is not such a file due to absence of qmake
generator file specific to msvc.net. However, we can regard qmake using
win32-msvc option is compatible with msvc.net environment, considering
the successful final nmake link result.

6. go back the root directory of Qt 4. Using qmake with recursive
option to produce makefiles in current directory and all its
subdirectories.

cd ..
qmake -recursive -spec win32-msvc.net

7. nmake Qt4. The process will be longer than you expected. Be patient. 🙂

nmake

编译是变态的慢,要有心理准备哦!我在dell c640/2.0G/512M上花了将近6个小时才编译完成

需要qt4msvc_bcc.zip的请留email给我.
If you need qt4msvc_bcc.zip, please leave me a message, or drop me an
Email. You can also download this file from Kde-cygwin maillist. Thank
Andreas Hausladen for sending this zip file to the public maillist.

p.s. It will be better to use makespec files got from the win
evaluation Qt 4 version. Just override the counterparts in
qt4msv_bcc.zip. I have done as this, and comipled successfully.

qt 4推出在windows平台下的GPL版

可惜不支持ms VC++或VC.net,网上已经有一些讨论,看是否将qt opensource版在
MsVc.net环境下编译过去,目前来看,好象有些问题,比如trolltech给出opensourc里
的qmake可能跟商业版的qmake不尽相同。现在没有时间整这些,试目以待。
在这个邮件列表里可以搜索到一些 关于此话题的技术讨论
http://lists.kde.org/?l=kde-cygwin&r=1&w=2
在http://www.qtforum.org/board.php?boardid=45j和
http://www.osnews.com/comment.php?news_id=9675&limit=no里也有一些讨论,很是
热闹!