开发者

this->geometry and other size functions returning windowsize and not widget size. - Qt

I have a widget (which inherits QWidget) that when the user clicks on it, he can drag it anywhere on the screen. I've implemented the mouseMoveEvent() and setMouseTracking() false f开发者_开发技巧or this widget. This is the code,

void Piece::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() == Qt::LeftButton && this->geometry().contains(event->pos()))
    {
    x = event->x()-18;
    y = event->y()-18;
    update();
    }
}

The problem is (after using the debugger) I find that geometry() gives the dimensions of the window (400x400) and not the widget dimensions (36x36). I've also tried other functions that returned QRects, and I've tried the underMouse() function, but every rect is 400x400 and the underMouse() function always returns true, even if the mouse isn't above the widget's image.

I have an idea of what the problem might be, which may be wrong. The application contains a checkerboard of squares, which each square being an object in part of a QGridLayout. In order to draw a widget on this grid (the playing piece), I just did,

layout->addWidget(pawn[0], 1, 1, 8, 8);

So in other words, the widget can be placed anywhere on the screen (the grid contains 8 rows and columns). If this is the problem, how do I display this piece widget on top of the grid layout? I've tried simply adding the widget to the parent widget, the same parent widget that is the parent to the QGridLayout (and I made sure to set the parent in the constructor), but that didn't work. If this is causing the problem, than how do I fix it? If not, what's causing the problem?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜