现在的位置: 首页 > 综合 > 正文

QT 知识点 汇总

2013年10月10日 ⁄ 综合 ⁄ 共 4759字 ⁄ 字号 评论关闭
QT 窗口居中显示技巧

//需要引用下面这个
#include <QDesktopWidget>

//.......在需要居中的窗口构造函数中加入下面代码
QDesktopWidget* desktop = QApplication::desktop(); // =qApp->desktop();也可以
move((desktop->width() - this->width())/2,
     (desktop->height() - this->height())/2);
//.......

//编译运行,看效果吧

 

http://www.oschina.net/code/list_releted_codes?id=3889&p=1

QT 设置中文文字的时候出现乱码

view source
print?
01 #include <QtGui/QApplication>
02 #include <QTextCodec>
03 #include "mainwindow.h"
04  
05 int main(int argc, char *argv[])
06 {
07  
08    QApplication a(argc, argv);
09    // 以下部分解决中文乱码
10    QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
11    QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
12    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
13    // 以上部分解决中文乱码
14    MainWindow w;
15  
16    w.show();
17  
18    return a.exec();
19 }

或者------------------
这个方法对于已经在汉语字符串周围使用tr()宏的人来说比较有效。按照说明的方法定义一个宏,将其放到全局可见的地方,然后把所有的tr替换成宏的名字即可
标签: 乱码 QT
代码片段(1)
[代码] cpp代码
01 /*在一个汉字字符串可见的地方声明一个QTextCodeC指针,例如某个对话框要显示汉字,那么可以将其声明为该对话框的成员变量*/
02 QTextCodec *gbCode;
03  
04 /*对话框的构造函数中*/
05 #define Mtr(s) (gbkCode->toUnicode(s))
06 AppDialog::AppDialog(QWidget *parent)
07    : QDialog(parent), ui(new Ui::Dialog)
08 {
09    gbkCode=QTextCodec::codecForName("GB2312");//你完全可以改成GB18080等编码
10    ui->setupUi(this);
11    createTray();
12    this->setWindowTitle(Mtr("Linkapp v1.0"));
13    /**其他代码***/
14 }
15 /*代码中的使用*/
16 void AppDialog::disconnect()
17 {
18    if(connected){
19        linkThread->stop();
20        connected=false;
21    }
22    linkButtonState=false;
23    trayIcon->setIcon(QIcon(":/images/form-icon.png"));
24    ui->link_unlink->setText(Mtr("连接")); //使用例子1
25    trayIcon->setToolTip(QString(Mtr("未连接")));//使用例子2
26 }

透明按鈕

1 QPushButton *bt =new QPushButton(this);
2 bt->setText("ok");
3 bt->move(200,100);
4 bt->setFlat(true);//就是这句能够实现透明,真是意外的发现,希望对一些学习的朋友有点帮助

QT 播放flash
] cpp代码
view source
print?
01 #include <QtGui>
02 #include <QAxWidget>
03  
04 /**
05 * 此方法仅限于Windows
06 */
07 int main(int argc, char *argv[])
08 {
09    QApplication a(argc, argv);
10  
11    QAxWidget *flash = new QAxWidget(0,0);
12    flash->resize(1000,700);
13    flash->setControl(QString::fromUtf8("{d27cdb6e-ae6d-11cf-96b8-444553540000}"));
14    flash->dynamicCall("LoadMovie(long,string)",0,"G:/e.swf");
15    flash->show();
16    
17    return a.exec();
18 }
19 //需要在pro里加 CONFIG+=qaxcontainer
 Widget背景圖片
1 //在Qt3中,使用QWidget::setBackgroundPixmap可以很容易地为窗口添加背景图片,例如:
2  
3 widget->setBackgroundPixmap(pixmap);
4  
5 //到了Qt4中,则推荐使用调色板(palette)来代替以上方法,同样十分简单,例如:
6  
7 QPalette palette;
8 palette.setBrush(widget->backgroundRole(), QBrush(pixmap));
9 widget->setPalette(palette);

QT  動態加載dll

#ifdef Q_OS_WIN
02    typedef long ( *SHELLRUN)(long,const char*, const char*, const char* ,const char* ,int );
03    SHELLRUN test;
04    QString str = "shell32.dll";
05    QLibrary lib(str);
06    test = (SHELLRUN)lib.resolve("ShellExecuteA");
07    const char * te = "open";
08    const char * te1 = ".\\help\\Guide.hlp";
09    test(NULL,te,te1,NULL,NULL,5);
10    lib.unload();
11 #endif

Qt 双击时不触发单击事件

为了避免双击时触发单击事件,在单击处理函数clicked()中启动一timer,延时 qApp->doubleClickInterval(),而在此timer的timeout()中处理单击事件,在双击处理函数停止此 timer,一个完整的例子代码如下:(for Qt3,Qt4的也差不多,省略)
标签: 双击 QT
代码片段(1)
[代码] main.cpp
01 #include <qapplication.h>
02 #include <qtimer.h>
03 #include <qlistbox.h>
04  
05 class Test :public QListBox {
06    Q_OBJECT
07  
08 public:
09    Test( QWidget* parent, const char* name = 0 )
10        : QListBox( parent, name ) {
11        connect( this, SIGNAL( clicked( QListBoxItem* ) ),
12                 this, SLOT( seeingSingleClick() ) );
13        connect( this, SIGNAL( doubleClicked( QListBoxItem* ) ),
14                 this, SLOT( handleDoubleClick() ) );
15  
16        _timer = new QTimer( this );
17        connect( _timer, SIGNAL( timeout() ),
18                 this, SLOT( handleSingleClick() ) );
19  
20        insertStringList( QStringList() << "Item 1" << "Item 2"
21                          << "Item 3" << "Item 4" );
22    }
23  
24 protected slots:
25    void seeingSingleClick() {
26        _timer->start( qApp->doubleClickInterval(), true );
27    }
28    void handleSingleClick() {
29        qDebug("This was a single click!");
30    }
31    void handleDoubleClick() {
32        qDebug("This was a double click!");
33        _timer->stop();
34    }
35 private:
36    QTimer* _timer;
37 };
38  
39  
40 int main( int argc, char** argv ) {
41    QApplication app( argc, argv );
42  
43    Test* test = new Test(0);
44    test->show();
45  
46    QObject::connect( qApp, SIGNAL( lastWindowClosed() ),
47                      qApp, SLOT( quit() ) );
48    return app.exec();
49 }
50  
51 #include "main.moc"

Qt中去除QString字符串里面多余的空格

问题:QString str(“a  b c            d e”) =>    QString rstr(“a b c d e”);
用法:QString str(“a b        c”);
str = deBlank(str);
经过转换后str的内容就为"a b c”。
标签: QString
代码片段(1)
[代码] 解决方法
01 void deBlank(QString &strs)
02 {
03    int len = strs.length();
04    for (int i=0;i<len;i++)
05    {
06        if (strs.at(i).isSpace())
07        {
08            strs[i] = QChar(' ');
09        }
10    }
11 }

QT 最小化到托盘 和 关闭到托盘 的例子

以下的最小化到托盘依赖于关闭到托盘,虽然有点不常规,但代码是如此的简明...
 [代码] cpp代码
01 //关闭到托盘---------
02 void Widget::closeEvent(QCloseEvent *e)
03 {
04    e->ignore();
05    this->hide();
06 }
07  
08  
09 //最小化到托盘----
10 void Widget::changeEvent(QEvent *e)
11 {
12    if((e->type()==QEvent::WindowStateChange)&&this->isMinimized())
13    {
14        QTimer::singleShot(100, this, SLOT(close()));
15    }
16 }

抱歉!评论已关闭.