1.菜单栏上的弹出窗口

  1. void MainWindow::on_new_action_triggered()
  2. {
  3.     MyDialog myDialog;//MyDialog是一个ui
  4.     myDialog.setModal(true);
  5.     myDialog.exec();
  6.     /*******上面的写法弹出的窗口挡住后面的窗口***********/
  7.     /*******下面的写法弹出的窗口不挡住后面的窗口,并且可以弹出多个****/
  8. //    myDialog = new MyDialog(this);
  9. //    myDialog->show();
  10. }

2.水平布局&垂直布局

  1. QWidget *window = new QWidget;
  2. window->setWindowTitle("Layout测试");
  3. QPushButton *button1 = new QPushButton("one");
  4. QPushButton *button2 = new QPushButton("two");
  5. QPushButton *button3 = new QPushButton("three");
  6. QHBoxLayout * hlayout = new QHBoxLayout;
  7. hlayout->addWidget(button1);
  8. hlayout->addWidget(button2);
  9. hlayout->addWidget(button3);
  10. window->setLayout(hlayout);
  11. window->show();

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/27/9D/wKioL1Nzc43RxUmAAABmPedd_k4814.jpg" title="QQ截图20140514214110.png" alt="wKioL1Nzc43RxUmAAABmPedd_k4814.jpg" />

  1. QVBoxLayout * hlayout = new QVBoxLayout;//垂直布局

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/27/9D/wKioL1Nzc-bjRFIlAABSqEd680s737.jpg" title="QQ截图20140514214231.png" alt="wKioL1Nzc-bjRFIlAABSqEd680s737.jpg" />

  1. void Mainwindow::init(){
  2.     vBoxLayout = new QVBoxLayout(this);
  3.     
  4.     topWidget = new QWidget;
  5.     topWidget->setStyleSheet("background:#FFCCCC");
  6.     topWidget->setMaximumHeight(50);
  7.     topWidget->setMinimumHeight(50);
  8.     vBoxLayout->addWidget(topWidget);
  9.  
  10.     mainWidget = new QWidget;
  11.     mainWidget->setStyleSheet("background:#0099CC");
  12.  
  13.     vBoxLayout->addWidget(mainWidget);
  14.  
  15.     mainVBoxLayout = new QVBoxLayout(mainWidget);
  16.     //定义一个垂直布局,垂直布局放到mainWidget中
  17.     //MAX_X MAX_Y 都在.h中预定义
  18.     for(int i = 0 ; i < MAX_X ; i++){
  19.         mainHBoxLayout[i] = new QHBoxLayout();//新建一个水平布局
  20.         for(int j = 0 ; j < MAX_Y ; j++){
  21.             buttons[i][j] = new QPushButton;
  22.             buttons[i][j]->setStyleSheet("background:black");
  23.             buttons[i][j]->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  24.             //Expanding填充
  25.             mainHBoxLayout[i]->addWidget(buttons[i][j]);
  26.             //将每一行的button添加到水平布局
  27.         }
  28.         mainVBoxLayout->addLayout(mainHBoxLayout[i]);
  29.         //将水平布局添加到垂直布局中
  30.     }
  31. }

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/2A/A1/wKioL1OEWjGB7FixAAG0BdoWs9U446.jpg" title="QQ截图20140527172030.png" alt="wKioL1OEWjGB7FixAAG0BdoWs9U446.jpg" />

3.小球碰撞边框反弹算法

  1. void MainWindow::slotBallMove(){
  2.     //小球的坐标
  3.     int mx = ball.x() + ball.width() / 2;
  4.     int my = ball.y() + ball.height();
  5.  
  6.     if(my >= bat.y()){
  7.         if(mx >= bat.x() && mx <= bat.x() + bat.width()){
  8.             dy = -1;
  9.         }
  10.         else{
  11.             timer.stop();
  12.             emit signalGameOver();
  13.         }
  14.     }
  15.     else if(> this->width() - ball.width()){
  16.         dx = -1;
  17.     }
  18.     else if(< 0){
  19.         dy = 1;
  20.     }
  21.     else if(< 0){
  22.         dx = 1;
  23.     }
  24.  
  25.     x += dx;
  26.     y += dy;
  27.     ball.move(x, y);
  28.  
  29. }

4.弹出的messageBox

  1.  QMessageBox::information(this, "提示", "*** Game Over ***", QMessageBox::Ok);

5.设置ico图标

  1. myapp.rc//放在项目目录下
  2. IDI_ICON1               ICON    DISCARDABLE     "appico.ico" 
  3. //然后在.pro文件中添加下面的语句
  4. RC_FILE = myapp.rc

6.获取屏幕分辨率、计算机最佳显示位置,最小window大小

  1.     QDesktopWidget* desktopWidget = QApplication::desktop();
  2.     //获取可用桌面大小
  3.     QRect deskRect = desktopWidget->availableGeometry();
  4.     //获取设备屏幕大小
  5.     QRect screenRect = desktopWidget->screenGeometry();
  6.     int screenX = screenRect.width();
  7.     int screenY = screenRect.height();
  8.     int screenCount = desktopWidget->screenCount();//可用的显示器数
  9.     int appWidth = 1000;
  10.     int appHeight = 618;
  11.     int x_p = screenX/2 - appWidth/2;//计算出居中显示位置
  12.     int y_p = screenY/2 - appHeight/2;
  13.     this->setGeometry(x_p, y_p, appWidth, appHeight);
  14.     this->setMinimumHeight(appHeight);
  15.     this->setMinimumWidth(appWidth);
  16.     qDebug() << screenX << screenY << screenCount;

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1411247

QT笔记的更多相关文章

  1. QT笔记之解决QT5.2.0和VS2012中文乱码 以及在Qt Creator中文报错

    转载:http://bbs.csdn.net/topics/390750169 VS2012 中文乱码 1.方法一: 包含头文件 #include <QTextCodec> ....... ...

  2. QT笔记之VS开发程序遇到的问题

    转载:http://www.cnblogs.com/li-peng/p/3644812.html 转载:http://www.cnblogs.com/csuftzzk/p/VS_Qt_Experien ...

  3. QT笔记之不规则窗口的实现

    QT实现的不规则窗口,是根据图片的形状显示 1.去标题栏 2.设置窗口背景为透明色 3.最后给窗口设置背景色 注:背景图为镂空的 格式为.png 图片资源下载:http://pan.baidu.com ...

  4. QT笔记之模态对话框及非模态对话框

    模态对话框(Modal Dialog)与非模态对话框(Modeless Dialog)的概念不是Qt所独有的,在各种不同的平台下都存在.又有叫法是称为模式对话框,无模式对话框等.所谓模态对话框就是在其 ...

  5. QT笔记之QLineEdit自动补全以及控件提升

    转载:http://www.cnblogs.com/csuftzzk/p/qss_lineedit_completer.html?utm_source=tuicool&utm_medium=r ...

  6. QT笔记之VS2010 Qt中导入qrc资源文件

    转载1:http://qimo601.iteye.com/blog/1404693 转载2:http://blog.sina.com.cn/s/blog_92cde3060101lobm.html 转 ...

  7. QT笔记之实现阴影窗口

    方法一: 代码实现 在窗口构造函数中加入:setAttribute(Qt::WA_TranslucentBackground),保证不被绘制上的部分透明 重写void paintEvent(QPain ...

  8. QT笔记之自定义窗口拖拽移动

    1.QT自定义标题栏,拖拽标题栏移动窗口(只能拖拽标题,其他位置无法拖拽) 方法一: 转载:http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html . ...

  9. 自学QT笔记

    前言: Qt 是一个跨平台的 C++图形用户界面库,由挪威 TrollTech 公司于1995年底出品. Trolltech 公司在 1994 年成立,但是在 1992 年,成立 Trolltech ...

  10. QT笔记(1)--QT编程环境搭建

    一.QT简介 Qt  是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊 ...

随机推荐

  1. WordPress翻译中 __()、_e()、_x、_ex 和 _n 的用法及区别

    编译函数 WordPress使用了下面几个函数来方便语言本地化. __() _e() _x() _ex() _n() 以上所列的函数是用来包含所需翻译的字符串的,根据字符串的不同参数和输出类型,需要使 ...

  2. 查找Linux系统中的占用磁盘空间

    目录的来查看空间占用情况 du -sh /* 先看看根目录下面 让文件夹下的文件让文件按大小排序 方法一:# ls -lhSl 长格式显示,h human readable模式,大小单位为M,G等易读 ...

  3. 算法与数据结构之选择排序(C语言)

    #include<stdio.h> #include<stdlib.h> void SelectSort(int *a,int n);//预声明要调用的函数 int main( ...

  4. 【UOJ#33】【UR#2】树上GCD 有根树点分治 + 容斥原理 + 分块

    #33. [UR #2]树上GCD 有一棵$n$个结点的有根树$T$.结点编号为$1…n$,其中根结点为$1$. 树上每条边的长度为$1$.我们用$d(x,y)$表示结点$x,y$在树上的距离,$LC ...

  5. 【bzoj1922】 Sdoi2010—大陆争霸

    http://www.lydsy.com/JudgeOnline/problem.php?id=1922 (题目链接) 题意 一张无向图,每个节点被k个节点保护,想要走到一个节点当且仅当它不被保护.你 ...

  6. HNOI2016(BZOJ4542) 大数

    HNOI2016 Day2 T3 大数 Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小B还有一个素数P ...

  7. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  8. SELINUX、Security Access Control Strategy && Method And Technology Research - 安全访问控制策略及其方法技术研究

    catalog . 引言 . 访问控制策略 . 访问控制方法.实现技术 . SELINUX 0. 引言 访问控制是网络安全防范和客户端安全防御的主要策略,它的主要任务是保证资源不被非法使用.保证网络/ ...

  9. tar.xz文件如何解压

    1. tar.xz介绍 XZ压缩最新压缩率之王 xz这个压缩可能很多都很陌生,不过您可知道xz是绝大数linux默认就带的一个压缩工具. 之前xz使用一直很少,所以几乎没有什么提起. 2. 压缩 ta ...

  10. 远程连接RabbitMQ失败

    远程连接RabbitMQ失败 为了避免污染宿主系统环境,于是在虚拟机中搭建了一个linux环境并且按照了rabbitmq-server.然后在远程连接的时候一直连接失败. 官网上面给的例子都是在本地使 ...