Qt 編程點(diǎn)滴 初學(xué)者必看 (5)
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)更新
- 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所有結(jié)點(diǎn)(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配置文件中的字段名是區(qū)分大小寫(xiě)的
- 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);
計(jì)算坐標(biāo)兩點(diǎn)間的角度,有兩種方法。
***種方法:
- 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軸上時(shí)兩種結(jié)果
- if ( y > 0) return 180;
- }
- if ( y == 0)
- {
- if ( x > 0 ) return 90;//在Y軸上時(shí)兩種結(jié)果
- if ( x < 0) return 270;
- }
- k1 = 0; //因?yàn)橹本€(L1)在Y軸上,所以方程為:y=0x+0;即Y=0;斜率為0
- k2 = y / x; //直線(L2)的斜率為 y/x,前面已經(jīng)去除了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)是打開(kāi)鼠標(biāo)移動(dòng)跟蹤,默認(rèn)情況下只有在鼠標(biāo)按下后才會(huì)發(fā)送QMouseMoveEvent()事件,打開(kāi)鼠標(biāo)移動(dòng)跟蹤后就能夠隨時(shí)發(fā)送了。
Qt獲取mysql包含中文的值
- QString lname2 = QString::fromUtf8(query.value(0).toByteArray());
- qDebug()<
- QTreeWidgetItem::setData ( int column, int role, const QVariant & value )
用法:自定義一個(gè)類:
- class ItemData
- {
- public:
- QString name;
- int age;
- };
- Q_DECLARE_METATYPE(ItemData);
//把數(shù)據(jù)指針存入結(jié)點(diǎn)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下運(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)看編輯推薦。