Qt 编程点滴 初学者必看 (5)
创始人
2024-08-01 02:21:30
0

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=0或y=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 编程点滴介绍,也给自己提高了编程过程中需要注意的细节问题,由于本话题是一节一节为大家展现的,所以更多内容,请看编辑推荐。

 

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...