您的位置:首页 > 编程语言 > Qt开发

QT学习笔记

2015-01-19 09:06 411 查看

QT学习笔记(一)

作者:Kagula

第一个版本:2015-01-07

最后更新日期:2015-09-15

Revision:2

目的:

了解QT基本开发流程。

正文:

准备:

环境:

VS2010SP1、 VS2013Update3、 Win7SP1-64bits

安装:

[1]Qt 5.4.0 for Windows 32-bit (VS 2010, OpenGL, 625 MB) 

[2]Visual Studio Add-in 1.2.4 for Qt5 (156 MB)

可以通过两种IDE(QT Creator和VS2010)来新建QT项目,这里使用VS2010。

QT库的缺省安装路径为“C:\Qt\Qt5.4.0\5.4\msvc2010_opengl”。

例一:QT有两种UI实现方式,这里验证,第一种UI实现方式,传统UI设计!

参考

《【Qt学习笔记】Qt+VS2010的配置》
http://blog.csdn.net/jocyln9026/article/details/8575218
我新建了LearnQT项目,项目为自动生成下面的文件

learnqt.ui

learnqt.h

learnqt.qrc

learnqt.cpp

main.cpp

等五个文件。

双击项目中的learnqt.ui文件,打开设计器,为默认视图添加了

“[Display Widgets]->[QWebView]”控件,

在项目里面(我是Debug模式)添加下面的依赖库

Qt5WebKitWidgetsd.lib

Qt5WebViewd.lib

Qt5WebKitd.lib

重新编译项目后,就跑起来了,这种感觉有点像传统MFC编程。

使用QT插件在Visual Studio中新建项目只支持传统UI设计。

例二:QT的第二种UI实现方式,QML

Step1:

 在QT Creator中新建QT Quick Application,选择Qt Quick Controls 1.3。

Step2:

 参考资料

《How to configure CDB in Qt Creator?》
http://stackoverflow.com/questions/9975949/how-to-configure-cdb-in-qt-creator
设置Debugger,设置好后需要重启QT Creator。

注意这里要选择Standalone Debugging Tools for Windows (WinDbg)从微软官网下载。

现在运行程序,不旦可以调试C++源码,还可以单步跟踪qml源码。

Step3:使用Visual Studio。

 在Visual Studio中使用Qt5插件可以打开QT Creator项目,Rebuild后就可以在Visual Studio中

调试Qt的c++代码,但是在VS中不支持在QML代码中下断点调试。

例三:QML编程入门

参考下面的教程,
http://doc.qt.io/qt-5/qml-tutorial1.html
例四:自定义Skin

自定义Widgets可以使用QT Style Sheet,参考资料[13]下载些已经定义好的Stylesheet感受下什么是Style Sheet功能。

自定义边框和标题可以参考资料[14]QSkinObject项目源代码。

常见问题

Q1 开发QT应用程序用Visual Studio还是QT Creator?

A 取决于“要不要跨平台”? 如果将来要跨平台肯定是QT Creator。

用QT Creator可以用上QML(QT Quick项目),QML类似XAML应该是未来的方向。

Visual Studio功能要比QT Creator健全和稳定,大型软件开发的成功案例相对来说较多,

缺点是只支持Windows平台,Visual Studio的QT插件对QML支持不好,只能采用传统方式来做UI。

Q2 如果添加第三方库的引用

A 打开pro文件(QT Creator工程文件),定义头文件的库文件的搜索路径,

参考片段如下:

win32 {

    DEFINES += BOOST_ALL_DYN_LINK  # for using dynamic libraries

    INCLUDEPATH += D:/SDK/boost_1_55_0

    CONFIG(debug, debug|release) {

    LIBS += -LD:/SDK/boost_1_55_0/stage/lib  \

               -lboost_filesystem-vc100-mt-gd-1_55 \

               -lboost_atomic-vc100-mt-gd-1_55

    } else {

    LIBS += -LD:/SDK/boost_1_55_0/stage/lib  \

               -lboost_filesystem-vc100-mt-1_55 \

               -lboost_atomic-vc100-mt-1_55

    }

}

修改好后,在QT Creator中执行[build]->[Run qmake],再rebuild。

Q3 VS2013(XP兼容)程序,能不能通过VS2010的Remote Debugger进行远程调试?

A 可以,需要以下步骤,可能一些VS2013的特性不能被使用。

Step1:

在VS2013中把项目类型设置为Platform Toolset设置为“Visual Studio 2010(v100)”工具集,

注意:不是“Visual Studio 2013-WinXP”工具集。

Step2:

并把QT设置为使用VS2010程序集(替换为原来的VS2013程序集),

包括要放在远程测试机上的QT的DLL文件,全部要从VS2013版换成VS2010 Debug版。

Step3:

在远程测试机上启动VS2010的Remote Debugger。

Step4:

关闭VS2013,用VS2010重新打开VS2013上开发的QT项目程序,启动VS2010中的远程调试工具即可单步跟踪

远程QT程序。

Q4 如何给应用程序打包

A VS2010 Widgets App最小依赖列表

icudt53.dll

icuin53.dll

icuuc53.dll

Qt5Core.dll

Qt5Gui.dll

Qt5Widgets.dll

\platforms\qwindows.dll(这个文件必须放在platforms目录下)

共七个DLL文件。

其中第七个文件来自“C:\Qt\5.4\msvc2010_opengl\plugins\platforms”路径。

VS2013 Widgets App最小依赖列表可以参考上面以及参考资料[6]。

Q5 没有找到QT WebEngine组件

A 离线安装包可能没有这个组件的,得采用在线安装包安装QT。

  或者

  你没有安装msvc2013组件,注意msvc2010不支持QT Web Engine组件。

Q6 QT54 Widgests App 如何响应按钮点击事件

A Step1:在UI中加入PushButton实例,实例名为pushButton。

  Step2:在当前页面头文件加入下面的代码:  

private slots:
void handleButton();

  Step3:在当前页面构造函数“ui.setupUi...”代码后面加入

  connect(ui.pushButton, SIGNAL(released()), this, SLOT(handleButton()));

  Step4:实现“handleButton()”的代码。

需要为QMessageBox添加头文件引用  #include <qmessagebox>

void testQT54::handleButton()

{
QMessageBox::about(NULL, "About", "About this <font color='red'>application</font>");

}

Q7 If using the pure Qt technology, not QML, how to skin the controls look & feel with some pictures? Is there a sample to show how to do this?

A

use QSS (Qt Style Sheets)

use QStyle

subclass controls and provide custom widget painting

Q8 如何使用QT StyleSheet文件?



例一:

打开
http://tech-artists.org/forum/showthread.php?2359-Release-Qt-dark-orange-stylesheet
网址上的stylesheet文件,把里面的内容复制到[Qt 设计师]->[对象查看器]->[改变样式表...]里去

把stylesheet文件依赖的三个png文件复制到当前QT项目路径下。

保存UI文件,并启动程序,会看到样式已经改变。

例二:使用下面的代码段可以动态加载qss文件改变当前窗口样式。

void testLayout::LoadStyleSheet()

{
//QFile fileDark("D:/QTWorkspace/testLayout/Win32/Debug/styleSheet/darkorange.qss");
QString path = QCoreApplication::applicationDirPath() + "/styleSheet/darkorange.qss";
QFile fileDark(path);
fileDark.open(QFile::ReadOnly);
setStyleSheet(fileDark.readAll());

}

Q9 如何MessageBox方式跟踪代码?

A 参考下面代码段

QMessageBox::information(NULL, "Title", "Content");

 QString("Row%1, Column%2")
.arg(index.row() + 1)
.arg(index.column() + 1)

Q10 QString转wchar_t*  ?

QString s = "Hello";

wchar_t* ch = new wchar_t[s.length()];

s.toWCharArray(ch);

如果在Windows下可以采用下面的方式转

(wchar_t*)text.toUtf16()

Q11 让QToolBar工具栏的按钮既显示图标又显示文字.

m_pToolBar = new QToolBar(this);

m_pToolBar->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

Q12 如何动态加widget
QTextEdit *te = new QTextEdit();
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(te);
ui->tab_2->setLayout(mainLayout);
ui->tab_2->layout();

注意,一定要先有个layout,widget只能加到layout上面。

Q13 Set start value for AUTOINCREMENT in SQLite

    Explicitly insert the value-1 into the table, then delete the row.

Q14 Widgets MV模式下如何更新视图

参考在QAbstractTableModel的derived类中

使用beginResetModel();endResetModel()两个方法。

Q15 编辑框控件的输入输出例子

void testQtApp::handleButton()
{
//取第一个"Line Edit"控件。
QString qsText = ui.lineEdit->text();

//QString类型转std::string类型。
//std::string strText = qsText.toLocal8Bit().toStdString();

//从第二个“Line Edit”控件中取出值。
//转整型失败的话,会返回0。
int nText = ui.lineEdit_2->text().toInt()*10;

//把信息显示在第二个“Line Edit”控件。
QString qsShow = QString("title=%1,value=%2").arg(qsText).arg(nText);
ui.lineEdit_2->setText(qsShow);

//QMessageBox::about(NULL, "About", QString("title=%1,value=%2").arg(qsText).arg(nText));
//QMessageBox::about(NULL, "About", "About this <font color='red'>application</font>");
}


备注

[1]QT5.4自带的QWebView控件,加载远程HTML5页面的速度

比cef_binary_3.1916.1857_windows32框架慢很多。

下面是我用的HTML5测试页面地址。
http://chrome.360.cn/test/html5/index.html
QT5.4新的WebEngine组件,需要QT-vs2013,而且对中文支持不好。

[2]Qt Quick 2.0中QQuickView代替了1.0中的QDeclarativeView。

[3]页面切换可以用QTStackedWidget,在UI Designer中为[Containers]->[Stacked Widget]。

在C++里使用下面的代码段,将会切换到第二个页面。

这个函数的入口参数,以1为基准值,所以the parameter is 2代表第2页。

ui->stackedWidget->setCurrentIndex(2);

还可以使用below URL中的代码动态插入widget。
http://doc.qt.io/qt-5/qstackedwidget.html
[4]如何运行“参考资料[14]QSkinObject项目”,

导入到VS2013后,有“skin”,“SkinConfig”,“SkinTest”三个项目,

把这三个项目的Platform Toolset改为Visual Studio 2010(vs100)。

[QT Project Settings]->[Version]改为使用VS2010OpenGL的QT版本。

skin项目添加“C:\Qt\5.4\msvc2010_opengl\include\QtWidgets\5.4.0\QtWidgets”搜索路径。

注释“QApplication::syncX();”语句,在Windows系统下没有用到WindowsX系统。

找到下面这条statement

QColor(s.value("Hintergrundfarbe", Qt::white).toString());

改为如下statement:

QColor(s.value("Hintergrundfarbe", QColor(255, 255, 255)).toString());

把SkinTest项目设为主项目后,启动就能看到效果

[5]Qt4: How to get the Common Application Data folder path?

QSettings register("HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", QSettings::NativeFormat);

QString allUserDesktop( register.value("Common Desktop").toString());

[6]一个UI文件的实例,如下,只有最近一次绑定有效

Ui_ProductInfoForm *_formProductInfo;

......

_formProductInfo->setupUi(widgetUnfinish);

//下面这段代码_formProductInfo取消了同widgetUnfinish的绑定,改而绑定到widgetFinish。

_formProductInfo->setupUi(widgetFinish);

[7]QLabel使用

因为setText()不会立即调用paintEvent进行绘图。程序会等你退出循环后才有机会进行绘图。所以你看到的是最后一次读回来的值。

解决的办法是:

label->setText()后,调用label->repaint()立即重绘。

参考资料

[1]《Qt Creator, linked boost library and debug》
http://qt-project.org/forums/viewthread/5994
[2]《Building QtWebEngine on Windows.》
http://qt-project.org/wiki/QtWebEngineOnWindows
[3]《Porting from Qt WebKit to Qt WebEngine》
http://qt-project.org/wiki/QtWebEnginePortingFromQtWebKit#4fa3243e1dc8c0d1e849ba3b3bb4f280
[4]《How to create smooth rounded corners on a popup widget in Qt》
http://stackoverflow.com/questions/24612576/how-to-create-smooth-rounded-corners-on-a-popup-widget-in-qt
[5]《Qml Grid, Column, and RowLayout does not work with LayoutMirroring》
http://stackoverflow.com/questions/18376356/qml-grid-column-and-rowlayout-does-not-work-with-layoutmirroring
[6]《Deploying Qt or how to avoid “could not find or load the Qt platform plugin”》
http://www.tripleboot.org/?p=138
[7]《Deploy windows created application on Mac》
http://qt-project.org/forums/viewthread/21608
[8]《Using Layouts in Qt Designer》
http://qt-project.org/doc/qt-4.8/designer-layouts.html
[9]《Using a Designer UI File in Your Application》
http://qt-project.org/doc/qt-4.8/designer-using-a-ui-file.html
[10]《Qt自定义窗口部件》
http://qimo601.iteye.com/blog/1407257
[11]《Qt Style Sheets》
http://qt-project.org/doc/qt-4.8/stylesheet.html
[12]《Download Qt Style Sheet files》
http://qt-project.org/forums/viewthread/29370
[13]《qtstyleplugins下载地址》
https://qt.gitorious.org/qt/qtstyleplugins/source/8ff2ac6035fb1d01f5c0054ba14afb949410e3a7: https://github.com/qtproject/qtstyleplugins
[14]《QSkinObject》
http://sourceforge.net/projects/qskinwindows/
[15]《Signals & Slots》
http://qt-project.org/doc/qt-4.7/signalsandslots.html
[16]《Qt5中文乱码解决办法》
http://bbs.csdn.net/topics/390389194
[17]《Qt标准对话框之QMessageBox》
http://devbean.blog.51cto.com/448512/217694
[18]《how can I have more than a UI for a QMainWindow?》
http://stackoverflow.com/questions/861951/how-can-i-have-more-than-a-ui-for-a-qmainwindow
[19]《Problem with switching UI files》
http://qt-project.org/forums/viewthread/31191
[20]《Model/View Tutorial》
http://qt-project.org/doc/qt-4.8/modelview.html
[21]《QT Style Sheet参考》
http://thesmithfam.org/blog/?s=stylesheet
[22]《Help on QDialog Position》
http://qt-project.org/forums/viewthread/8959
[23]《Need save/restore QMainWindow minimized state》
http://qt-project.org/forums/viewthread/11653
[24]《save and restore window / dialog positions》
http://qt-project.org/forums/viewthread/8478
[25]《Qt Style Sheets Examples》
http://doc.qt.io/qt-5/stylesheet-examples.html
[26]《Qt/C++: Icons not showing up when program is run》
http://stackoverflow.com/questions/4114418/qt-c-icons-not-showing-up-when-program-is-run
[27]《Qt Creator中Tabwidget控件使用方法》
http://blog.sina.com.cn/s/blog_4c8287230100kcug.html
[28]《Qt Xlsx》
http://qtxlsx.debao.me/
[29]《QT笔记:数据库总结(一)》
http://blog.csdn.net/reborntercel/article/details/6991147
[30]《Qt: how to update model?》
http://stackoverflow.com/questions/7864313/qt-how-to-update-model
{beginResetModel();...endResetModel();}

beginInsertRows( QModelIndex(), m_points.count(), m_points.count() );

m_points.insert( m_points.count( ), pointItem );

endInsertRows()

[31]《Creating QModelIndex from QTableView row number》
http://forum.qt.io/topic/41208/solved-creating-qmodelindex-from-qtableview-row-number/2
辅助工具下载

[1]《QSS Editor》
http://sourceforge.net/projects/qsseditor/?source=typ_redirect
[2]DB Browser for SQLite
http://sourceforge.net/projects/sqlitebrowser/?source=typ_redirect
[3]Sqlite插件 for Visual Studio
https://visualstudiogallery.msdn.microsoft.com/0e313dfd-be80-4afb-b5e9-6e74d369f7a1/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: