Pregunta

I have a QMainWindow object parent to a QDialog Object. When I call the QDialog with exec() it stays open, but I can't use the QMainWindow at the same time. If I use show() instead, the QDialog opens and hides immediately after.

I know this relates to the modality flag of the QDialog, but it does not have a Qt::Modeless flag, so I'm a bit lost.

Question: How can I display a QDialog and still be able to interact with its parent QMainWindow?

My code for the QDialog object:

class catalog : public QDialog
{
    Q_OBJECT

public:
    explicit catalog(QWidget *parent = 0);
    ~catalog();

private:
    Ui::catalog *ui;
};

How I'm calling it:

void DiagramWindow::showCatalog()
{
    catalog catalog(this);
    catalog.exec();
}
¿Fue útil?

Solución

It closes, because QDialog::show() method is asynchronous and your catalog object is destroyed right after your code leaves DiagramWindow::showCatalog() method. You should rewrite it like this:

void DiagramWindow::showCatalog()
{
    catalog *c = new catalog(this);
    c->setAttribute(Qt::WA_DeleteOnClose);
    c->show();
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top