Qt 编程点滴 初学者必看 (5)

移动开发
本文介绍的是Qt 编程点滴,作为一名新手,我建议必须看一看。编程那些事,只有编程人员自己明白!所以推荐本文。

Qt 编程继续为大家讲解,还是接着文章 Qt 编程点滴 初学者必看 (4) ,继续介绍,说编程那些细节。由于本话题是一节一节为大家介绍的,所以更多内容请看末尾编辑推荐。

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

  1. void GpsSideBar::on_treeWidget_itemChanged ( QTreeWidgetItem * item, int column )  
  2. {  
  3.     if (!item || column != 0)  
  4.         return;  
  5.  
  6.     Qt::CheckState state = item->checkState(0);  
  7.     QTreeWidgetItem *parent = item->parent();  
  8.  
  9.     if (parent)  
  10.     {  
  11.         int number = 0;  
  12.         int partiallyCheckedNum = 0;  
  13.         for (int row = 0; row < parent->childCount(); ++row)  
  14.         {  
  15.             if (parent->child(row)->checkState(0) == Qt::Checked)  
  16.                 ++number;  
  17.             if (parent->child(row)->checkState(0) == Qt::PartiallyChecked)  
  18.                 ++partiallyCheckedNum;  
  19.         }  
  20.         if (number == 0)  
  21.         {  
  22.             if (parent->checkState(0) != Qt::Unchecked && partiallyCheckedNum == 0)  
  23.                 parent->setCheckState(0, Qt::Unchecked);  
  24.             else if (parent->checkState(0) != Qt::PartiallyChecked && partiallyCheckedNum > 0)  
  25.                 parent->setCheckState(0, Qt::PartiallyChecked);  
  26.  
  27.         }  
  28.         else if (number == parent->childCount())  
  29.         {  
  30.             if (parent->checkState(0) != Qt::Checked )  
  31.                 parent->setCheckState(0, Qt::Checked);  
  32.         }  
  33.         else  
  34.         {  
  35.             if (parent->checkState(0) != Qt::PartiallyChecked )  
  36.                 parent->setCheckState(0, Qt::PartiallyChecked);  
  37.         }  
  38.     }  
  39.  
  40.     if (item->childCount() > 0)  
  41.     {  
  42.         int row;  
  43.         if (state == Qt::Checked)  
  44.         {  
  45.             for (row = 0; row < item->childCount(); ++row)  
  46.             {  
  47.                 if (item->child(row)->checkState(0) != Qt::Checked)  
  48.                     item->child(row)->setCheckState(0, Qt::Checked);  
  49.             }  
  50.         }  
  51.         else if (state == Qt::Unchecked )  
  52.         {  
  53.             for (row = 0; row < item->childCount(); ++row)  
  54.             {  
  55.                 if (item->child(row)->checkState(0) != Qt::Unchecked)  
  56.                     item->child(row)->setCheckState(0, Qt::Unchecked);  
  57.             }  
  58.         }  
  59.     }  

清空QtreeWidget/QTreeView所有结点(gpssidebar.cpp文件中提取):

  1. void GpsSideBar::clearTreeWidget(QTreeWidget *treeWidget) {  
  2.     while ( treeWidget->topLevelItemCount() > 0 )  
  3.     {  
  4.         QtreeWidgetItem *parentItem = treeWidget->takeTopLevelItem(0);  
  5.         QList list = parentItem->takeChildren ();  
  6.  
  7.         for (int j = 0; j < list.size(); j++)  
  8.         {  
  9.             QtreeWidgetItem *childItem = list.at(j);  
  10.             delete &GetGPSNestData(childItem);  
  11.             delete childItem;  
  12.         }  
  13.         delete &GetGPSNestData(parentItem);  
  14.         delete parentItem;  
  15.     }  

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

  1. void MainWindow::contextMenuEvent(QContextMenuEvent *event)  
  2. {  
  3.     QMenu menu(this);  
  4.     menu.addAction(cutAct);  
  5.     menu.addAction(copyAct);  
  6.     menu.addAction(pasteAct);  
  7.     menu.exec(event->globalPos());  

 
让QLineEdit不弹出右键菜单:

  1. QLineEdit->setContextMenuPolicy(Qt::NoContextMenu); 

计算坐标两点间的角度,有两种方法。

***种方法:

  1. double calcAngle(const QPointF& centerPos,const QPoint& pos)  
  2. {  
  3.     double px1,px2,py1,py2;  
  4.     px1 = centerPos.x();  
  5.     py1 = centerPos.y();  
  6.     px2 = pos.x();  
  7.     py2 = pos.y();  
  8.     double x = px2 - px1;  
  9.     double y = py2 - py1;  
  10.     double hyp = sqrt(pow(x,2) + pow(y,2));  
  11.     double cos = x / hyp;  
  12.     double rad = acos(cos);  
  13.     double deg = 180/(M_PI / rad);  
  14.     if (y < 0)  
  15.     {  
  16.         deg = -deg;  
  17.     }  
  18.     else if ((y == 0) && (x <0))  
  19.     {  
  20.         deg = 180;  
  21.     }  
  22.     degdeg = deg + 90;  
  23.     if (deg < 0)  
  24.     {  
  25.         degdeg = deg + 360;  
  26.     }  
  27.     return deg;  

第二种方法:

  1. int calcAngle(const double& sx,const double& sy,const double& dx,const double& dy)  
  2. {  
  3.     double x, y, k1, k2;  
  4.     x = dx - sx;  
  5.     y = dy - sy;  
  6.     if ( (x == 0) && (y == 0) )  
  7.     {  
  8.         return 0;  
  9.     }  
  10.   if (x == 0)  
  11.   {  
  12.       if ( y < 0) return 0;////在X轴上时两种结果  
  13.       if ( y > 0) return 180;  
  14.   }  
  15.  
  16.   if ( y == 0)  
  17.   {  
  18.       if ( x > 0 ) return 90;//在Y轴上时两种结果  
  19.       if ( x < 0) return 270;  
  20.   }  
  21.   k1 = 0; //因为直线(L1)在Y轴上,所以方程为:y=0x+0;即Y=0;斜率为0  
  22.   k2 = y / x; //直线(L2)的斜率为 y/x,前面已经去除了x=0y=0的情况  
  23.   int  result = round(atan(fabs(k1 - k2)) * 180 / M_PI);  
  24.   //由于K1=0,所以 a :abs(k1 - k2) / abs(1 + k1 * k2);  
  25.   if ( (x > 0) && (y < 0) )  
  26.   {  
  27.       result = 90 - result;  
  28.   }  
  29.   else if ( (x > 0) && (y > 0) )  
  30.   {  
  31.       result = 90 + result;  
  32.   }  
  33.   else if ( (x < 0) && (y > 0) )  
  34.   {  
  35.       result = 270 - result;  
  36.   }  
  37.   else if ( (x < 0) && (y < 0) )  
  38.   {  
  39.       result = 270 + result;  
  40.   }  
  41.   return result;  
  1. void MainWindow::setCurrentFile(const QString &fileName)  
  2. {  
  3.     curFile = fileName;  
  4.     if (curFile.isEmpty())  
  5.         setWindowTitle(tr("Recent Files"));  
  6.     else  
  7.         setWindowTitle(tr("%1 - %2").arg(strippedName(curFile))  
  8.                                     .arg(tr("Recent Files")));  
  9.  
  10.     QSettings settings("Trolltech", "Recent Files Example");  
  11.     QStringList files = settings.value("recentFileList").toStringList();  
  12.     files.removeAll(fileName);  
  13.     files.prepend(fileName);  
  14.     while (files.size() > MaxRecentFiles)  
  15.         files.removeLast();  
  16.     settings.setValue("recentFileList", files); 

   
setMouseTracking(true)是打开鼠标移动跟踪,默认情况下只有在鼠标按下后才会发送QMouseMoveEvent()事件,打开鼠标移动跟踪后就能够随时发送了。

Qt获取mysql包含中文的值 

  1. QString lname2 = QString::fromUtf8(query.value(0).toByteArray());  
  2. qDebug()< 
  3. QTreeWidgetItem::setData ( int column, int role, const QVariant & value ) 

用法:自定义一个类:

  1. class ItemData  
  2. {  
  3. public:  
  4.   QString name;  
  5.   int age;  
  6. };  
  7. Q_DECLARE_METATYPE(ItemData); 

//把数据指针存入结点Data:

  1. void GpsSideBar::setItemData(QTreeWidgetItem * item,ItemData *itemData)  
  2. {  
  3.     //item->setData(0,Qt::UserRole, qVariantFromValue(ItemData(*itemData)) );  
  4.     item->setData(0,Qt::UserRole, qVariantFromValue( int(itemData) ) );  
  5. }  
  6. //取值  
  7. ItemData* GpsSideBar::GetGPSNestData(QTreeWidgetItem *item)  
  8. {  
  9.     //return qVariantValue(item->data(0,Qt::UserRole));  
  10.    return  reinterpret_cast ( qVariantValue(item->data(0,Qt::UserRole)) );  

在linux下运行designer不能正常显示中文的解决方法:

在qtconfig中设置font为Bitstream Charter,然后保存就OK了。

小结:本文主要介绍了在Qt 窗体的使用,通过Qt 编程点滴介绍,也给自己提高了编程过程中需要注意的细节问题,由于本话题是一节一节为大家展现的,所以更多内容,请看编辑推荐。

 

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-06-17 14:29:55

Qt

2011-06-17 14:12:32

Qt

2011-06-17 15:32:28

Qt

2011-06-17 14:54:31

Qt

2011-06-17 15:25:18

Qt

2011-06-17 15:44:25

Qt

2011-06-17 14:41:56

Qt

2011-06-17 15:19:28

Qt

2011-06-17 15:37:42

Qt

2011-09-16 09:38:19

Emacs

2011-06-27 14:56:46

Qt Designer

2011-09-08 10:38:37

Widget

2011-08-24 17:05:01

Lua

2013-04-23 10:51:15

Linux压缩

2011-07-26 17:55:16

iPhone Runtime

2011-08-04 18:01:07

IOS Cocoa Touc

2009-10-29 09:19:59

ADO.NET

2009-11-17 15:33:26

PHP数组元素

2009-10-22 16:46:03

VB.NET初步知识

2009-12-02 10:01:54

点赞
收藏

51CTO技术栈公众号