Handle file drop in QT

File drop is an easy-to-use feature for users, as users can directly drag-and-drop a file from file browser onto your application. People may think it is difficult to implement. But actually, it is rather easy. Here is a fast way to start.

The main idea is to handle these two events, QDropEvent and QDragEnterEvent. The following shows how to handle them in your app.

First, add the following code to the class declaration in a header file,

protected:
    void dropEvent(QDropEvent *event);
    void dragEnterEvent(QDragEnterEvent *event);

Then add the implementation to the cpp file,

void CLASS::dragEnterEvent(QDragEnterEvent *event)
{
  if (event && event->mimeData()) {
    const QMimeData* md = event->mimeData();
    if (md->hasUrls())
        event->acceptProposedAction();
  }
}

and

void CLASS::dropEvent(QDropEvent *event)
{
    const QMimeData *data = event->mimeData();
    if(data->hasUrls())
      foreach(QUrl url, data->urls()) {
        QFileInfo info(url.toLocalFile());
            if(info.exists() && info.isFile())
          ///// do something with url.tolocalFile()
      }
}

One more thing, add the following code in the constructor of the class to enable the widget to accept the events.

setAcceptDrops(true);

That’s all!

Tags:

Comments(0)

Open terminal in Nautilus/Explorer/Finder and vice versa

Nautilus, Explorer and Finder are GUI file browsers of Ubuntu, Windows, and Mac OS respectively. No matter under which system, it would be extremely convenient if users can open a terminal with path being the same as location of the file browsers or vice versa. Here I show the ways of doing such under each system.

Ubuntu
Open terminal in Nautilus:
just install nautilus-open-terminal with your Synaptic Package Manager, then you can find a “open in terminal” item in your context menu of your Nautilus.

Open Nautilus in terminal

nautilus . 

Windows
Open cmd line in Explorer
install cmdhere tool in the collections of powertoys provided by Microsoft

Open Explorer in terminal

start .

Mac OS
Open terminal in Finder
Download a simple cdto … app here and drag it onto your Finder toolbar. Clicking it will open a terminal with the current path in Finder.

Open Finder in terminal

open .

Tags: , ,

Comments(0)

Create socks proxy with ssh under Linux

If you have a server supporting ssh, you can easily create a socks proxy with ssh under linux, using the following commnd

$ssh -D portnumber username@ip-address-of-ssh-server

Then you got a socks proxy, localhost:portnumber. Just enjoy it in your browser or somewhere else.

Keep the terminal alive until u don’t need the proxy anymore.

Tags:

Comments(0)

how to use cout, cin, cerr in QT

Include

#include <QTextStream>
#include <stdio.h>

Create these instances globally

QTextStream cin(stdin, QIODevice::ReadOnly);
QTextStream cout(stdout, QIODevice::WriteOnly);
QTextStream cerr(stderr, QIODevice::WriteOnly);

Then you can use

cout<<QString("bla bla")<<endl

as you wish.

refer: http://lists.trolltech.com/qt4-preview-feedback/2005-03/thread00012-0.html

Tags:

Comments(0)

How to use OpenCV in QT under ubuntu

Install OpenCV with Synaptic Package Manager
Add one line in QT project file (*.pro)

LIBS += -L/usr/local/lib -lcxcore -lcv -lhighgui -lcvaux

Then, include OpenCV header file in your source code.
Do whatever you like in your source file.
Compile.

Note: This post is incomplete so far and subject to change.

Tags: , ,

Comments(0)

Duplicate Finder Window

Open a new Finder Window with current Folder

  1. Open ScriptEditor.app
  2. Paste the following script
  3. Save as application (Name it with “duplicateFinder.app”)
  4. drag the app onto the toolbar of Finder
  5. click it whenever you want to duplicate current Finder window
tell application "Finder"
get the exists of the front Finder window
if the (exists of the front Finder window) is true then
try
set newWindow to target of front window
set oldView to current view of front window
make new Finder window to newWindow
set current view of front window to oldView
end try
else
try
make new Finder window to alias ":"
set the current view of the front Finder window to column view
end try
end if
end tell

From: http://www.fosk.it/how-to-open-a-new-finder-window-from-current-location.html

Tags:

Comments(0)

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

* Comments(1)

转域名from Dreamhost to Godaddy

dreamhost的租约要到期了, 因为续费很贵, 所以准备转出来了.已经在淘宝上买了个国内的主机,很便宜,29块钱一年, 虽然只有100M, 流量2G, 但它胜在它有ssh支持. 如果只是放个blog, 流量也够了. 凑合吧. 几天粗略测试了下, 速度稳定都还可以. 那么, 现在就把在dreamhost注册的域名转出, 然后关闭帐户.  关于域名, 听说godaddy的服务好,价格低,就尝试一下吧.

转域名的流程很容易, 可以参考这个地址: http://www.watch-life.net/website-app/transfer-domain-godaddy.html

值得一提的是, dreamhost很爽快, 转出域名所需的secure code就直接在帐户里可以查到. 不需要人工的去沟通. 所以有人需要的话, 我也挺推荐dreamhost的. 顺道说句, 当时我用了dreamhost的coupon注册的, 所以我可以多注册一个域名, 刚才在转的时候, 似乎我可以先免费的续费一年, 然后再转, 这样可以少出一年的注册费了. 但本着做人要厚道的原则, 没必要利用它系统上的漏洞得这么点小利, 况且dreamhost对转出域名那么大方了.

在godaddy上总共的花费是7.12*2美刀. godaddy的转入费用是6.99+20分的ICCAN的注册费. 对于转入的域名, godaddy会给一年的免费, 也就是, 域名的到期时间自动多了一年.

初步试用了下godaddy, 功能还不错, 域名解析管理都挺好的, 而且解析可以使用比较低的TTL. 刚刚发现, godaddy为每个域名还提供了个免费的hosting plan, 支持php, 但要放godaddy的广告的(也听说用某些办法可以去掉). 感兴趣的朋友可以看这篇文章.

Comments(0)