qt学习笔记(四) qt编程时遇到的问题的总结

来源:互联网 发布:js身份证格式验证 编辑:程序博客网 时间:2024/05/16 15:23

1、设置qt widget全屏显示

[cpp] view plaincopyprint?
  1. int main(int argc, char *argv[])  
  2. {  
  3.     QApplication a(argc, argv);  
  4.     MainWindow w;  
  5.     //w.show();   
  6.     w.showFullScreen();  
  7.     return a.exec();  
  8. }  

2、获取屏幕分辨率

[html] view plaincopyprint?
  1. #include <QDesktopWidget>  
  2. #Include <QApplication>  
  3. void MainWindow::GetScreenInfo()  
  4. {  
  5.     QDesktopWidget* desktopWidget = QApplication::desktop();  
  6.     //获取可用桌面大小  
  7.     //QRect deskRect = desktopWidget->availableGeometry();  
  8.     //获取设备屏幕大小  
  9.     QRect screenRect = desktopWidget->screenGeometry();  
  10.   
  11.     sceenSizeX = screenRect.width();  
  12.     sceenSizeY = screenRect.height();  
  13.   
  14.     //获取系统设置的屏幕个数(屏幕拷贝方式该值为1)  
  15.     //g_nScreenCount = desktopWidget->screenCount();  
  16. }  

3、QGraphicsView添加背景图片:1

[html] view plaincopyprint?
  1. view = new QGraphicsView;  
  2. scene = new QGraphicsScene(0,0,800,480);  
  3. QPixmap pixmap(":/gear.png");  
  4. scene->addPixmap(pixmap);  
  5. view->setScene(scene);  

3、QGraphicsView添加背景图片:2

[cpp] view plaincopyprint?
  1. view->setBackgroundBrush(QImage(":/gear.png"));  
前提是将gear.png加入到资源中

4、设置无边框(标题栏)的应用程序

[cpp] view plaincopyprint?
  1. QApplication a(argc, argv);  
  2. MainWindow w;  
  3.   
  4. w.setWindowOpacity(1);  
  5. w.setWindowFlags(Qt::FramelessWindowHint);  
  6. w.setAttribute(Qt::WA_TranslucentBackground);  
  7. w.show();  

5 、QGraphicsPixmapItem,显示图片Item

[cpp] view plaincopyprint?
  1. scene = new QGraphicsScene(0,0,800,480);  
  2.   
  3. QGraphicsPixmapItem *pixmapItem = new QGraphicsPixmapItem(QPixmap(":/gear.png"));  
  4. pixmapItem->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);  
  5. scene->addItem(pixmapItem);  

6 Q_OBJECT 问题

[html] view plaincopyprint?
  1. class NodeUI : public QGraphicsPixmapItem  
  2. {  
  3.     Q_OBJECT  
  4. ...  
  5. }  
编译不通过

[cpp] view plaincopyprint?
  1. class NodeUI : public QGraphicsPixmapItem ,public QObject  
  2. {  
  3.     Q_OBJECT  
  4. ...  
  5. }  
依然不通过

[cpp] view plaincopyprint?
  1. class NodeUI : public QObject,public QGraphicsPixmapItem  
  2. {  
  3.     Q_OBJECT  
  4. ...  
  5. }  
编译通过!

7 ,编译出错:error: passing 'const QPointF' as 'this' argument of 'QPointF& QPointF::operator=(const QPointF&)' discards qualifiers

[cpp] view plaincopyprint?
  1. NodeUI *MainWindow::selectedNodeUI() const  
  2. {  
  3.     QList<QGraphicsItem *> items = scene->selectedItems();  
  4.     if (items.count() == 1) {  
  5.         mmyPos = items.first()->pos();  
  6.         //QPointF myPos = pos;   
  7.         //mmyPos = pos.toPoint();  
  8.         //mmyPos.setX(x);   
  9.         //mmyPos.setY(y);   
  10.         //mmyPos = QPoint(x,y);   
  11.         //qDebug()<<"items.x:"<<nodePos.x()<<"items.y:"<<nodePos.y();  
  12.         return dynamic_cast<NodeUI *>(items.first());  
  13.     } else {  
  14.         return 0;  
  15.     }  
  16. }  
修改为如下,问题解决;

[cpp] view plaincopyprint?
  1. NodeUI *MainWindow::selectedNodeUI()  
  2. {  
  3.     QList<QGraphicsItem *> items = scene->selectedItems();  
  4.     if (items.count() == 1) {  
  5.         mmyPos = items.first()->pos();  
  6.         //QPointF myPos = pos;   
  7.         //mmyPos = pos.toPoint();  
  8.         //mmyPos.setX(x);   
  9.         //mmyPos.setY(y);   
  10.         //mmyPos = QPoint(x,y);   
  11.         //qDebug()<<"items.x:"<<nodePos.x()<<"items.y:"<<nodePos.y();  
  12.         return dynamic_cast<NodeUI *>(items.first());  
  13.     } else {  
  14.         return 0;  
  15.     }  
  16. }  

8,QMap遍历问题

QMap插入时的顺序和最后得到的QMap遍历顺序可能是不通的,QMap所谓的有序是按照key内部自动升序,在不同的电脑上,顺序都可能不同。

对顺序有要求时,不可以用QMap,QHash就更不用说了。

转自:http://blog.csdn.net/ghostyu/article/details/7002178
原创粉丝点击