Qt QDialog问题:QLineEdit不会输入

时间:2011-08-21 06:24:02

标签: c++ qt4.7

我有这个基本的Qt程序来自“C ++ GUI Programming with Qt”一书。但是,当我按照说明(因为本书是为Qt 4.4编写而有所不同)时,我的QLineEdit将不会显示键盘输入的文本。

源代码如下(我没有包含moc _ * .cpp文件)。 对于那些不喜欢通过复制和粘贴重新组装代码的人,可以从我的subversion PlayGround下载整个项目:

http://matthewh.me/PlayGround/branches/Qt4Devel/gotocell/ USER =来宾PASSWORD =来宾

#include <QApplication>
#include <QDialog>

#include <iostream>
#include "gotocelldialog.h"

int main( int argc, char *argv[ ] )
{
    QApplication app( argc, argv );
    GoToCellDialog *dialog = new GoToCellDialog;
    dialog->show( );
    dialog->activateWindow( );
    dialog->lineEdit->activateWindow( );
    return app.exec( );
}

#include <QtGui>
#include <iostream>

#include "gotocelldialog.h"

GoToCellDialog::GoToCellDialog( QWidget *parent )
    : QDialog( parent ) /* Call Parents Constructor */
{
    setupUi( this );
    lineEdit->setEchoMode( QLineEdit::Normal );
    QRegExp regExp( "[A-Z][a-z][1-9][0-9]{0,2}" );
    lineEdit->setValidator( new QRegExpValidator( regExp, this ) );
    //connect( okButton, SIGNAL( clicked( ) ), this, SLOT( accept( ) ) );
    //connect( cancelButton, SIGNAL( clicked( ) ), this, SLOT( reject( ) ) );
}

void GoToCellDialog::on_lineEdit_textChanged( )
{
    //okButton->setEnabled( lineEdit->hasAcceptableInput( ) );
}

#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H

#include <QDialog>

#include "ui_gotocelldialog.h"

/* Adapter Class */
class GoToCellDialog : public QDialog, public Ui::GoToCellDialog
{
    Q_OBJECT
public:
    GoToCellDialog( QWidget *parent = 0 );
private slots:
    void on_lineEdit_textChanged( );
};

#endif

/********************************************************************************
** Form generated from reading UI file 'gotocelldialog.ui'
**
** Created: Sat Aug 20 23:05:28 2011
**      by: Qt User Interface Compiler version 4.7.3
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_GOTOCELLDIALOG_H
#define UI_GOTOCELLDIALOG_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QHeaderView>
#include <QtGui/QLineEdit>

QT_BEGIN_NAMESPACE

class Ui_GoToCellDialog
{
public:
    QLineEdit *lineEdit;

    void setupUi(QDialog *GoToCellDialog)
    {
        if (GoToCellDialog->objectName().isEmpty())
            GoToCellDialog->setObjectName(QString::fromUtf8("GoToCellDialog"));
        GoToCellDialog->resize(286, 110);
        lineEdit = new QLineEdit(GoToCellDialog);
        lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
        lineEdit->setGeometry(QRect(92, 23, 165, 24));

        retranslateUi(GoToCellDialog);

        QMetaObject::connectSlotsByName(GoToCellDialog);
    } // setupUi

    void retranslateUi(QDialog *GoToCellDialog)
    {
        GoToCellDialog->setWindowTitle(QApplication::translate("GoToCellDialog", "Go to Cell", 0, QApplication::UnicodeUTF8));
        lineEdit->setPlaceholderText(QString());
    } // retranslateUi

};

namespace Ui {
    class GoToCellDialog: public Ui_GoToCellDialog {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_GOTOCELLDIALOG_H

3 个答案:

答案 0 :(得分:2)

我最近有类似的问题。

这就是我解决问题的方法:

mainwindow->centralWidget()->releaseKeyboard();

答案 1 :(得分:1)

我解决了这个问题,我没有仔细阅读,我应该创建一个Widget而不是一个对话框。仍然好奇为什么对话框不会输入。我认为这与QDialogBox不是顶级小部件的事实有关吗?

答案 2 :(得分:0)

我也有同样的情况。

当我使用this->setWindowFlags(Qt::Popup| Qt::FramelessWindowHint);时输入不起作用。如果删除此行,则输入正常,但我无法隐藏窗口小部件。