Qt QDialog问题:QLineEdit将不接受输入

Qt QDialog Issue : QLineEdit Will Not Take Input

本文关键字:不接受 输入 QLineEdit QDialog 问题 Qt      更新时间:2023-10-16

我有这个基本的Qt程序,它来自于《c++ GUI Programming with Qt》一书。然而,当我按照说明(因为这本书是为Qt 4.4编写的),我的QLineEdit将不显示键盘输入的文本。

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

http # 58;//matthewh.me/Qt4Devel/gotocell//操场/分支用户=客人密码=客人

#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

我最近也遇到过类似的问题。

我是这样解决这个问题的:

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

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

我也遇到过同样的情况。

当我使用this->setWindowFlags(Qt::Popup| Qt::FramelessWindowHint);时,输入不工作。如果这一行被删除,输入工作正常,但是我不能隐藏小部件。