当前位置:首页 > QT编程实用大全
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 return reinterpret_cast =========================================================== 在linux下运行designer不能正常显示中文的解决方法: 在qtconfig中设置font为Bitstream Charter,然后保存就OK了 ======================================= 移交控制权 qApp.processEvents(); 相当于delphi中的application.processmessage; ================================================== Qt Script Debugger — 用于调试Qt Script的工具,可以单步运行,查看输出等。 Qt文档里有很详细的一篇专门讲这个的,有兴趣的来看下: Qt Script Debugger Manual ============================================= Com口大于10需经特殊处理: \\\\\\\\.\\\\COMxx 如 \\\\\\\\.\\\\COM10 等价于 COM10; ==================================================== 透明的控件的TranslucentBackground属性为true (继承了parent的属性), 而非透明的控件则在代码中强制将TranslucentBackground设为了false, 这样就造就了有意思的结果。 代码片段如下: label = new QLabel(‖www.cuteqt.com‖); label->setAttribute(Qt::WA_TranslucentBackground, false); //设置为false完全不透明 label->setAutoFillBackground(true); ======================= 怎样将日志输出到文件中 void myMessageOutput( QtMsgType type, const char *msg ) { switch ( type ) { case QtDebugMsg: //写入文件; break; case QtWarningMsg: break; case QtFatalMsg: abort(); } } int main( int argc, char** argv ) { QApplication app( argc, argv ); qInstallMsgHandler( myMessageOutput ); …… return app.exec(); } qDebug(), qWarning(), qFatal()分别对应以上三种type。 ======================================================= QGraphicsView的updateSceneRect 有些时候,当你往一个QGraphicsView中添加一个空的QGraphicsScene并且批量地在这个QGraphicsScene中添加上大量的自定义的图形对象时,会发现QGraphicsView显示出来的图像有些偏移:有足够的空间来显示这些图形,可是有些图形画到QGraphicsView的边缘去了以致于没有完全显示出来。 这是因为当前的消息循环还没有处理完毕,因此QGraphicsView的槽―updateSceneRect‖还没有被调用。这样它的sceneRect没有刷新,就没有将更改过大小的scene移动到中心点了。 解决办法是在添加完毕图形对象之后立即调用updateSceneRect,使之刷新sceneRect。 ======================================================= QGraphicsView绘图问题 QGraphicsScene scene; scene.setSceneRect(0, 0, 800, 800); QGraphicsLineItem *line = new QGraphicsLineItem(0, 0, 500, 500); scene.addItem(line); QGraphicsView *view = new QGraphicsView(&scene); 上面这段代码,如果把view作为主窗体在main函数中显示出来,线会正常的画出来. 但一但有其它窗体作为主窗体,比如MainWindow,然后在其构造函数或其它函数中调用这这段代码, view可以显示出来,但线不会被画出来.(无论是作为单独的窗体还是作为MainWindow的 CentralWidget都不会被画出来,看了sample里面的几乎完全一样的代码却正常 解决方法: scene是局部变量,函数结束后被销毁了,应该用 QGraphicsScene *scene = new QGraphicsScene(this); 但问题是为什么main函数中这样用不会出问题? 因为你那个main函数没有结束,这个函数是要到程序结束时结束的,所以那个临时变量没有删除, 这样用就没有问题。其他的函数调用完就结束了。 ================================ ======================= 查出通讯录中代理不能取得焦点的BUG原因:MainWindow 要是继续自QMainWindow或QWidget就 取不了焦点,但如果继承自QDialog则可以取得焦点 =================================================== 窗体CallingCardEdtFrm(继承自QWidget),在此窗体上创建个组件 QListWidget,QListWidget中的QListWidgetItem(里面有个QLineEdit编辑组件)的绘制与 显示使用代理实现 class CallingCardEdtDlg:public QDialog //如此继承自QMainWindow或QWidget则QLineEdit获取不了 //焦点并且不能输入,但如果继承自QDialog就没问题了 { Q_OBJECT public: CallingCardEdtDlg(QWidget*); }; CallingCardEdtDlg::CallingCardEdtDlg(QWidget* parent) :QDialog(parent) { CallingCardEdtForm * frm = new CallingCardEdtForm(0); frm->setGeometry(0,0,200,200); QStackedWidget* stackedWidget = new QStackedWidget(0); stackedWidget->addWidget(frm); stackedWidget->setCurrentIndex(0); QGraphicsScene* scene = new QGraphicsScene(); QGraphicsView* view = new QGraphicsView(scene); view->setParent(this); QGraphicsProxyWidget* proxyWidget = new QGraphicsProxyWidget(); proxyWidget->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
共分享92篇相关文档