1.菜单栏上的弹出窗口
voidMainWindow::on_new_action_triggered() { MyDialogmyDialog;//MyDialog是一个ui myDialog.setModal(true); myDialog.exec(); /*******上面的写法弹出的窗口挡住后面的窗口***********/ /*******下面的写法弹出的窗口不挡住后面的窗口,并且可以弹出多个****/ //myDialog=newMyDialog(this); //myDialog->show(); }
2.水平布局&垂直布局
QWidget*window=newQWidget; window->setWindowTitle("Layout测试"); QPushButton*button1=newQPushButton("one"); QPushButton*button2=newQPushButton("two"); QPushButton*button3=newQPushButton("three"); QHBoxLayout*hlayout=newQHBoxLayout; hlayout->addWidget(button1); hlayout->addWidget(button2); hlayout->addWidget(button3); window->setLayout(hlayout); window->show();
QVBoxLayout*hlayout=newQVBoxLayout;//垂直布局
voidMainwindow::init(){ vBoxLayout=newQVBoxLayout(this); topWidget=newQWidget; topWidget->setStyleSheet("background:#FFCCCC"); topWidget->setMaximumHeight(50); topWidget->setMinimumHeight(50); vBoxLayout->addWidget(topWidget); mainWidget=newQWidget; mainWidget->setStyleSheet("background:#0099CC"); vBoxLayout->addWidget(mainWidget); mainVBoxLayout=newQVBoxLayout(mainWidget); //定义一个垂直布局,垂直布局放到mainWidget中 //MAX_XMAX_Y都在.h中预定义 for(inti=0;i<MAX_X;i++){ mainHBoxLayout[i]=newQHBoxLayout();//新建一个水平布局 for(intj=0;j<MAX_Y;j++){ buttons[i][j]=newQPushButton; buttons[i][j]->setStyleSheet("background:black"); buttons[i][j]->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); //Expanding填充 mainHBoxLayout[i]->addWidget(buttons[i][j]); //将每一行的button添加到水平布局 } mainVBoxLayout->addLayout(mainHBoxLayout[i]); //将水平布局添加到垂直布局中 } }
3.小球碰撞边框反弹算法
voidMainWindow::slotBallMove(){ //小球的坐标 intmx=ball.x()+ball.width()/2; intmy=ball.y()+ball.height(); if(my>=bat.y()){ if(mx>=bat.x()&&mx<=bat.x()+bat.width()){ dy=-1; } else{ timer.stop(); emitsignalGameOver(); } } elseif(x>this->width()-ball.width()){ dx=-1; } elseif(y<0){ dy=1; } elseif(x<0){ dx=1; } x+=dx; y+=dy; ball.move(x,y); }
4.弹出的messageBox
QMessageBox::information(this,"提示","***GameOver***",QMessageBox::Ok);
5.设置ico图标
myapp.rc//放在项目目录下 IDI_ICON1ICONDISCARDABLE"appico.ico" //然后在.pro文件中添加下面的语句 RC_FILE=myapp.rc
6.获取屏幕分辨率、计算机最佳显示位置,最小window大小
QDesktopWidget*desktopWidget=QApplication::desktop(); //获取可用桌面大小 QRectdeskRect=desktopWidget->availableGeometry(); //获取设备屏幕大小 QRectscreenRect=desktopWidget->screenGeometry(); intscreenX=screenRect.width(); intscreenY=screenRect.height(); intscreenCount=desktopWidget->screenCount();//可用的显示器数 intappWidth=1000; intappHeight=618; intx_p=screenX/2-appWidth/2;//计算出居中显示位置 inty_p=screenY/2-appHeight/2; this->setGeometry(x_p,y_p,appWidth,appHeight); this->setMinimumHeight(appHeight); this->setMinimumWidth(appWidth); qDebug()<<screenX<<screenY<<screenCount;
本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/1411247