QRect::contains函数问题

Oskar_Sun 2014-12-10 10:21:59
两个问题
#include "mywidget.h"
#include<QtGui>


MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
setMouseTracking(true);
LineOfRect=false;
}
void MyWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen(Qt::red,4);
painter.setPen(pen);
QRect rect(30,30,200,200);
painter.drawRect(rect);
}

void MyWidget::setcursor()
{
if(LineOfRect)
{
setCursor(Qt::OpenHandCursor);
}
else{
setCursor(Qt::ArrowCursor);
}
}

void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
QRect rect(30,30,200,200);
if(rect.contains(event->pos(),false))
{
LineOfRect=true;
}
else{
LineOfRect=false;
}
setcursor();
QString pos=QString("%1,%2").arg(event->pos().x()).arg(event->pos().y());
QToolTip::showText(event->globalPos(),pos,this);
}



QRect::contains 函数帮助文档中:
bool QRect::contains ( const QPoint & point, bool proper = false ) const
Returns true if the given point is inside or on the edge of the rectangle, otherwise returns false. If proper is true, this function only returns true if the given point is inside the rectangle (i.e., not on the edge).
1.当proper为false时,当point在rect的边缘上应该也返回ture,但是我在上面的代码中鼠标在rect边缘上的时候鼠标形状并没有改变,而是当鼠标在rect内部时cursor才改变形状,为什么?
2.如果if语句改成 if(rect.contains(event->pos(),false)&&(!(rect.contains(event->pos(),ture)))) 可以实现当鼠标在rect边框上cursor形状改变么?
...全文
769 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
syaowen5835889 2014-12-11
  • 打赏
  • 举报
回复

    if(rect.contains(event->pos(),false)&!(rect.contains(event->pos(),true)))
就可以了。当参数为false时,鼠标在边框和边框内返回为真
#include "world.h" #include #include #include #include #include "bird.h" #include World::World(QWidget* parent): QWidget(parent) { //this->resize(432, 644); this->setGeometry(400,200, 432,644); bird = new Bird; ground = new Ground; c1 = new Column(0); c2 = new Column(1); gameoverImage.load(":gameover"); bgImage.load(":bg"); startImage.load(":start"); gameOver = false; startGame = false; score = 0; score_label = new QLabel(this); score_label->setGeometry(QRect(270,10,120,40)); score_label->setStyleSheet(QString::fromUtf8("font: 20pt \"Khmer OS System\";\n" "color: rgb(85, 0, 255);")); timer.setInterval(1000/70); connect(&timer, SIGNAL(timeout()), this, SLOT(run())); //一会写run // timer.start(); QFile file("./score.dat"); if(!file.open(QFile::ReadOnly | QFile::Text)){ best_score = 0; }else{ //QTextStream in(&file); QDataStream in(&file); in >> best_score; qDebug() << "read..."; } file.close(); } World::~World(){ if(score > best_score) save(score); } void World::save(unsigned short best){ QFile file("./score.dat"); if(!file.open(QFile::WriteOnly | QFile::Text)){ return; }else{ // QTextStream out(&file); QDataStream out(&file); out << best; //qDebug() << "write"; } file.close(); } //哑元函数 void World::paintEvent(QPaintEvent*){ QPainter painter(this); painter.drawImage(0,0,bgImage); //将画笔传给bird对象,由bird对象画出当前小鸟的图片 c1->paint(&painter); c2->paint(&painter); bird->paint(&painter); ground->paint(&painter); if(!startGame){ painter.drawImage(0,0,startImage); } if(gameOver){ painter.drawImage(0,0,gameoverImage); } if(!startGame){ painter.setFont(QFont("Khmer OS System",20,QFont::Bold)); painter.drawText(QRect(QPoint(145,390), QPoint(320,445)), QString::fromUtf8("历史最高:")+=QString::number(best_score)); } score_label->setText(QString("score:")+=QString::number(score)); } void World::run(){ bird->fly();//飞 bird->step();//小鸟下落 c1->step(); c2->step(); ground->step(); if(bird->pass(*c1) || bird->pass(*c2)){ qDebug("pass"); score++; } if(bird->hit(*c1,*c2,*ground)){ timer.stop(); gameOver = true; //gameover ... //TODO /** 1)加载gameover图片,实现点击图片 的开始按钮重新开始游戏。 2)将开始画面加入,点击鼠标或者键盘的 空格键才开始游戏 完成上两步后做以下工作: 3)加入评分机制。 通过一根柱子得1分 */ } this->repaint();//重新绘制 } void World::mousePressEvent(QMouseEvent *p){ //点击鼠标让当前速度保持为初始速度 //speed = v0; //bird->speed = bird->v0; bird->flappy(); //restart()之前 starGame = false; if(!startGame){ startGame = true; timer.start(); } if(gameOver){ QRect rect(QPoint(135,331), QPoint(281,408)); QPoint point = p->pos(); if(rect.contains(point)){ restart(); } } } //实现restart void World::restart(){ gameOver = false; startGame = false; if(score > best_score){ best_score = score; save(best_score); } score = 0; delete bird; delete c1; delete c2; bird = new Bird; c1 = new Column(0); c2 = new Column(1); qDebug()<< "restart..."; this->repaint();//定时器停止需要手动重绘 }

16,173

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧