سؤال

I have a class like the following representing a game Card. There are more cards displayed on the table but I'd like that when one is clicked it goes at the center of the table. Now, there could already be another card at the center of the table (let's assume the center is at coordinates 100x, 100y) so if this is the case I'd like to position the new card next to it, say, at 200x, 100y but how do I tell if there's something already at 100, 100?

from PyQt4 import QtGui, QtCore

class CardLabel(QtGui.QLabel):
    def __init__(self, *args, **kwargs):
        QtGui.QLabel.__init__(self, *args, **kwargs)

    def mouseReleaseEvent(self, ev):
        print 'clicked'
        self.emit(QtCore.SIGNAL('clicked()'))
        self.move(100, 100)

I cannot find any QLabel attribute (not even in its inherited parents) nor method that returns the coordinates of a label. If I could I would maybe record the coordinates of all widgets in a global dictionary and loop over its items every time I have to move a card (would that be the best way or is there something more efficient?).

هل كانت مفيدة؟

المحلول

QLabel inherits from QWidget so the x(), y(), pos() and other similar functions are all available to it. Also you can have a look to the Window Geometry documentation that can help you. When you put a card at the center of the table you can update some dictionary or similar with the relevant information. If you want to put a new card at the center just read the dictionary and do things accordingly to its content.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top