自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Qt 編程點(diǎn)滴 初學(xué)者必看 (5)

移動(dòng)開(kāi)發(fā)
本文介紹的是Qt 編程點(diǎn)滴,作為一名新手,我建議必須看一看。編程那些事,只有編程人員自己明白!所以推薦本文。

Qt 編程繼續(xù)為大家講解,還是接著文章 Qt 編程點(diǎn)滴 初學(xué)者必看 (4) ,繼續(xù)介紹,說(shuō)編程那些細(xì)節(jié)。由于本話題是一節(jié)一節(jié)為大家介紹的,所以更多內(nèi)容請(qǐng)看末尾編輯推薦。

QTreeWidget/QTreeView中的CheckStatus狀態(tài)的級(jí)聯(lián)更新

  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所有結(jié)點(diǎn)(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配置文件中的字段名是區(qū)分大小寫(xiě)的

  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); 

計(jì)算坐標(biāo)兩點(diǎn)間的角度,有兩種方法。

***種方法:

  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軸上時(shí)兩種結(jié)果  
  13.       if ( y > 0) return 180;  
  14.   }  
  15.  
  16.   if ( y == 0)  
  17.   {  
  18.       if ( x > 0 ) return 90;//在Y軸上時(shí)兩種結(jié)果  
  19.       if ( x < 0) return 270;  
  20.   }  
  21.   k1 = 0; //因?yàn)橹本€(L1)在Y軸上,所以方程為:y=0x+0;即Y=0;斜率為0  
  22.   k2 = y / x; //直線(L2)的斜率為 y/x,前面已經(jīng)去除了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)是打開(kāi)鼠標(biāo)移動(dòng)跟蹤,默認(rèn)情況下只有在鼠標(biāo)按下后才會(huì)發(fā)送QMouseMoveEvent()事件,打開(kāi)鼠標(biāo)移動(dòng)跟蹤后就能夠隨時(shí)發(fā)送了。

Qt獲取mysql包含中文的值 

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

用法:自定義一個(gè)類:

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

//把數(shù)據(jù)指針存入結(jié)點(diǎn)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下運(yùn)行designer不能正常顯示中文的解決方法:

在qtconfig中設(shè)置font為Bitstream Charter,然后保存就OK了。

小結(jié):本文主要介紹了在Qt 窗體的使用,通過(guò)Qt 編程點(diǎn)滴介紹,也給自己提高了編程過(guò)程中需要注意的細(xì)節(jié)問(wèn)題,由于本話題是一節(jié)一節(jié)為大家展現(xiàn)的,所以更多內(nèi)容,請(qǐng)看編輯推薦。

 

責(zé)任編輯:zhaolei 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-06-17 14:29:55

Qt

2011-06-17 14:12:32

Qt

2011-06-17 15:32:28

Qt

2011-06-17 15:19:28

Qt

2011-06-17 15:25:18

Qt

2011-06-17 15:44:25

Qt

2011-06-17 15:37:42

Qt

2011-06-17 14:54:31

Qt

2011-06-17 14:41:56

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數(shù)組元素

2009-10-22 16:46:03

VB.NET初步知識(shí)

2011-08-30 11:23:16

無(wú)線網(wǎng)卡怎么用無(wú)線網(wǎng)卡無(wú)線上網(wǎng)卡
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)