0

I have a class, that subclasses QDialog without overriding exec(), accept() or reject() and another one, that calls the Dialog class inside its mousePaintEvent:

void Canvas::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton){
        if (dialog->isVisible()){
            dialog->setModal(true);
            dialog->move(QWidget::mapToGlobal(event->pos()));
             //I connect the dialog's accepted signal to the CallingClass's slot, that uses the information taken from the dialog
            connect(dialog, &Dialog::accepted, this, &CallingClass::slot);
            dialog->exec();
        }
    }
    if (dialog->isVisible()){
        if (dialog->rect().contains(event->pos())){
            dialog->reject();
        }
    }
}

I have tried using the Dialog's existence for the check, but delete didn't really work(I put it after dialog.reject()), I even tried using a bool, which I, again, set to false after dialog.reject() in the last if, but I start to think, that nothing works after .reject(). How am I to proceed?

2
  • 1
    what are you actually trying to do in high level terms?
    – UmNyobe
    Apr 22, 2015 at 15:42
  • This is the paint event of a "Canvas" (which is a QOpenGlWidget). What I want to do is to open a dialog on left-mouse button click, from which I can select what to draw on the canvas, and, while the Dialog is open, I want to be able to close it by clicking outside it. Apr 23, 2015 at 6:07

2 Answers 2

1

My understanding is that dialog->rect() does not give you what you want (see this). Unfortunately I can not test it right now, but I think you should try to use it in combination with pos or try directly to use frameGeometry. With this you will have the real position and size of your window relative to its parent. Try to see the values you obtain as coordinates from the click event and the values from those methods in order to figure out exactly how to use them... Basically you will need to decide whether to use global coordinates for the desktop of relative to your parent window.

6
  • Although that does help, I understand I didn't ask the question properly. It is about how am I to check whether the dialog is present, in this example I am checking isVisible(), before that I tried deleting dialog and checking if it is null, I even tried with a boolean set after dialog->reject() at the end, but none of this worked and I am not even sure of the reason Apr 23, 2015 at 8:23
  • 1
    Does your mouse event handler get called at all while the dialog is visible? It seems to me that having a modal window will prevent this from happening at all (you can check this for details). Apr 23, 2015 at 8:38
  • Since clicking the canvas inside the rect of the dialog is impossible I reduced my code to this: void Class::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton){ if (!dialog->isVisible()){ dialog->setModal(false); dialog->setVisible(true); } else { dialog->setVisible(false); } } } But somehow isVisible always returns false (and the dialog is shown). Why? Apr 23, 2015 at 14:48
  • 1
    Is dialog->isVisible() returning false right after you set it to true? I suppose that you have left the default implementations of setVisible, isVisible, show and hide? Apr 23, 2015 at 15:06
  • Yes, all failed, because something along the hierarchy didn't appear to be visible, although I see it. I got it right, so I am going to post it as an answer. Apr 23, 2015 at 15:22
1

The problem with isVisible always returning false was due to it only returning true if all the ancestors are visible, as pointed here: http://doc.qt.io/qt-5/qwidget.html#visible-prop What I fail to grasp is why some of the ancestors (the class is child of a QWidget child of QTabWidget added from QDesigner) would not be marked as visible, as the are drawn on the screen. I did not get isVisible to show whether the widget is visible indeed(as it is) but I applied a workaround using a classical boolean approach:

void Class::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton){
        if (!dialogOpened){
            dialog->show();
            dialogOpened = true;
        } else {
            dialog->hide();
            dialogOpened = false;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.