Qt6 topics merged with General

Multiple QRect

  • Hi All,
    I have added an event filter for Mouse Clicks. I am checking whether the mouse click is inside a QRect or not. If it is inside QRect, it is OK else block the event.This approach is OK for one QRect or two QRect.

    Now the problem is that i do not know in advance how many QRects are present as this info get at runtime. I basically want to have OR condition between all the QRects like this:

    ' if(QRect(x1,y1,w1,h1).contains(e->pos()) || QRect(x2,y2,w2,h2).contains(e->pos()) || QRect(x3,y3,w3,h3).contains(e->pos()) || QRect(x4,y4,w4,h4).contains(e->pos()) || QRect(x5,y5,w5,h5).contains(e->pos())) '

    But as i do not know before hand how many QRects will come, i am not able to write the above statement. I want to know if there is any operation on QRect that i can perform to get the union of all the QRects so that i can have one final QRect at the end and i can put that inside the if condition. Kindly give some pointers how to proceed??


  • Moderators

    @Sidii You can keep a QList of QRect (i.e when QRect come add it to QList) and when the event of interest is triggered check throughout the list. In this way you need not require the hardcoded OR condition.

  • @p3c0 Thanks for your reply. But i have one question. How will i put OR condition between all the elements of QList?
    if(list.at(0).contains(e.pos())|| list.at(1).contains(e.pos()) || list.at(2).contains(e.pos()))

    As i do not know in advance how many QList elements i have to put inside the if condition.

    Kindly give some hints.


  • Moderators

    @Sidii No need of OR condition. Just iterate over the list. For eg:

    QRect r1(0,0,30,60);
    QRect r2(10,20,40,60);
    QRect r3(20,30,50,60);
    QList<QRect> list;
    foreach(QRect rect, list) {
        if(rect.contains(e.pos())) {
            qDebug() << rect;

  • Moderators

    @Sidii Make sure when a new QRect comes, add it to the QList.

  • @p3c0 Thanks i got it :)

  • @p3c0 Will mark this thread as solved !!

  • Moderators

    @Sidii Ok. You're Welcome :)

  • Moderators

    @p3c0 You can upvote answer if you find it useful ;)