PDA

View Full Version : Modal dialog not behaving



tpf80
19th August 2008, 00:04
I recently upgraded to Qt 4.4.1, and after compiling my program and running it, I noticed that when the first modal dialog, a login popup appears, it does not allow me to click on any of the buttons in it anymore. In addition, I can not drag the login popup around like I normally should be able to. I broke down the code to a simple example, and the same exact thing happens: the dialog will appear, but I can not click on any of the buttons or fields in it. Here is the code for the simple example that has this problem:

Main.cpp:


#include <QApplication>

#include "calculatorform.h"


int main(int argc, char *argv[]) {
QApplication app(argc, argv);

//show the main window:
FabwareMain calculator;
calculator.show();
return app.exec();
}

Calculatorform.h:


#ifndef CALCULATORFORM_H
#define CALCULATORFORM_H
//user interface for this window:
#include "ui_calculatorform.h"

//dialogs that are spawned from this window:
#include "dialog_login.h"

#include "var_login.h"

//QT modules used by this window:
#include <QString>

class FabwareMain : public QMainWindow {
Q_OBJECT

public:
FabwareMain();

private slots:

void popupLogin();

private:
//interface:
Ui::FabwareMain ui;

//variables:
var_login login; //stores last login information
};

#endif



Calculatorform.cpp:


#include <QtGui>

#include "calculatorform.h"
#include <QMessageBox>
#include <QDebug>
/*
Constructor for main window:
*/
FabwareMain::FabwareMain() {
ui.setupUi(this);

connect( ui.actionLogin, SIGNAL( triggered() ), this, SLOT( popupLogin() ) );

}

//popup for login dialog:
void FabwareMain::popupLogin() {

//create a dialog box:
DialogLogin dlg(this);

//do something once we click OK:
if( dlg.exec() == QDialog::Accepted ) {
login = dlg.login;


}
}



Dialog_login.h



#ifndef DIALOG_LOGIN_H
#define DIALOG_LOGIN_H

//user interface items:
#include "ui_dialog_login.h"

//data structure items:
#include "var_login.h"

class DialogLogin : public QDialog, private Ui::DialogLogin {

Q_OBJECT

public:
//functions:
DialogLogin(QWidget *parent = 0);


//variables:
var_login login;

private slots:
void returnVars();

private:

};

#endif



dialog_login.cpp:


#include <QtGui>

#include "dialog_login.h"

//setup interface of main window:
DialogLogin::DialogLogin(QWidget *parent) : QDialog(parent) {

setupUi(this);

//connect form item signals to slots that arent in the UI file here:
connect(buttonBox, SIGNAL(accepted()), this, SLOT(returnVars()));
connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

}

void DialogLogin::returnVars() {

login.username = userid->text();
login.password = password->text();
accept();
}



var_login.h:


#ifndef VAR_LOGIN_H
#define VAR_LOGIN_H

#include <QString>

class var_login {

public:
QString username,
password;

};

#endif

spirit
19th August 2008, 07:43
where is ui file? :eek:
PS. please, post compilable sources in archive, I spent time for creating files etc....

tpf80
19th August 2008, 09:45
Here you go, the same code and the pro files in a zip

jpn
19th August 2008, 10:09
Works fine for me on X11 with both Qt 4.4.0 and Qt 4.5.0-snapshot.

spirit
19th August 2008, 10:18
I found trouble, the trouble in dialog_login.ui,
QLineEdit userid has windowModality property Qt::ApplicationModal.
If you set it in Qt::NonModal, everything works fine.

tpf80
19th August 2008, 11:10
Yes, this fixed the issue. Thank you for your help :)

What is interesting, is that this ui file was made in designer, and I did not see the "windowModality" property in designer for the QLineEdit, so I am not sure how it got there especially since this property for the main widget was Qt::NonModal. Editing the ui file by hand did work though.

JP Kreps
16th September 2008, 20:57
I'm a Qt n00b who inherited the duty to maintain a data entry application written in an earlier version of Qt. I've just started going through the code, learning the application and the Qt environment.

I had the same problem with a QDialog object that you mentioned in your post that started this thread.

My QDialog object contains:

(1) a QTableView to list some records in a query from which the user was to choose one by clicking on it.

(2) an OK QPushButton to accept the choice, and pass the record ID selected to the parent window while closing the QDialog window.

(3) a Cancel QPushButton to close the QDialog window.

Originally, the WindowModality properties of the QDialog window object and (1) and (3) were set to "NonModal" and that of (2) was set to "Aplication Modal".

The QDialog functioned perfectly well when compiled in an earlier version of Qt. But when I compiled it in version 4.4.0, the behavior was exactly as you described.

After I read this thread, I changed the WindowModality property of (2) to "NonModal" to match the WindowModality properties of the QDialog window object and (1) and (3), thinking that the inconstant values of the object properties were causing the problem.

And it worked!:)

Obviously, some "under the hood" functionality of the Qt libraries changed, causing the QDialog window object to behave differently when compiled with the new version even though there were no code or ui changes.

But what puzzles me is that even though the QDialog window object and all the objects it contains now have their WindowModality properties set to "NonModal", the QDialog window still behaves like it is a modal window; that is, you can't shift focus to the parent window without first closing the QDialog child window. This is fine, since that is how it is supposed to function. But by my understanding, if the QDialog is set to be "NonModal", then it shouldn't behave like a modal window, should it?:confused:

Is there something that I don't understand or is this a bug?

tpf80
16th September 2008, 22:03
In my case what seemed to be happening, was that when i use "exec()" then the pop up of course is shown modal. The "Qt::ApplicationModal" property of the QLineEdit caused it to be modal relative to the pop up and blocking all input to the pop up, which in turn blocked the main window, locking up the whole program.

Thus, making the QLineEdit Qt::NonModal made it not block the pop up window, in turn allowing the submit button to work.

This behavior makes sense, however in older versions (and on other operating systems aside from windows) the "Qt::ApplicationModal" property in the .ui file seems to be ignored for the QLineEdit, as I had used the offending .ui file for quite a while on many platforms with no problem.

I am not even sure how my QLineEdit got this property given that I made the UI file with designer, and designer does not give you the option to set this property for QLineEdit. It was however originally made with an older designer in the 4.x series on linux, and then used when I compiled with the latest version on windows. Manually editing the .ui file to remove that property fixed the issue right away.