开发者

QDialog doesn't accept text input if modal

开发者 https://www.devze.com 2022-12-19 07:58 出处:网络
Strange bug with a Qt dialog ( Qt4.6/windows) If the dialog is started from MainWindow (from a toolbar/or menu action) and it is modal none of the text boxes will accept any edits. Other controls (dr

Strange bug with a Qt dialog ( Qt4.6/windows)

If the dialog is started from MainWindow (from a toolbar/or menu action) and it is modal none of the text boxes will accept any edits. Other controls (drop down, buttons etc) all work.

If I make the dialog non-modal, with show() rather than exec(), it works perfectly!

Anyone come across anything like this?

开发者_如何转开发

example code:

#include "ui_testDlg.h"    
class TestDlg : public QDialog, public Ui::TestDlg
{
    Q_OBJECT;    
public:
    TestDlg(QWidget *parent=0)  {
        setupUi(this);
    }

    ~TestDlg(void) {}    

private:
    Ui::TestDlg ui;     
};

TestDlg.ui is just the simplest dialog box + lineEdit control created in QDesigner.

void MainWindow::onTest()
{
TestDlg *test = new TestDlg(this);
test->show();  // works
//or    
test->exec();  // opens dlg but can't type in it!
}

EDIT: It is the same problem if I just use a QInputWidget - and it is only a problem in MainWindow. So must be some signal I am blocking/consuming?


You could change the relation between TestDlg and Ui::TestDlg from subclassing to private member.

#include "ui_testdlg.h"

class TestDlg: public QDialog {
    Q_OBJECT
public:
    TestDlg(QWidget *parent = 0) : QDialog(parent), ui(new Ui::TestDlg)
    {
        ui->setupUi(this);
    }
   ~TestDlg()
    {
        delete ui;
    }

private:
    Ui::TestDlg*ui;
};

QtCreator defaults new widget classes like this and with this setup I did not have any problems with the QLineEdit. (Qt 4.6 on WinXP)


I just had the same issue. I figured out a solution for my case, I'm not sure if it's useful for your case too.

So basically I'm trying to create a login dialog. Before I show the mainwindow, I show the login dialog first. if logging in failed, I just quit my program.

My code looks like this:

MainWindow w;
LoginDialog loginDialog;
loginDialog.exec();
w.init();
w.showApp();

When the login dialog shows up, the lineEdit on it can't accept any text input.

but if I reorganize the code this way:

LoginDialog loginDialog;
loginDialog.exec();
MainWindow w;
w.init();
w.showApp();

The lineEdit can work now. I guess something inside the mainwindow's constructor messed up the event loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消