1.菜单栏上的弹出窗口
void MainWindow::on_new_action_triggered() { MyDialog myDialog;//MyDialog是一个ui myDialog.setModal(true); myDialog.exec(); /*******上面的写法弹出的窗口挡住后面的窗口***********/ /*******下面的写法弹出的窗口不挡住后面的窗口,并且可以弹出多个****/ // myDialog = new MyDialog(this); // myDialog->show(); }
2.水平布局&垂直布局
QWidget *window = new QWidget; window->setWindowTitle("Layout测试"); QPushButton *button1 = new QPushButton("one"); QPushButton *button2 = new QPushButton("two"); QPushButton *button3 = new QPushButton("three"); QHBoxLayout * hlayout = new QHBoxLayout; hlayout->addWidget(button1); hlayout->addWidget(button2); hlayout->addWidget(button3); window->setLayout(hlayout); window->show();
QVBoxLayout * hlayout = new QVBoxLayout;//垂直布局
void Mainwindow::init(){ vBoxLayout = new QVBoxLayout(this); topWidget = new QWidget; topWidget->setStyleSheet("background:#FFCCCC"); topWidget->setMaximumHeight(50); topWidget->setMinimumHeight(50); vBoxLayout->addWidget(topWidget); mainWidget = new QWidget; mainWidget->setStyleSheet("background:#0099CC"); vBoxLayout->addWidget(mainWidget); mainVBoxLayout = new QVBoxLayout(mainWidget); //定义一个垂直布局,垂直布局放到mainWidget中 //MAX_X MAX_Y 都在.h中预定义 for(int i = 0 ; i < MAX_X ; i++){ mainHBoxLayout[i] = new QHBoxLayout();//新建一个水平布局 for(int j = 0 ; j < MAX_Y ; j++){ buttons[i][j] = new QPushButton; 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.小球碰撞边框反弹算法
void MainWindow::slotBallMove(){ //小球的坐标 int mx = ball.x() + ball.width() / 2; int my = ball.y() + ball.height(); if(my >= bat.y()){ if(mx >= bat.x() && mx <= bat.x() + bat.width()){ dy = -1; } else{ timer.stop(); emit signalGameOver(); } } else if(x > this->width() - ball.width()){ dx = -1; } else if(y < 0){ dy = 1; } else if(x < 0){ dx = 1; } x += dx; y += dy; ball.move(x, y); }
4.弹出的messageBox
QMessageBox::information(this, "提示", "*** Game Over ***", QMessageBox::Ok);
5.设置ico图标
myapp.rc//放在项目目录下 IDI_ICON1 ICON DISCARDABLE "appico.ico" //然后在.pro文件中添加下面的语句 RC_FILE = myapp.rc
6.获取屏幕分辨率、计算机最佳显示位置,最小window大小
QDesktopWidget* desktopWidget = QApplication::desktop(); //获取可用桌面大小 QRect deskRect = desktopWidget->availableGeometry(); //获取设备屏幕大小 QRect screenRect = desktopWidget->screenGeometry(); int screenX = screenRect.width(); int screenY = screenRect.height(); int screenCount = desktopWidget->screenCount();//可用的显示器数 int appWidth = 1000; int appHeight = 618; int x_p = screenX/2 - appWidth/2;//计算出居中显示位置 int y_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