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

本文介绍的是Qt 编程点滴,作为一名新手,我建议必须看一看。编程那些事,只有编程人员自己明白!所以推
首页 新闻资讯 行业资讯 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);              }          }      }  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

清空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;      }  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

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

复制

void MainWindow::contextMenuEvent(QContextMenuEvent *event)  {      QMenu menu(this);      menu.addAction(cutAct);      menu.addAction(copyAct);      menu.addAction(pasteAct);      menu.exec(event->globalPos());  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

 
让QLineEdit不弹出右键菜单:

复制

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

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

***种方法:

复制

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;  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

第二种方法:

复制

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;  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

复制

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);
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

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

Qt获取mysql包含中文的值 

复制

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

  • 2.

  • 3.

用法:自定义一个类:

复制

class ItemData  {  public:    QString name;    int age;  };  Q_DECLARE_METATYPE(ItemData);
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

//把数据指针存入结点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)) );  }
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

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

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

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

 

14    2011-06-17 15:06:14    Qt