QT编程实用大全(3)

2019-08-29 18:29

3.运行如下命令:

lupdate ttl.pro

生成ttl_zh-cn.ts文件;(PS:.ts的名字来自―翻译源‖(translation source)) 4.运行如下命令:

linguist ttl_zh-cn.ts

这时候会弹出一个图形界面工具: 1)单击左边窗口的QPushButton

2)双击中间窗口的helloworld!这时会弹出一个对话框,在Translation下输入:你好世界! 3)单击工具栏的Done and Next按钮(这个时候QPushButton的前面会变成绿色的对号)显示翻译完成

4)然后File->Release,这个是生成.qm文件(.qm来自―QT消息‖Qt message),保存到当前目录下

也可以使用命令release ttl_zh-cn.ts来生成.qm文件的。

5)点击linguist―X‖退出窗口,这个时候会提示保存ttl_zh-cn.ts文件,单击save,完成操作。 这一步的目的是把―你好世界!‖来替代ttl_zh-cn.ts中的―unfinished‖,这个只要了解就可以了,有兴趣的可以去看看QT参考文档。 5.运行如下命令: qmake ttl.pro

6.运行如下命令: make

7.运行如下命令:

./ttl

这个时候你会发现按钮是显示的是:―你好世界!‖ 而不是―helloworld!‖

PS:lupdate和lrelease命令都可以带参数-verbose,这样会显示一些提示信息。这个 参数是可选的。

通过上面的步骤可以完成正常的翻译,但对象QLineEdit的右键菜单显示的还是英文,解决方法:

把Qt\\translations目录下的qt_zh_CN.ts里面的内容全部拷到自己项目ts文件的后面就可以了(也就是把两个ts文件合并)

============================================================ ==================================================== 4字节空间存INT类型:

#define USERGROUP_WIDTH 5; char buff[5];

int groupid = atoi(groupId.trimmed().toAscii().data()); //得到GroupID的int值 char* gid = (char*)(&groupid); //将groupid转化为char*类型 memcpy(buff, gid, USERGROUP_WIDTH-1); char p[4];

memset(buff, 0, USERGROUP_WIDTH); memcpy(buff, p, USERGROUP_WIDTH-1);

int gid = *((int*)(&buff));

==================================

错误信息:redefinition class… 请核对

#ifndef IGPSINTERACTION_H_INCLUDED #define IGPSINTERACTION_H_INCLUDED

上面两行中的名称是否一样(出现过第两行中最后一个‖D‖没掉, 找了N久才查出问题,汗~~~) 另一原因是变量定义不可放在.h文件中,如下 struct mystruct{

… };

是一个变量 (不可放在.h文件中实现) typedef struct MyStruct{ ….

}mystruct;

其中 MyStruct是一种类型,而mystruct是一个变量 标准用法 在.h文件中

typedef struct MyStruct{ …. };

在.cpp中定义变量 struct MyStruct mystruct;

========================================================= std::string 转QString:

std::string groupName = ?abcdef‘;

const char *groupNameCh = groupName.c_str(); QString tmpStr = QObject::tr(groupNameCh);

================================================= 窗体在执行destory()时,qapp对象就已经退出啦;

=============================================== gsoap项目中的错误:multiple definition of `namespaces‘

解决方法:用gsoap中的工具生成的 nsmap文件(#include ―UMPCServer.nsmap‖)引用不能写在.h中,应该要写在.cpp文件中;

a.cpp:

#include ―UMPCServer.nsmap‖

上面的写法是正确的,不能写在a.h文件中,否则就会报错 ================ 删除TreeWidget结点:

void MainWindow::clearTreeWidget() {

while ( treeWidget->topLevelItemCount() > 0 )

{

QTreeWidgetItem *parentItem = treeWidget->takeTopLevelItem(0); QList list = parentItem->takeChildren ();

for (int j = 0; j < list.size(); j++) {

QTreeWidgetItem *childItem = list.at(j); delete &nodeItemData(childItem); delete childItem; }

delete &nodeItemData(parentItem); delete parentItem; } }

=======================================================

IGPSNestData* resolveRecord(const QSqlRecord &record,const DataType &dateType ) error: expected `,‘ or `…‘ before ?&‘ token 解决方法 #include

=========================================================== GpsSideBar::IGPSNestData* GpsSideBar::resolveRecord(const QSqlRecord &record,const GpsSideBar::DataType &dateType ); {

}

error: declaration of `GpsSideBar::IGPSNestData* GpsSideBar::resolveRecord(const QSqlRecord&, const GpsSideBar::DataType&)‘ outside of class is not definition 解决方法:去掉函数头最后的‖;‖

GpsSideBar::IGPSNestData* GpsSideBar::resolveRecord(const QSqlRecord &record,const GpsSideBar::DataType &dateType ) { }

============================================================ QTreeWidget/QTreeView中的CheckStatus状态的级联更新

void GpsSideBar::on_treeWidget_itemChanged ( QTreeWidgetItem * item, int column ) {

if (!item || column != 0) return;

Qt::CheckState state = item->checkState(0); QTreeWidgetItem *parent = item->parent(); if (parent) {

int number = 0;

int partiallyCheckedNum = 0;

for (int row = 0; row < parent->childCount(); ++row)

{

if (parent->child(row)->checkState(0) == Qt::Checked) ++number;

if (parent->child(row)->checkState(0) == Qt::PartiallyChecked) ++partiallyCheckedNum; }

if (number == 0)

{

if (parent->checkState(0) != Qt::Unchecked && partiallyCheckedNum == 0) parent->setCheckState(0, Qt::Unchecked);

else if (parent->checkState(0) != Qt::PartiallyChecked && partiallyCheckedNum > 0) parent->setCheckState(0, Qt::PartiallyChecked); }

else if (number == parent->childCount()) {

if (parent->checkState(0) != Qt::Checked ) parent->setCheckState(0, Qt::Checked); } else {

if (parent->checkState(0) != Qt::PartiallyChecked ) parent->setCheckState(0, Qt::PartiallyChecked); } }

if (item->childCount() > 0) {

int row;

if (state == Qt::Checked) {

for (row = 0; row < item->childCount(); ++row) {

if (item->child(row)->checkState(0) != Qt::Checked) item->child(row)->setCheckState(0, Qt::Checked); }

}

else if (state == Qt::Unchecked )

{

for (row = 0; row < item->childCount(); ++row) {

if (item->child(row)->checkState(0) != Qt::Unchecked) item->child(row)->setCheckState(0, Qt::Unchecked); } }

} }

==========================================

清空QTreeWidget/QTreeView所有结点(gpssidebar.cpp文件中提取): void GpsSideBar::clearTreeWidget(QTreeWidget *treeWidget) {

while ( treeWidget->topLevelItemCount() > 0 ) {

QTreeWidgetItem *parentItem = treeWidget->takeTopLevelItem(0); QList list = parentItem->takeChildren (); for (int j = 0; j < list.size(); j++) {

QTreeWidgetItem *childItem = list.at(j); delete &GetGPSNestData(childItem); delete childItem; }

delete &GetGPSNestData(parentItem); delete parentItem; } }

========================================================== ini配置文件中的字段名是区分大小写的

========================================================= void MainWindow::contextMenuEvent(QContextMenuEvent *event) {

QMenu menu(this);

menu.addAction(cutAct); menu.addAction(copyAct); menu.addAction(pasteAct); menu.exec(event->globalPos()); }

================================================== 让QLineEdit不弹出右键菜单:

QLineEdit->setContextMenuPolicy(Qt::NoContextMenu); ========================================= 计算坐标两点间的角度: 第一种方法:

double calcAngle(const QPointF& centerPos,const QPoint& pos) {

double px1,px2,py1,py2; px1 = centerPos.x();


QT编程实用大全(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2015-2016学年河南省洛阳市高一下学期期中考试历史(解析版)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: