Qt 編程點滴 初學者必看 (5)
Qt 編程繼續為大家講解,還是接著文章 Qt 編程點滴 初學者必看 (4) ,繼續介紹,說編程那些細節。由于本話題是一節一節為大家介紹的,所以更多內容請看末尾編輯推薦。
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();
- py1 = centerPos.y();
- px2 = pos.x();
- py2 = pos.y();
- double x = px2 - px1;
- double y = py2 - py1;
- double hyp = sqrt(pow(x,2) + pow(y,2));
- double cos = x / hyp;
- double rad = acos(cos);
- double deg = 180/(M_PI / rad);
- if (y < 0)
- {
- deg = -deg;
- }
- else if ((y == 0) && (x <0))
- {
- deg = 180;
- }
- degdeg = deg + 90;
- if (deg < 0)
- {
- degdeg = deg + 360;
- }
- return deg;
- }
第二種方法:
- int calcAngle(const double& sx,const double& sy,const double& dx,const double& dy)
- {
- double x, y, k1, k2;
- x = dx - sx;
- y = dy - sy;
- if ( (x == 0) && (y == 0) )
- {
- return 0;
- }
- if (x == 0)
- {
- if ( y < 0) return 0;////在X軸上時兩種結果
- if ( y > 0) return 180;
- }
- if ( y == 0)
- {
- if ( x > 0 ) return 90;//在Y軸上時兩種結果
- if ( x < 0) return 270;
- }
- k1 = 0; //因為直線(L1)在Y軸上,所以方程為:y=0x+0;即Y=0;斜率為0
- k2 = y / x; //直線(L2)的斜率為 y/x,前面已經去除了x=0或y=0的情況
- int result = round(atan(fabs(k1 - k2)) * 180 / M_PI);
- //由于K1=0,所以 a := abs(k1 - k2) / abs(1 + k1 * k2);
- if ( (x > 0) && (y < 0) )
- {
- result = 90 - result;
- }
- else if ( (x > 0) && (y > 0) )
- {
- result = 90 + result;
- }
- else if ( (x < 0) && (y > 0) )
- {
- result = 270 - result;
- }
- else if ( (x < 0) && (y < 0) )
- {
- result = 270 + result;
- }
- return result;
- }
- void MainWindow::setCurrentFile(const QString &fileName)
- {
- curFile = fileName;
- if (curFile.isEmpty())
- setWindowTitle(tr("Recent Files"));
- else
- setWindowTitle(tr("%1 - %2").arg(strippedName(curFile))
- .arg(tr("Recent Files")));
- QSettings settings("Trolltech", "Recent Files Example");
- QStringList files = settings.value("recentFileList").toStringList();
- files.removeAll(fileName);
- files.prepend(fileName);
- while (files.size() > MaxRecentFiles)
- files.removeLast();
- settings.setValue("recentFileList", files);
setMouseTracking(true)是打開鼠標移動跟蹤,默認情況下只有在鼠標按下后才會發送QMouseMoveEvent()事件,打開鼠標移動跟蹤后就能夠隨時發送了。
Qt獲取mysql包含中文的值
- QString lname2 = QString::fromUtf8(query.value(0).toByteArray());
- qDebug()<
- QTreeWidgetItem::setData ( int column, int role, const QVariant & value )
用法:自定義一個類:
- class ItemData
- {
- public:
- QString name;
- int age;
- };
- Q_DECLARE_METATYPE(ItemData);
//把數據指針存入結點Data:
- void GpsSideBar::setItemData(QTreeWidgetItem * item,ItemData *itemData)
- {
- //item->setData(0,Qt::UserRole, qVariantFromValue(ItemData(*itemData)) );
- item->setData(0,Qt::UserRole, qVariantFromValue( int(itemData) ) );
- }
- //取值
- ItemData* GpsSideBar::GetGPSNestData(QTreeWidgetItem *item)
- {
- //return qVariantValue(item->data(0,Qt::UserRole));
- return reinterpret_cast ( qVariantValue(item->data(0,Qt::UserRole)) );
- }
在linux下運行designer不能正常顯示中文的解決方法:
在qtconfig中設置font為Bitstream Charter,然后保存就OK了。
小結:本文主要介紹了在Qt 窗體的使用,通過Qt 編程點滴介紹,也給自己提高了編程過程中需要注意的細節問題,由于本話題是一節一節為大家展現的,所以更多內容,請看編輯推薦。