Qt 知识的总结

来源:互联网 发布:线切割绘图编程步骤 编辑:程序博客网 时间:2024/06/05 07:17
0. 有用的帮助文档
* Qt Designer Manual
    * A Quick Start to Qt Designer
* Qt Core
    * The Meta-Object System
    * Object Model
    * Signals & Slots
    * The Event System
        *  QCoreApplication::notify()
        *  QCoreApplication::exec()



1. QMessageBox cause program quit
看帮助文档quitOnLastWindowClosed, 默认情况是,在最后一个“visiable”的窗口关闭时,程序会退出。
http://stackoverflow.com/questions/5116459/problem-with-hidden-qmainwindow-application-crashes-after-qmessagebox-is-displa

2. Useful manual page


3. QT中让鼠标光标定位到某一文本框闪烁
   ui->lineEdit->setFocus();

4. 对话框
模态对话框, 非模态对话框
用show()显示非模态对话框,此时可以操作它的父窗体。
用exec()显示模态对话框, 此时只可以操作该窗体

见帮助文档 modal dialogs

5. 布局管理
可以嵌套

6. QT窗体显示在屏幕中间位置
QDesktopWidget *deskdop = QApplication::desktop();
move((deskdop->width() - this->width())/2, (deskdop->height() - this->height())/2);

7. setQuitOnLastWindowClosed(false) 导致程序不能正常退出
此时应该重写virtual函数 ‘event’, 自己处理 close event
bool Login::event(QEvent * e)
{
    if (e->type() == QEvent::Close)
    {
        qApp->quit();
        return true;
    }
    else
    {

        return QDialog::event(e); // 父类的event
    }

}


8. 关于tr
http://blog.csdn.net/tju355/article/details/7253133
在论坛中漂,经常遇到有人遇到tr相关的问题。用tr的有两类人:

    (1)因为发现中文老出问题,然后搜索,发现很多人用tr,于是他也开始用tr
    (2)另一类人,确实是出于国际化的需要,将需要在界面上显示的文件都用tr包起来,这有分两种:
        (2a) 用tr包住英文(最最推荐的用法,源码英文,然后提供英文到其他语言的翻译包)
        (2b) 用tr包住中文(源码用中文,然后提供中文到其他语言的翻译包)

注意哦,如果你正在用tr包裹中文字符,却不属于(2b),那么,这是个信号:

    你在误用tr
    你需要的是QString,而不是tr


9. tr 例子程序, 见自带例子hellotr
非常吊,在程序中使用英文, 然后提供一个字典,就可以转换成其他语言。但是性能会下降。

10. 窗口 禁止改动大小
setFixedSize


11. 一个界面中有多个layout, 指定parent的问题:
layout在创建时,一般不用parent,在使用setLayout,addLayout 时,会自动设置parent
http://blog.csdn.net/qq575787460/article/details/7824633
见 C++ programming with QT gui chapter2


12. 让窗口选择合适大小
sizeHint() 函数。
首先,该窗体必须有一个layout,这样sizeHint返回有有效的返回值
setFixedSize(sizeHint().width(), sizeHint().height())


13. 使用命令行编写、编译、运行 helloworld
见C++ programming with QT gui chapter1
写好helloworld.cpp
$ qmake -project   生成平台无关的pro文件
$ qmake xxx.pro    生成平台相关的makefile文件
编译
Linux: $ make
windows: $ nmake ..


14. QMessageBox 修改button的text
http://www.qtcentre.org/threads/28739-QMessageBox-Text-in-the-buttons


15. ui文件
ui文件其实就是一个xml文件,记录了窗口的信息,窗口类的名字,有什么控件,控件位置等等
uic 会将ui文件编译成 .h 文件。
然后你可以使用这个.h文件。
例子见 见C++ programming with QT gui chapter2


16. ig4icd32.dll qtcreator
在windows上安装qtcreator,运行时出错,说有未处理的异常。
解决方法:在命令行启动qtcreator
qtcreator -noload Welcome

参考: https://forum.qt.io/topic/55765/qt-creator-on-windows-10-solved/6


17. 给程序添加配置文件

http://stackoverflow.com/questions/4021563/does-qt-have-a-way-of-storing-configuration-settings-in-a-file


18. During startup program exited with code 0xc0000135.

原因:未找到相应的DLL

解决方法:把DLL拷贝到debug目录,或者设置PATH、LD_LIBRARY_PATH


19. 平台相关的宏

Q_OS_WIN

Q_OS_UNIX

也可以使用WIN32


20. 如何发布一个Qt程序

https://lemirep.wordpress.com/2013/06/01/deploying-qt-applications-on-linux-and-windows-3/

静态编译,动态连接


21. 调试信息的输出,发布时去掉调试信息 (QT_NO_DEBUG_OUTPUT)

http://stackoverflow.com/questions/3886105/how-to-print-to-console-when-using-qt

If it is good enough to print to stderr, you can use the following streams originally intended for debugging:

qDebug() << "C++ Style Debug Message";qDebug( "C Style Debug Message" );qWarning() << "C++ Style Warning Message";qWarning( "C Style Warning Message" );qCritical() << "C++ Style Critical Error Message";qCritical( "C Style Critical Error Message" );// qFatal does not have a C++ style method.qFatal( "C Style Fatal Error Message" );

Though as pointed out in the comments, bear in mind qDebug messages are removed ifQT_NO_DEBUG_OUTPUT is defined

If you need stdout you could try something like this (as Kyle Strand has pointed out):

QTextStream& qStdOut(){    static QTextStream ts( stdout );    return ts;}

You could then call as follows:

qStdOut() << "std out!";

0 0